Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with RabbitMQ fanout exchange

I am able to create a fanout exchange using the Publish/Subscribe RabbitMQ Java tutorial, and any connected consumer will receive a copy of a message. Instead of declaring an exchange and binding dynamically/programmatically, I would like to create the exchange and the binding prior to connecting any consumers. I have done this through the RabbitMQ Management Console. For some reason, however, my consumers are receiving messages in a round-robin fashion, rather than all receiving copies of the message. What am I missing? Here are some code snippets:

Publisher:

channel.basicPublish("public", "", null, rowId.getBytes("UTF-8"));

Consumer:

QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume("myqueue", false, consumer);

...And in the RabbitMQ Management console, I created an exchange "public" of type "fanout", and I set a binding from that exchange to "myqueue".

I'd appreciate any help!

like image 282
littleK Avatar asked Mar 11 '13 15:03

littleK


1 Answers

It sounds like all of your consumers are subscribing to the same queue. When multiple consumers are subscribing to the same queue, the default behavior of RabbitMQ is to round-robin the messages between all the subscribed consumers. See "Round-robin dispatching" in the RabbitMQ Tutorial #2: Work Queues.

The fanout exchange is for ensuring that each queue bound to it gets a copy of the message, not each consumer. If you want each consumer to get a copy of the message, typically you would have each consumer create their own queue and then bind to the exchange. I'm not sure why you're trying to avoid programmatically creating/binding a queue, but if you know ahead of time the number of subscribers and create a queue for each one, you can get the same effect.

like image 133
Brian Zell Avatar answered Sep 19 '22 13:09

Brian Zell