gpt4 book ai didi

java - 兔子MQ : Connection recovery mechanism

转载 作者:搜寻专家 更新时间:2023-10-31 20:19:17 25 4
gpt4 key购买 nike

我正在使用 rabbit mq 3.4.1 java 客户端库,但无法使自动恢复机制正常工作。

这就是我创建 rabbit mq 连接工厂的方式:

factory = new ConnectionFactory();
factory.setUsername(userName);
factory.setPassword(password);
factory.setVirtualHost(virtualHost);
factory.setAutomaticRecoveryEnabled(true);
factory.setNetworkRecoveryInterval(5);
factory.setRequestedHeartbeat(3);

消息发布后,如果我关闭 rabbit mq 代理并再次启动它,我希望恢复机制启动并将连接恢复到“正常”状态。但是我收到以下错误:

com.rabbitmq.client.AlreadyClosedException: connection is already closed due to connection error; protocol method: #method<connection.close>(reply-code=320, reply-text=CONNECTION_FORCED - broker forced connection closure with reason 'shutdown', class-id=0, method-id=0)
at com.rabbitmq.client.impl.AMQChannel.ensureIsOpen(AMQChannel.java:190) ~[amqp-client-3.4.1.jar:na]
at com.rabbitmq.client.impl.AMQChannel.transmit(AMQChannel.java:291) ~[amqp-client-3.4.1.jar:na]
at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:654) ~[amqp-client-3.4.1.jar:na]
at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:631) ~[amqp-client-3.4.1.jar:na]
at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:622) ~[amqp-client-3.4.1.jar:na]

我在这里遗漏了什么吗?解决此问题的唯一方法是注册 ShutDownListener 并重新初始化 rabbit mq 连接工厂、连接和 channel 。

也来回答

"chrislott"

评论,我看到自动恢复开始恢复。我使用临时 channel 创建交换:

Channel channel = connection.createChannel();
channel.exchangeDeclare(exchangeName, exchangeType, durable);
channel.close();

我在尝试恢复拓扑时看到以下异常:

Caught an exception when recovering topology Caught an exception while recovering exchange testSuccessfulInitVirtualHost_Exchange: channel is already closed due to clean channel shutdown; protocol method: #method<channel.close>(reply-code=200, reply-text=OK, class-id=0, method-id=0)
com.rabbitmq.client.TopologyRecoveryException: Caught an exception while recovering exchange testSuccessfulInitVirtualHost_Exchange: channel is already closed due to clean channel shutdown; protocol method: #method<channel.close>(reply-code=200, reply-text=OK, class-id=0, method-id=0)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.recoverExchanges(AutorecoveringConnection.java:482)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.recoverEntities(AutorecoveringConnection.java:467)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.beginAutomaticRecovery(AutorecoveringConnection.java:411)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.access$000(AutorecoveringConnection.java:52)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection$1.shutdownCompleted(AutorecoveringConnection.java:351)
at com.rabbitmq.client.impl.ShutdownNotifierComponent.notifyListeners(ShutdownNotifierComponent.java:75)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:574)

如果我不关闭用于创建交换的 channel ,则不会出现上述异常。

最佳答案

我对 RabbitMQ ConnectionFactory#setAutomaticRecoveryEnabled(Boolean) 方法的解读是,它主要支持从 NETWORK 故障中恢复。

这是一个很好的讨论:https://www.rabbitmq.com/api-guide.html

例如,如果您的机器在一段时间内丢失了到代理的路由,可能是由于切换或其他故障,那么自动恢复可以重新建立连接等。文档没有说明幸存经纪人关闭/重启,我认为你的期望是不合理的。

恕我直言,从代理重启中恢复,关闭监听器方法似乎是一种可靠的方法。

关于java - 兔子MQ : Connection recovery mechanism,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com