问题
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