RaceBase
RaceBase

Reputation: 18848

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?

Upvotes: 2

Views: 7880

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

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.

Upvotes: 1

Related Questions