Consuming message with specific routing key?

Viewed 2419

I am new to Rabbit MQ. Read couple of Rabbit MQ tutorial

In the mentioned link my question is about below ReceiveLogs

     channel.basicConsume(queueName, true, consumer)

As single queue can hold messages routed with different routingKey. Say if consumer want to consume message with specific routing key. Is it possible ? Do i need to publish meesage with single routing key only on specific queue in this case ?

I do not see any method under Channel API

1 Answers

In RabbitMQ producer publishes to an exchange not to a queue - this decouples producer from the consumer. Yes, as described in the official article you can have messages routed with different routing key in the same queue, you just need to create more than one binding for that queue for this to happen.

Now to directly answer your question: if you want to have one routing key per queue you need to do the following:

for each routing key you want consumer to listen to:

  • Create a queue
  • Call channel.queueBind(queueName, EXCHANGE_NAME, <your_routing_key>); only once for that queue.
Related