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

本秂侑毒 提交于 2019-12-21 02:41:42

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/40015338/how-to-start-stop-and-reconnect-spring-rabbitmq-listener-containers-and-their-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!