I have multiple modules which communicate each other by mean of a message queue (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.
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
When you declare SimpleMessageListenerContainer
manually, not as beans, you have also to ensure application context callbacks and lifecycle:
listenerContainer.setApplicationContext()
listenerContainer.setApplicationEventPublisher()
listenerContainer.afterPropertiesSet()
listenerContainer.start()
And don't forget to stop()
and destroy()
them in the end of application.