問(wèn)題描述
我的 Java 應(yīng)用程序?qū)⑾l(fā)送到 RabbitMQ 交換器,然后交換器將消息重定向到綁定隊(duì)列.我將 Springframework AMQP java 插件與 RabbitMQ 一起使用.
My Java application sends messages to RabbitMQ exchange, then exchange redirects messages to binded queue. I use Springframework AMQP java plugin with RabbitMQ.
問(wèn)題:消息進(jìn)入隊(duì)列,但它停留在Unacknowledged"狀態(tài),它永遠(yuǎn)不會(huì)變成Ready".
The problem: message comes to queue, but it stays in "Unacknowledged" state, it never becomes "Ready".
可能是什么原因?
推薦答案
Unacknowledged 消息意味著它已被您的消費(fèi)者讀取,但消費(fèi)者從未向 RabbitMQ 代理返回 ACK 表示它已完成處理它.
An Unacknowledged message implies that it has been read by your consumer, but the consumer has never sent back an ACK to the RabbitMQ broker to say that it has finished processing it.
我對(duì) Spring Framework 插件并不太熟悉,但是在某個(gè)地方(對(duì)于您的消費(fèi)者)您將聲明您的隊(duì)列,它可能看起來(lái)像這樣(取自 http://www.rabbitmq.com/tutorials/tutorial-two-java.html):
I'm not overly familiar with the Spring Framework plugin, but somewhere (for your consumer) you will be declaring your queue, it might look something like this (taken from http://www.rabbitmq.com/tutorials/tutorial-two-java.html):
channel.queueDeclare(queueName, ....)
然后你將設(shè)置你的消費(fèi)者
then you will setup your consumer
bool ackMode = false;
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, ackMode, consumer);
上面的 ackMode 是一個(gè)布爾值,通過(guò)將其設(shè)置為 false,我們明確告訴 RabbitMQ 我的消費(fèi)者將確認(rèn)收到的每條消息.如果此標(biāo)志設(shè)置為 true,那么您將不會(huì)在 RabbitMQ 中看到 Unacknowledged 計(jì)數(shù),而是在消費(fèi)者讀取消息后(即,它已交付給消費(fèi)者,它將從隊(duì)列中刪除).
ackMode above is a boolean, by setting it to false, we're explicitly saying to RabbitMQ that my consumer will acknowledge each message it is given. If this flag was set to true, then you wouldn't be seeing the Unacknowledged count in RabbitMQ, rather as soon as a consumer has read the message off (i.e it has been delivered to the consumer it will remove it from the queue).
要確認(rèn)消息,您可以執(zhí)行以下操作:
To acknowledge a message you would do something like this:
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
//...do something with the message...
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); //the false flag is to do with multiple message acknowledgement
<小時(shí)>
如果您可以發(fā)布您的一些消費(fèi)者代碼,那么我可能會(huì)提供進(jìn)一步的幫助......但同時(shí)看看 BlockingQueueConsumer 具體來(lái)說(shuō):您將看到的構(gòu)造函數(shù)可以設(shè)置 AcknowledgeMode 并采取查看 nextMessage() 這將返回一個(gè) Message 對(duì)象,其中包含一個(gè)名為 getDeliveryTag() 的方法 這將返回一個(gè) Long ,這是您將在 basicAck 上發(fā)回的 ID
If you can post some of your consumer code then I might be able to help further...but in the mean time take a look at BlockingQueueConsumer specifically: the constructor you will see that you can set the AcknowledgeMode and also take a look at the nextMessage() this will return a Message object which contains a method called getDeliveryTag() this will return a Long which is the ID that you would send back on the basicAck
這篇關(guān)于RabbitMQ:消息保持“未確認(rèn)";的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!