简体   繁体   中英

How to let Spring Boot configure RabbitMQ configuration based on externalised config?

I am trying to find ways to declare queues etc in a way that they are automatically created when the applications starts up. I know that this can be done by adding the creation of the queues via Java code but ideally it would be nice if Spring Boot would configure my RabbitMQ environment based on an xml configuration. I tried creating a resources.xml file with no luck so far. So i am wondering if this is even possible?

Any hints on how to proceed or some example how this could be done?

To allow to Spring AMQP to populate AMQP object on application startup you must declare them as beans and use <rabbit:> namespace for XML configuration to simplify your life.

Somethis like this:

<rabbit:queue name="my.queue" />

<rabbit:direct-exchange name="my.exchange">
    <rabbit:bindings>
        <rabbit:binding queue="my.queue" key="my.routingKey" />
    </rabbit:bindings>
</rabbit:direct-exchange>

When you have that config in the resources.xml , you will be able to import it to the main Boot config:

@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:com/my/proj/configs/resources.xml")
public class MyConfiguration {
}

Thanks to Spring Boot Autoconfiguration, it produces RabbitAdmin bean for us to populate those AMQP objects on start up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM