RabbitMQ message acknowledgment

To understand better how it works, could be useful compare the behavior of a very simple rabbit consumer when the acknowledgment flag is set to true or false in the Channel.basicConsume() call.

I have written a stripped down example where the producer puts a message in a queue and then quits. The consumer works with automatic or explicit acknowledgment accordingly to an input value is provided to it. Looking at the code, we can see how the changes are very localized. We specify that the QueueingConsumer for the channel/queue is in one mode or the other, and then, if an explicit ack is required, we give it when the job is done:
// ...
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, autoAck, consumer); // 1

QueueingConsumer.Delivery delivery = consumer.nextDelivery();
// ...

if(!autoAck) { // 2
    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    // ...
}
1. Here we specify if an handshake is required (autoAck sets to false) or if we rely on the standard auto-acknowledging RabbitMQ policy.
2. When we are done with the message, and if we are not in auto-acknowledgment mode, we give our explicit acknowledgment to the rabbit broker.

The complete Java code is on github.

11 comments:

  1. hi can u explain rabbitmq time to live (ttl) java client examples

    ReplyDelete
    Replies
    1. hi Manny

      how much data can publisher send at a time to queue? can publisher send 1GB data to queue. is any limits is their?

      Delete
    2. Sorry Vadiraj, I don't know. I guess it is a platform-dependent limit, maybe there is some configuration parameter to set.
      If I were you, I would post a question on stackoverflow.com

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Can't help you on this either. I have only used rabbitmq for Java.

      Delete
    5. This comment has been removed by the author.

      Delete
    6. I'd suggest you to write some very simple test cases. When you get stuck on a very definite issue, you could raise a clear question on that point (on stack overflow, possibly).

      Delete
    7. This comment has been removed by the author.

      Delete
    8. Hi Manny

      Is it posible to know what time message is arrived in queue? if yes how to know that?

      Delete
  2. how to remove Duplicate Messages received by consumer(java client)? please explain with example.

    ReplyDelete