Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create RabbitMQ connection early-on with Spring Boot and Spring AMQP

I'm using Spring Boot 1.3.2 and Spring AMQP (with mostly default configuration) to achieve communication between a set of microservices, and I notice that the first "request" after the startup of each microservice takes a "long" time because it is when the connection to RabbitMQ is created.

Is there any way to force the connection to be created early-on (during the startup phase) instead of being created lazily on the first "request"?

like image 494
Tony E. Stark Avatar asked Oct 27 '25 03:10

Tony E. Stark


1 Answers

If you are consuming messages then the listener container will establish a connection on startup.

If you are only producing messages then you will see the behavior you describe.

To work around it; create a class that implements SmartLifecycle; @Autowire the connection factory; in the start() method, execute this.connectionFactory.createConnection() and, add a @Bean to the context.

There's no need to "close" the connection (but it won't hurt), it is a single connection that's used for all channels (by default).

like image 70
Gary Russell Avatar answered Oct 28 '25 18:10

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!