rabbitmq取消自动重连_RabbitMQ Java客户端自动重新连接

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.

I am using the latest version of the java client 3.3.4

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.

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.

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

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.

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

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值