简体   繁体   English

如何用Java确认RabbitMQ消息?

[英]How to confirm RabbitMQ messages with Java?

I tried to figure out how to confirm messages in java but I haven't understood it. 我试图弄清楚如何在Java中确认消息,但我不了解。
Here is the official RabbitMQ example: 这是官方的RabbitMQ示例:
http://hg.rabbitmq.com/rabbitmq-java-client/file/default/test/src/com/rabbitmq/examples/ConfirmDontLoseMessages.java http://hg.rabbitmq.com/rabbitmq-java-client/file/default/test/src/com/rabbitmq/examples/ConfirmDontLoseMessages.java

The problem is that they use 10000 messages to send to an queue and only after that they wait when all messages will be confirmed. 问题在于,它们使用10000条消息发送到队列,只有在此之后,它们才会等待所有消息被确认。 I need to send 1 message and only one message per thread and confirm it (In my case I have several equal publishers that have to send messages from time to time). 我只需要发送1条消息,每个线程仅发送一条消息并确认(在我的情况下,我有几个相等的发布者必须不时发送消息)。 How to confirm one message (not confirm all messages)? 如何确认一条消息(不确认所有消息)?

I need something like: 我需要类似的东西:

for (long i = 0; i < MSG_COUNT; ++i) {
    ch.basicPublish("", QUEUE_NAME,
                   MessageProperties.PERSISTENT_BASIC,
                   "nop".getBytes());
    ch.wait_for_confirm();
    if(ch.isConfirmed){
        //OK
    }
    else{
        //Republish
    }
}

Read this post: 阅读这篇文章:

http://www.rabbitmq.com/blog/2011/02/10/introducing-publisher-confirms/ http://www.rabbitmq.com/blog/2011/02/10/introducing-publisher-confirms/

In short you can use the tx-transactions : ch.txSelect(); for (int i = 0; i < MSG_COUNT; ++i) { ch.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_BASIC, "nop".getBytes()); ch.txCommit(); } 简而言之,您可以使用tx-transactionsch.txSelect(); for (int i = 0; i < MSG_COUNT; ++i) { ch.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_BASIC, "nop".getBytes()); ch.txCommit(); } ch.txSelect(); for (int i = 0; i < MSG_COUNT; ++i) { ch.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_BASIC, "nop".getBytes()); ch.txCommit(); }

or the publish-confirmation handler: publish-confirmation处理程序:

ch.addConfirmListener(new ConfirmListener() {....}

The first one is easier but slower than the second one. 第一个比第二个更容易但更慢。

您应该对每条消息都使用“确认”,您可以检查以下链接: https : //www.rabbitmq.com/confirms.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM