Search code examples
rabbitmqamqpspring-amqpspring-rabbit

Order in which RabbitMQ concurrent listeners listen to the same queue


I have a @RabbitListener that listens to a container of queues.

@RabbitListener(id = CONTAINER_ID, concurrency = "100")
public void instrumentListener(String payload) {
   // do some action
}

My question is:

  1. Can concurrent listeners listen to the same queue?
  2. If so, does concurrent listeners respect the order of objects that are pushed in "the" queue they're listening to.

For example if there is one queue named order_queue where many orders get pushed in it, and there are two concurrent listeners that listen to order_queue (if is possible), and one listener get suspended for few seconds on an order object that it received (now order is out of queue and in this listener body), does the other listener wait for the listener that is suspended.

order_queue elements initially: [5,4,3,2,1] --> head of queue


   queue    concurrent listeners  in process   processed  
[...,5,4] ______ Listener1         (2) 1sec     (1)
         |______ Listener2         (3) 10sec


After 1sec, does the Listener1 waits for 9sec for Listener2 to finish its process with '3' or does Listener1 takes action to get "4" and the order is not respected?

In other words, does everything in a @RabbitListener function body has to finish (ack sent) before the next object in the queue has the permission to release?


Solution

  • No; concurrent consumers are independent; the broker will send preFetch messages to each consumer and they process those messages at whatever rate at which they are running, independently.