[英]Multiple Listeners for different queues - Spring Rabbit
I have multiple modules which communicate each other by mean of a message queue (Spring Rabbit).我有多个模块通过消息队列(Spring Rabbit)相互通信。 Some modules produce messages and others consume them.一些模块产生消息,而其他模块使用它们。 However, a single module can listen different queues, I have a list of queue names in a list, so I created a SimpleMessageListenerContainer
for each queue name as follow.但是,单个模块可以侦听不同的队列,我在列表中有一个队列名称列表,因此我为每个队列名称创建了一个SimpleMessageListenerContainer
,如下所示。
public void build() {
for (String queueName: queues) {
SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
listenerContainer.setConnectionFactory(connectionFactory());
listenerContainer.setQueueNames(queueName);
listenerContainer.setMessageListener(listenerAdapter());
}
}
@Bean
private MessageListenerAdapter listenerAdapter() {
return new MessageListenerAdapter(new MessageListener() {
@Override
public void onMessage(Message message) {
System.out.println(message.getBody());
}
}, "onMessage");
}
This implementation is not working for me, consumer are not registered in the queue and any error or exception is throwing during execution.这个实现对我不起作用,消费者没有在队列中注册,并且在执行过程中抛出任何错误或异常。
Note: I am using Spring and I am limited to not use annotations such as @RabbitListener
注意:我使用的是 Spring 并且我被限制不使用@RabbitListener
注释
When you declare SimpleMessageListenerContainer
manually, not as beans, you have also to ensure application context callbacks and lifecycle:当您手动声明SimpleMessageListenerContainer
而不是 bean 时,您还必须确保应用程序上下文回调和生命周期:
listenerContainer.setApplicationContext()
listenerContainer.setApplicationEventPublisher()
listenerContainer.afterPropertiesSet()
listenerContainer.start()
And don't forget to stop()
and destroy()
them in the end of application.并且不要忘记在应用程序结束时stop()
和destroy()
它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.