如何启动、停止和重新连接 spring rabbitmq 侦听器容器及其连接? [英] How to start, stop and reconnect spring rabbitmq listener containers and their connections?

查看:65
本文介绍了如何启动、停止和重新连接 spring rabbitmq 侦听器容器及其连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个互联网连接,其中出口网关会定期更改.在发生这种情况之前的几秒钟,我收到了我的 Spring 应用程序的事件或回调通知.我想停止我的兔子消费者和连接,然后在几秒钟后再次连接(当我的网络连接恢复时).

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

我正在使用 spring amqp 基于注释的方法,但我也可以切换到另一种实现.

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

我知道 spring-amqp 正在为我重新连接,但我想自己这样做以更好地控制它.我如何以编程方式完全停止所有消费者(RabbitListeners)以及与rabbitmq代理的完全断开连接,并再次干净地连接和启动所有消费者?如果消费者能够重新创建就好了.如果这是不可能的,我将不得不重新初始化一些对象属性.

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.

推荐答案

您可以@Autowire RabbitListenerEndpointRegistry 并调用 stop()停止所有 @RabbitListener 实例.

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

然后在连接工厂上调用resetConnection()来关闭连接.

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

重新启动注册表将导致重新建立​​连接.

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

这篇关于如何启动、停止和重新连接 spring rabbitmq 侦听器容器及其连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆