Search code examples
springrabbitmqspring-amqpspring-rabbit

RabbitMQ - How multiple consumers can consume same message from single queue?


Most of the RabbitMQ documentation seems to be focused on round-robin, ie where a single message is consumed by a single consumer. I have a requirement wherein would like to receive the same message from a single queue to multiple subscribed consumers.

Below is my sample consumers code. Here there are 2 listeners listening to the same Queue, but the message is getting received by only one of the consumer. How do I configure it so that the same message gets delivered to both the Consumers? (Consumer1 and Consumer2). Any help will be highly appreciated.

@Component
public class Consumer1 {
    @RabbitListener(queues="test.queue.jsa")
    public void recievedMessage(Employee msg) {
          System.out.println("Recieved Message: " + msg);
    }
}

@Component
public class Consumer2 {
    @RabbitListener(queues="test.queue.jsa")
    public void recievedMessage(Employee msg) {
          System.out.println("Consumed Message: " + msg);
    }
}

Solution

  • This is not possible; it just doesn't work that way. Each consumer needs its own queue; use a fanout exchange.