Spring Boot 多个 RabbitMQ 监听器到单个容器 [英] Spring Boot Multiple RabbitMQ Listeners to Single Container
问题描述
我正在使用 spring boot(基于注释)开发 RabbitMQ.我正在尝试向单个容器添加多个侦听器,但只有一个侦听器可以工作.
I am working on RabbitMQ using spring boot(Annotation based). I am trying to add multiple listeners to a single containers, but only one listener work.
我能够初始化两个容器并为每个容器分配单独的消息侦听器.这是否是添加多个侦听器的唯一方法,其中每个容器都有各自的侦听器?我正在使用 ChannelAwareMessageListener 接口,以便我可以单独处理消息并确认它们.我不想使用 RabbitListener,因为我无法确认消息.
I am able to initialize two containers and assign each container individual message Listener. Whether it is the only way to add multiple listener where each container have their respective listener? I am using ChannelAwareMessageListener interface, so that I can process the message individually and acknowledge them. I don't want to use RabbitListener because I cannot acknowledge the message.
@Bean
@Qualifier("receiverContainer")
SimpleMessageListenerContainer receiverContainer(ConnectionFactory connectionFactory,@Qualifier("receiverAdapter") MessageListenerAdapter
listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
container.setAutoStartup(false);
return container;
}
在spring xml中,我可以给一个容器添加两个监听器
In spring xml, I can add two listener to one container
<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="auto" concurrency="10"
requeue-rejected="true">
<rabbit:listener ref="myMessageListener" queues="spring.queue" />
<rabbit:listener ref="messageQueueManager" queues="user.login.notification" />
</rabbit:listener-container>
那么如何在基于 Spring Boot 注释的一个容器中添加两个侦听器?或者我应该采用一个容器一个监听器的方式?
So how do I add two listener to one container in spring boot annotation based? Or I should go with one container-one listener way?
推荐答案
否;每个容器只允许一个监听器.
No; only one listener per container is allowed.
在使用 XML 命名空间时,虽然看起来"有多个侦听器,但实际上每个 <rabbit:listener/>
元素都有一个单独的容器.
When using the XML namespace, although it "looks" like there are multiple listeners, there is really a separate container for each <rabbit:listener/>
element.
<rabbit:listener-container/>
父元素只是一种为子元素创建的每个容器指定通用容器属性的便捷方式.
The <rabbit:listener-container/>
parent element is just a convenient way to specify common container attributes for each container created by the child elements.
这篇关于Spring Boot 多个 RabbitMQ 监听器到单个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!