Search code examples
javarabbitmqspring-amqpspring-rabbit

Listening to many short-lived, dynamically created queues with Spring AMQP


I'm building an application using RabbitMQ/Spring/Spring AMQP and am having trouble handling the way I've laid out my queues.

Essentially I have one queue that every consumer listens to, with each message basically saying "this queue is ready to be processed by a single consumer". The consumer will then listen to the queue indicated in the message, consume all the messages in that queue, and finally delete it when done.

These short lived queues are all created on the fly as data comes in to be processed and cannot be consumed by multiple consumers (whichever gets the message in the 'ready' queue).

I'm having trouble gracefully handling the consumers in this situation. Right now I just create a new DirectMessageListenerContainer each time a consumer gets a message from the 'ready' queue and then stop it once it has gotten all the messages it needs. It seems like this solution isn't ideal. Is there any better way to handle a situation like this with Spring AMQP/RabbitMQ?


Solution

  • You can add/remove queues to/from existing container(s) at runtime; it is more efficient with the direct container (see Choosing a container).

    The MessageProperties has the consumerQueue property to tell you which queue the message came from.