RabbitMq : Create queue dynamically

Rahul picture Rahul · Apr 10, 2015 · Viewed 7.4k times · Source

I have a scenario where I want to publish some messages to rabbitmq-exchange using a specific routing key for eg. abc

The issue is there may already be any queue already binded with routing key "abc" or may be not. The behavior for such scenarios seem to be either drop that message or if a dead letter exchange is configured, it will be routed to dead letter exchange.

I want to dynamically create a queue with same name as the routing key i.e. "abc" if no queue is present for that routing key instead of dropping or sending it to DLX.

Is there any known way to do the same?

Answer

GeekChick picture GeekChick · Apr 13, 2015

From my research, I'm not aware of a way to configure the server side to create queues dynamically. However, you could do this on the client side to achieve the same effect:

Implement a ReturnListener on the channel to listen for unroutable messages. Look at the "Handling Unroutable Messages" section on this page for an example:

https://www.rabbitmq.com/api-guide.html

Then, you can use the routingKey that's passed into the handler to create a queue with the same name, using the queueDeclare() and queueBind() methods (see "Using exchanges and queues" on the same link for an example).