Spring Rabbitmq Simplerabbitlistenercontainerfactory Usage

From the docs, I want to use consume from queues by dynamically changing the consumers without restarting the application.

I do see that Spring RabbitMQ latest version supports the same, but no clue/example/explanation to change the same. I couldn't see proper source code for the same or how to pass params like maxConcurrentConsumers

I am using XML based configuration of Spring RabbitMQ along with Spring integration

<bean id="rabbitListenerContainerFactory"
      class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
    <property name="connectionFactory" ref="rabbitConnectionFactory"/>
    <property name="concurrentConsumers" value="3"/>
    <property name="maxConcurrentConsumers" value="10"/>
    <property name="acknowledgeMode" value="AUTO" />
</bean>

<int-amqp:inbound-channel-adapter channel="lowInboundChannel" queue-names="lowLoadQueue" advice-chain="retryInterceptor" acknowledge-mode="AUTO" listener-container="rabbitListenerContainerFactory" />
<int-amqp:inbound-channel-adapter channel="highInboundChannel" queue-names="highLoadQueue" advice-chain="retryInterceptor" acknowledge-mode="AUTO" listener-container="rabbitListenerContainerFactory" />

Can anyone guide me how to dynamically configure the consumers?

1 Answer

First of all you shouldn't share the same rabbitListenerContainerFactory for different <int-amqp:inbound-channel-adapter>s, because they do this:

protected void onInit() {
    this.messageListenerContainer.setMessageListener(new ChannelAwareMessageListener() { 

So, only last adapter wins. From other side there is even no reason to have several adapters. You can specify queue-names="highLoadQueue,lowLoadQueue" for a single adapter. Although in case of listener-container you must specify queues on the SimpleRabbitListenerContainerFactory.

If you want to change some rabbitListenerContainerFactory options at runtime, you can just inject it to some service and invoke its setters.

Let me know if I have missed anything.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest