問題描述
我正在使用 Java 客戶端在 RHEL 5.3 上使用 RabbitMQ.我有 2 個節(jié)點(機器).Node1 正在使用 Java 幫助程序類 QueueingConsumer 從 Node2 上的隊列中消費消息.
I'm using RabbitMQ on RHEL 5.3 using the Java client. I have 2 nodes (machines). Node1 is consuming messages from a queue on Node2 using the Java helper class QueueingConsumer.
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("MyQueueOnNode2", noAck, consumer);
while (true)
{
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
... Process message - delivery.getBody()
}
如果接口在 Node1 或 Node2 上關(guān)閉(例如 ifconfig eth1 down),客戶端(上圖)永遠不會知道網(wǎng)絡(luò)不再存在.RabbitMQ 是否在 Java 客戶端上提供某種類型的配置,可用于確定連接是否已消失.關(guān)閉 Node2 上的 RabbitMQ 服務(wù)器將觸發(fā) ShutdownSignalException,可以捕獲該異常,并且應(yīng)用程序可以進入重新連接循環(huán).但是關(guān)閉接口不會導(dǎo)致任何類型的異常發(fā)生,因此代碼將在 consumer.nextDelivery() 上永遠等待.
If the interface is brought down on Node1 or Node2 (e.g. ifconfig eth1 down), the client (above) never knows the network isn't there anymore. Does RabbitMQ provide some type of configuration on the Java client that can be used to determine if the connection has gone away. Shutting down the RabbitMQ server on Node2 will trigger a ShutdownSignalException, which can be caught and the app can go into a reconnect loop. But bringing down the interface doesn't cause any type of exception to happen, so the code will be waiting forever on consumer.nextDelivery().
我也嘗試過使用這個調(diào)用的超時版本.例如
I've also tried using the timeout version of this call. e.g.
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("MyQueueOnNode2", noAck, consumer);
int timeout_ms = 30000;
while (true)
{
QueueingConsumer.Delivery delivery = consumer.nextDelivery(timeout_ms);
if (delivery == null)
{
if (channel.isOpen() == false) // Seems to always return true
{ throw new ShutdownSignalException(); }
}
else
{
... Process message - delivery.getBody()
}
}
但似乎這總是返回 true(即使接口已關(guān)閉).我假設(shè)在連接上注冊 ShutdownListener 會產(chǎn)生相同的結(jié)果,但還沒有嘗試過.
but appears that this always returns true (even though the interface is down). I assume registering for the ShutdownListener on the connection will yield the same results, but haven't tried that yet.
有沒有辦法配置某種心跳,或者你只需??要編寫自定義租約邏輯(例如我現(xiàn)在在這里")就可以讓它工作?
Is there a way to configure some sort of heartbeat, or do you just have to write custom lease logic (e.g. "I'm here now") in order to get this to work?
推薦答案
一般來說,你最好在 rabbitmq-discuss 郵件列表上發(fā)布有關(guān) rabbitmq 的問題.我們不傾向于跟蹤在此之外提出的問題.
In general, you're much better off posting questions regarding rabbitmq on the rabbitmq-discuss mailing list. We don't tend to track questions being asked outside of this.
您可以配置心跳,但默認情況下它是關(guān)閉的.您也可以打開 TCP Keep Alive.在創(chuàng)建新連接之前調(diào)用 ConnectionFactory
上的 setRequestedHeartbeat
,或者,子類 ConnectionFactory
,覆蓋 configureSocket
方法,并調(diào)用 socket.setKeepAlive(true)
.當(dāng)網(wǎng)絡(luò)中斷時,兩者都應(yīng)該導(dǎo)致連接通知.
There is a heartbeat that you can configure, though it is off by default. You could also turn on TCP Keep Alive. Either call setRequestedHeartbeat
on the ConnectionFactory
before creating a new connection, or, subclass ConnectionFactory
, override the configureSocket
method, and call socket.setKeepAlive(true)
. Both should result in the connection noticing when the network dies.
這篇關(guān)于使用 RabbitMQ(Java 客戶端),有沒有辦法確定消費期間網(wǎng)絡(luò)連接是否關(guān)閉?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!