Search code examples
rabbitmqspring-amqpspring-rabbitmessage-listener

How can you create a RabbitMQ MessageListener using a topic exchange with autodeclared queues?


I am using RabbitMQ in Java using the Spring AMQP. I am sending messages using an topic exchange.

I want to consume the messages using autodeclared queues with a specified routingKey and the only way I managed to achieve this was using a Thread, where I would wait for the deliveries (using consumer.nextDelivery()). Is there any other way to do this async?

Also Spring AMQP has a MessageListener interface that I can use only to listen to named Queues. Is there a way I can use the MessageListener to consume messages from autodeclared queues?

Thank you!


Solution

  • Is there a way I can use the MessageListener to consume messages from autodeclared queues?

    Actually it is a feature by defult: http://docs.spring.io/spring-amqp/docs/latest-ga/reference/htmlsingle/#automatic-declaration.

    And it is the main cool feature of AMQP protocol - automatically declare MQ objects on demand. This rule works as for queus as well for exchanges and binding between them.

    So, for you it's just enough to specify the binding for your queue to that topic exchange and refer the queue name from the <listener-container> configuration.

    On start up of your application all AMQP objects will be populated to the RabbitMQ broker via RabbitAdmin component, if they aren't present on the broker yet, of course.