3
votes

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);
    }
}
1
What you are looking for is a fanout. Here is an official example: rabbitmq.com/tutorials/tutorial-three-spring-amqp.html - Urosh T.
Hi @UroshT., thanks for the reply. But, here as well I can see separate queues being used to at listener end. My requirement is to get the messages from single queue to multiple consumers. - Karthik.Vushakola
yes, that is the basic idea - to use the exchange and then multiple queues. Have you maybe considered apache kafka? - Urosh T.
In the end, we had to create multiple queues for achieving the requirement. Thanks Urosh for your inputs. - Karthik.Vushakola

1 Answers

6
votes

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