Consumers in AMQP 0-9-1 can choose to use manual acknowledgements of deliveries.
The AMQP 0-9-1 specification defines the basic.reject
method that allows clients to reject individual, delivered messages, instructing the broker to either discard them or requeue them. Unfortunately, basic.reject
provides no support for negatively acknowledging messages in bulk.
To solve this, RabbitMQ supports the basic.nack
method that provides all the functionality of basic.reject
whilst also allowing for bulk processing of messages.
To reject messages in bulk, clients set the multiple
flag of the basic.nack
method to true
. The broker will then reject all unacknowledged, delivered messages up to and including the message specified in the delivery_tag
field of the basic.nack
method. In this respect, basic.nack
complements the bulk acknowledgement semantics of basic.ack
.
Negative acknowledgements work for both long running consumers and polling-based ones (that use basic.get
).
When a message is requeued, it will be placed to its original position in its queue, if possible. If not (due to concurrent deliveries and acknowledgements from other consumers when multiple consumers share a queue), the message will be requeued to a position closer to queue head.
This Java client example rejects a single message consumed via polling (basic.get
), asking the broker to requeue it:
GetResponse gr = channel.basicGet("some.queue", false); channel.basicNack(gr.getEnvelope().getDeliveryTag(), false, true);
This example rejects two messages with a single call to the broker (the second argument on basicNack
is the multiple
flag):
GetResponse gr1 = channel.basicGet("some.queue", false); GetResponse gr2 = channel.basicGet("some.queue", false); channel.basicNack(gr2.getEnvelope().getDeliveryTag(), true, true);