javaspring-bootspring-rabbitspring-framework-beans

Spring Rabbitmq - how to configure the consumer without using @RabbitListener


I'm writing some core features for developers who will use my library. One of the features is to provide an ability to switch the message consumption between two different sources by flag configuration, the handling of these messages should remain as is, no mater to the source - for example switching message consumption from kafka to rabbitmq. the same business logic will be executed with income message.

**I try to figure out how to configure the consumer without using the @RabbitListener, is it possible? **

RabbitAdmin - responsible for connect to the source. RabbitTemplete - responsible for publishing messages.

The only clue that I found is to use springs SimpleMessageListenerContainer, but the issue with him is that there is no ability to set multiple onMessage handlers?

Also I saw the option to use MessageListenerAdapter in this answer

The main issue with these answers is that I'm going to deal with multiple number of queues and bindings.. and here it seem like solution for single consumer in whole application. - am I wrong here?


Solution

  • You need a listener container for each listener.

    You can use Boot's auto configured listener container factory to created each container and add a listener to it.

    If the same listener can consume from multiple queues, you can configure the container to listen to those queues.