channel.basicQos(1);
while (true) {
GetResponse res = channel.basicGet(TEST_QUEUE, false);
if (res != null) {
deliveryTag = res.getEnvelope().getDeliveryTag();
}
// Handle all messages If the condition is true
if (condition) {
// nack all messages unhandled previously
channel.basicNack(deliveryTag - 1, true, true);
// ack current message only
channel.basicAck(deliveryTag, false);
}
else {
// Do not handle current message and continue to get next one
}
}
Q1。
我不確定如果我可以同時使用nack和ack。
我可以使用deliveryTag - 1來表示以前的所有消息嗎?Nack當前消息和Ack當前消息(rabbitmq,java)之前的所有消息
總之,我想跳過所有不符合條件的消息。
如果當前消息符合條件,那麼消除所有跳過的消息並確認當前消息。
通過這樣做,我想延遲處理一些特定的消息。 Q2302。
恐怕如果我寫作while(true)並且有多個工作人員在運行,那麼channel.basicQos(1)將無法按預期工作。
我應該編寫這樣的代碼來限制計數嗎?或者我應該如何編寫以確保所有其他工作人員均勻地收到郵件?
int prefetch = 1;
int count = 0;
while (count++ <= prefetch) {
}
Q3。
我注意到只要連接處於打開狀態,工作程序就不會終止。
連接將會打開多久,我是否需要手動關閉它?
最後, 的RabbitMQ Java客戶端API和AmqpTemplate VS RabbitTemplate哪一個更適合在該情況下(不使用了MessageListener(ChannelAwareMessageListener)模型)?