java 自动重新连接RabbitMQ通道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15316022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically reconnect RabbitMQ channels
提问by djechlin
I found this gem:
我发现了这个宝石:
In the event of a connection failure, the client will need to establish a new connection to the broker. Any channels opened on the previous connection will have been automatically closed and these will need re-opening too.
如果连接失败,客户端将需要与代理建立新连接。在先前连接上打开的任何通道都将自动关闭,这些通道也需要重新打开。
So that's not good. I'm about to go write a big layer of handling automatic reconnects and recreating the channels then encapsulate this happening from all of my code. Problem is, this should be done already. Is this possible in the Java RMQ libraries?
所以这不好。我将要编写一大层来处理自动重新连接和重新创建通道,然后从我的所有代码中封装这种情况。问题是,这应该已经完成了。这在 Java RMQ 库中可行吗?
回答by falconizer
Perhaps this is a new feature of the RabbitMQ client, but I found this in their docs:
也许这是 RabbitMQ 客户端的一个新功能,但我在他们的文档中发现了这一点:
To enable automatic connection recovery, use factory.setAutomaticRecoveryEnabled(true):
要启用自动连接恢复,请使用 factory.setAutomaticRecoveryEnabled(true):
https://www.rabbitmq.com/api-guide.html
https://www.rabbitmq.com/api-guide.html
Looks like it should solve the problem.
看起来应该可以解决问题。
回答by Jonathan
回答by djechlin
As of RabbitMQ 3.3.0(April 2014) this is possible with Java clients.
从RabbitMQ 3.3.0(2014 年 4 月)开始,Java 客户端可以实现这一点。
This release . . . allows Java-based clients to reconnect automatically after network failure.
本次发布。. . 允许基于 Java 的客户端在网络故障后自动重新连接。
I don't know if this is a server-only change, a change to the client libraries only that makes this possible, etc. Still researching.
我不知道这是否是仅限服务器的更改,仅是对客户端库的更改使这成为可能,等等。仍在研究中。
回答by theMayer
Yes, I agree this is a major disadvantage of the current RabbitMQ client implementations. I have been using RMQ for about 2 years now (the .NET library), and not much has changed about it in that time. It needs to be completely re-written from the ground up, and I just haven't had time to do it yet.
是的,我同意这是当前 RabbitMQ 客户端实现的一个主要缺点。我已经使用 RMQ 大约 2 年了(.NET 库),在那段时间并没有太大变化。它需要从头开始完全重写,我只是还没有时间去做。
But I do have some pointers. First, I would create a wrapper class for your connection/channel object (you need the channel to do AMQP operations, the only thing the connection is used for is to create channels). Then, your wrapper class can keep track of whether the channel or connection is open, and act accordingly.
但我确实有一些指示。首先,我将为您的连接/通道对象创建一个包装类(您需要使用通道来执行 AMQP 操作,连接唯一用于创建通道)。然后,您的包装类可以跟踪通道或连接是否打开,并采取相应的行动。
My code ends up looking like this:
我的代码最终看起来像这样:
while (_iNeedToBeSendingAndReceiving) {
try {
//This blocks indefinitely while waiting for a connection.
using (var channel = ConnectionWrapper.CreateChannel(string connectionString) {
//Do stuff, blah, blah
//When the connection or channel closes, an exception is thrown and
//I move to the catch block.
}
catch(ConnectionInterruptException ex) {
//Eat, yummy!
}
}
My eventual plan is to abstract even this stuff away, and create a completely new way of interacting with the RabbitMQ (or any other messaging) library. I'll let you know when I've gotten some work done on this, it could be a couple months.
我的最终计划是将这些东西抽象出来,并创建一种与 RabbitMQ(或任何其他消息传递)库交互的全新方式。当我完成一些工作时,我会告诉你,这可能需要几个月的时间。