RabbitMq: dynamically create queues

I have a script in which I want to post some messages to rabbitmq-exchange using a special routing key, for example. Abc

The problem is that there may already be some queue already associated with the routing key "abc", or maybe not. The behavior for such scenarios, apparently, either cancels this message, or if the dead letter exchange is configured, it will be redirected to the dead letter exchange.

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

Is there any known way to do the same?

+6
source share
2 answers

From my research, I do not know how to configure the server side for dynamic queuing. However, you can do this on the client side to achieve the same effect:

Add a ReturnListener to the channel to listen to unbearable messages. Take a look at the “Handling Unused Messages” section on this page for an example:

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

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

+1
source

In your case there is no afaik defaut behavior. You could create a plugin, or you could rely on the client logic, which is the goal of my answer.

It is important to know that the RabbitMQ declare / bind queue is idempotent operation

Queue, create if necessary. This method creates or checks the queue. When creating a new queue, the client can specify various properties that control the durability of the queue and its contents, as well as the level of sharing for the queue.

hypothesis 1: queues cannot be deleted or queues can be deleted, but clients find out about this, a set of queues can fit in memory

Each client supports a set of queues. Before sending a message, the client checks to see if the collection contains a queue. If not, it declares and binds the queue and puts the queue in the set.

When loading, a set of queues can be initialized by existing queues using, for example, the HTTP API (for example, a java client )

How to do this depends on your RabbitMQ client. For example, using spring-amqp , you can extend and override RabbitTemplate#doSend

hypothesis 2: queues can be deleted and clients will not know

As suggested by GeekChick, you can register a ReturnListener . All messages must be sent with a mandatory flag.

hypothesis 3: I am not opposed to declare / bind *

You always, before sending a message, declare and bind a queue. AFAIK value created after it was created should be more or less equivalent to network search + map.

+1
source

Source: https://habr.com/ru/post/984947/


All Articles