Tony E. Stark
Tony E. Stark

Reputation: 465

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"?

Upvotes: 3

Views: 789

Answers (1)

Gary Russell
Gary Russell

Reputation: 174494

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).

Upvotes: 3

Related Questions