Search code examples
spring-bootrabbitmqspring-rabbit

Proper use of SpringRabbit DirectMessageListenerContainer and setConsumersPerQueue property


I use the DirectMessageListenerContainer to listen to individual queues, since it creates a consumer per queue, preventing queue starvation if a messages on say "queue1" take a while to process, while the one on "queue2" are quick to process. I am confused about the DirectMessageListenerContainer.setConsumersPerQueue method. The default value is 1 looking at the source code.

Under what conditions would one need to increase it to greater than 1? If left to default value of 1, is the processing of messages done in a multithreaded way for a given queue, if fetch size is greater than 1?

    DirectMessageListenerContainer
        listenerContainer =
        new DirectMessageListenerContainer(connectionFactory);

    listenerContainer.setConsumersPerQueue(10);
    listenerContainer.addQueueNames("queue1","queue2");


Solution

  • It's to increase concurrency; for example if your listener is slow, and you don't care about message ordering, you can assign multiple consumers to the same queue and process messages in parallel. Depending on your message count, you may need to reduce the prefetch count so that one consumer doesn't "grab" all the messages during startup.

    The prefetch size has no effect on concurrency; it is just that; prefetch.