Badal
Badal

Reputation: 376

Avail same messages to multiple RabbitMQ Consumers

Requirement: 1)I need to fetch data stored in Mongo DB through Java Application and using topic exchange & binding keys, created 3 queues on RabbitMQ. I have implemented everything up to this point.

The problem starts from the 2nd point onwards.

2) When the messages should be available to multiple consumers from all the 3 queues. But when first consumer consumes the messages from 3 queues it will not be available for the rest of the consumers. How to make messages highly available to multiple consumers.

Is there any ways to achieve this or is this requirement has any alternate solutions to it.

Upvotes: 3

Views: 648

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121550

All your consumers must provide their own unique queue and bind it to the same exchange.

There is no such a Topic abstraction in AMQP, like it is with JMS.

Even if we can publish message through the topic or fanout exchange, the message will be placed to the queue as single entry, so only one consumer will be able to pick it up from the the.

The config for my proposition may look like:

<queue id="commandQueue" name="#{node.id}.command"
       auto-delete="true"/>

<fanout-exchange name="commandsExchange">
    <bindings>
        <binding queue="commandQueue"/>
    </bindings>
</fanout-exchange>

<amqp:inbound-channel-adapter id="commandConsumer"
                              queue-names="#{commandQueue.name}"
                              channel="commandChannel"/>

With that all my application instances bind their unique queue (based on the node.id abstraction) to the same commandsExchange. And the published message to the commandsExchange will be delivered to all my nodes.

auto-delete="true" helps me to avoid extra messages for my queue, if node is dead.

HTH

Upvotes: 4

Related Questions