Search code examples
javaspring-bootspring-amqpspring-rabbit

Connecting to queue or creating on non-existence in spring-rabbitmq


I am using Spring Boot with spring-rabbitmq. My connection factory is configured in application.properties and it seems to be nice.

My aim is: during start check if exists queue if specific name, and in case of absence create such queue. I am not sure how to deal with it. What beans should I create in config class? From what I read it should be RabbitAdmin, however I am not sure about it. Can you help me?


Solution

  • Everything is described clearly in the Reference Manual:

    The AMQP specification describes how the protocol can be used to configure Queues, Exchanges and Bindings on the broker. These operations which are portable from the 0.8 specification and higher are present in the AmqpAdmin interface in the org.springframework.amqp.core package.

    And further:

    When the CachingConnectionFactory cache mode is CHANNEL (the default), the RabbitAdmin implementation does automatic lazy declaration of Queues, Exchanges and Bindings declared in the same ApplicationContext.

    So, you should declare Queue, Exchange and Binding beans in your application context and AmqpAdmin will take care about their definition on the target Broker.

    There must be a note that according AMQP protocol, if entity already exists on the Broker, the declaration is just silent and idempotent.

    So, in your case you don't need to worry about queues existence and just provide their declarations as beans in the application context.