Search code examples
rabbitmqspring-rabbitrabbitmq-exchange

Single active consumer (SAC) - having a consumer thread consume only from a single queue


I'm trying to use single active consumer (SAC) in queues bounded to consistent hash exchange, which has 4 queues. This is for distributing the messages based on routing key and to have ordered processing in each of the queues.

Have set an Executor thread pool with 4 thread in TaskExecutor while creating Spring SimpleMessageListenerContainer, assuming to have one consumer thread per queue.

simpleContainer.setTaskExecutor(Executors.newFixedThreadPool(4));

When testing, there is only one thread is consuming from all the 4 queues, rarely two threads consume messages from the 4 queues. Which is like having a single consumer consumes from multiple queues.

What I need is to have a consumer thread consume from only one queue at a time, with SAC enabled. In this case I expect 4 threads from the thread pool consume from one queue each. Is there a way to achieve this?.


Solution

  • If you increase the concurrency to 4, you will get 4 consumers, each consuming from all 4 queues; but the SAC will mean only one of them will actually consume from each queue.

    However, there is no guarantee that each will get one queue.

    Or you can use 4 separate containers, one for each queue, with concurrency=1.

    Or, the DirectMessageListenerContainer uses 1 consumer per queue (by default).