Skip to content
Advertisement

Order of message receiving with concurrent consumers in Spring and RabbitMQ

I have following code, so I read messages from one queue and resend it, to another one.

I am interested in setConcurrentConsumers(3) method does it means that three listener threads will be created?

And in such case how I understood order of re-sending to queue1 and queue2 will not be met. As for me is importation to have the same messages order like when I receive it.

@RabbitListener(queues = "queue",containerFactory="rabbitListenerContainerFactory") 
public void processQueue(String message) {
    rabittemplate.send("queue1", message);
    rabittemplate.send("queue2", message);
}

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setConcurrentConsumers(3);
    factory.setMaxConcurrentConsumers(10);
    return factory;
}

Advertisement

Answer

The whole point of enabling concurrentConsumers is to allow to process messages in a parallel fashion, in order to speed up the overall execution. By doing this you are automatically signing a contract where you accept that everything is asynchronous and you can not anymore assume order.

If you define a sequence between threads and wait for the previous to finish so that you can respect the order, than you are returning to sequential processing and there is no benefit of having enabled concurrent consumes, instead you will be having overhead for handling multiple threads.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement