簡體   English   中英

RabbitMQ:連接恢復機制

[英]RabbitMQ : Connection recovery mechanism

我使用的是兔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連接工廠,連接和通道。

也回答

“chrislott”

評論,我看到自動恢復開始恢復。 我使用臨時頻道創建交換:

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)

如果我不關閉用於創建交換的通道,則不會出現上述異常。

我讀取RabbitMQ ConnectionFactory#setAutomaticRecoveryEnabled(布爾)方法的主要原因是它能夠從NETWORK故障中恢復。

這是一個很好的討論: https//www.rabbitmq.com/api-guide.html

例如,如果您的機器在一段時間內丟失了通往代理的路由,可能是由於交換機或其他故障,那么自動恢復可以重新建立連接等。文檔沒有說明有關幸存的代理關閉的信息/ restart,我認為你的期望不合理。

恕我直言從代理重啟恢復,關機監聽方法似乎是一個堅實的方法。

通常兔子客戶端應該自己處理恢復 - 你不應該手動重新實現它。 至少嘗試使用lyra

我在故障轉移測試期間遇到了一些問題。 連接往往在代理重啟時開始永遠掛起,因此關閉信號異常是日志中的最后一件事。 我通過設置修復它:

factory.setConnectionTimeout(20000);

臨時隊列也無法恢復恢復。 如果你有那些你可能需要做一些額外的處理(再次嘗試lyra)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM