RabbitMQ Java客户端自动重新连接 [英] RabbitMQ Java client auto reconnect

查看:5752
本文介绍了RabbitMQ Java客户端自动重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序失去与RabbitMQ的连接时,我将其连接工厂设置为自动尝试并重新连接

When my application looses connection to RabbitMQ I have its connection factory set to automatically try and reconnect

ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    factory.setRequestedHeartbeat(1);
    factory.setConnectionTimeout(5000);
    factory.setAutomaticRecoveryEnabled(true);
    factory.setTopologyRecoveryEnabled(true);

当它试图重新连接它时阻止但是一旦它再次连接它就永远不会停止阻塞不确定原因。

When it is trying to reconnect it blocks but it never stops blocking once it gets connected again and I am not to sure why.

我使用的是最新版本的java客户端3.3.4

I am using the latest version of the java client 3.3.4

这也是当我通过rabbitmq管理界面强行断开客户端连接时似乎发生了这种情况。

This also seems to happen when I force disconnect the client connection via the rabbitmq management interface.

在尝试获取频道但网络时,它似乎还有待进一步研究界面说已经连接了一个频道。

Some further research it seems like its hanging while it is trying to get a channel but the web interface says there is a channel connected.

推荐答案

将它们全部包装在一起,这是我的答案。

To wrap it all together, here my answer.

首先,rabbitmq,或者至少是java客户端,有一些奇怪的默认属性,有时是几秒钟,有时是几毫秒。从我的观点来看,他们的默认值似乎也没有考虑过。例如 connectionTimeout s默认值为0,这意味着等待永远。

First of all rabbitmq, or at least the java client, has some weird default properties which are sometimes in seconds and sometimes in milliseconds. Also their default values do not seem well thought from my point of view. For example connectionTimeouts default value is 0, which means wait for ever.

您还应该设置 networkRecoveryInterval 属性到您环境中的一些有用值。

You should also set the networkRecoveryInterval property to some usefull value in your environment.

要获得更多信息导致频道关闭的原因,请执行 ShutdownListener - 即使它只打印异常,它也会帮助您找出导致错误的原因。

To get a more information what caused the channel to close, implement a ShutdownListener - Even if it just prints the exception, it will help you to find out what caused the error.

为了测试连接的自动恢复,我在客户端使用了ip tables命令。然后你还可以看到rabbitmq.log中服务器端发生了什么

To test auto recovery of a connection I used an ip tables command on the client side. Then you can also see what happens on the server side in the rabbitmq.log

并且不要忘记调整 requestHeartbeet 属性,在我的设置中它总是超过5秒,默认的是在一些客户端实现580秒,这似乎有点奇怪,记住这一点。

And do not forget to adjust the requestHeartbeet property, in my setup its always above 5 seconds, the default one is in some client implementations 580 seconds which seems a bit strange also, keep that in mind.

无论如何,即使您在群集中运行rabbitmq,它也不能在不可靠的网络上运行良好。也许您对此有更多的兴趣,我建议您查看文档

In any way, even if you run rabbitmq in a cluster, it does not work well on unreliable networks. Maybe you are interested in more regarding this than I should advise you to look in the documentation.

这篇关于RabbitMQ Java客户端自动重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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