Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start, stop and reconnect spring rabbitmq listener containers and their connections?

I've an internet connection where the exit gateway periodically changes. I'm getting an event or callback notification to my spring application some seconds before this happens. I would like to stop my rabbit consumers and connections then and connect again after some seconds (when my network connection is back).

I'm using the annotation based approach of spring amqp but i could also switch to another kind of implementation.

I know that spring-amqp is doing a reconnect for me but i would like to do that by myself to have more control over it. How can i programmatically do a clean stop of all consumers (RabbitListeners) and also a clean disconnect from rabbitmq broker and again a clean connect and start of all consumers? It would be fine if the consumers would get recreated. If this is not possible i would have to reinitialize some object attributes.

like image 770
whitenexx Avatar asked Feb 06 '23 22:02

whitenexx


1 Answers

You can @Autowire the RabbitListenerEndpointRegistry and call stop() to stop all the @RabbitListener instances.

Then call resetConnection() on the connection factory to close the connection.

Restarting the registry will then cause the connection to be re-established.

like image 72
Gary Russell Avatar answered Feb 11 '23 17:02

Gary Russell