If a consumer cannot consume a message immediately but other consumers are able to do so, you can reject and requeue the message so that another consumer can consume the message. This article describes how to reject and requeue a message for a consumer.

Reject and requeue a single message for a consumer

The basicReject method can be used to reject a single message. You can call the basicReject method to reject and requeue a message for a consumer.

  • The basicReject method involves the following parameters:
    • deliveryTag: the unique identifier of the message in the channel.
    • requeue: specifies whether to requeue the rejected message. If you set this parameter to true, the rejected message is requeued. If you set this parameter to false, the rejected message is discarded or sent to dead-letter exchanges. For more information, see Dead-letter exchange.
  • The following code provides an example on how to call the basicReject method:
    channel.basicConsume("test", false, "consumertag", new DefaultConsumer(channel) {
       @Override
        public void handleDelivery(String consumerTag, Envelope envelope,
                          AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            System.out.println("Rejected: " + new String(body, "UTF-8") + ", deliveryTag: " + envelope.getDeliveryTag() + ", messageId: " + properties.getMessageId());
            channel.basicReject(envelope.getDeliveryTag(), true);
        }
    });

Reject and requeue multiple messages for a consumer

The basicNack method can be used to reject multiple messages. You can call the basicNack method to reject and requeue multiple messages for a consumer.

  • The basicNack method involves the following parameters:
    • deliveryTag: the unique identifier of the message in the channel.
    • multiple: specifies whether to reject multiple messages. If you set this parameter to true, the message with the specified delivery tag and the messages queued before that message are rejected. If you set this parameter to false, only the message with the specified delivery tag is rejected.
    • requeue: specifies whether to requeue the rejected messages. If you set this parameter to true, the rejected messages are requeued. If you set this parameter to false, the rejected messages are discarded or sent to dead-letter exchanges. For more information, see Dead-letter exchange.
  • The following code provides an example on how to call the basicNack method:
    channel.basicConsume("test", false, "consumertag", new DefaultConsumer(channel) {
       @Override
        public void handleDelivery(String consumerTag, Envelope envelope,
                          AMQP.BasicProperties properties, byte[] body)
                throws IOException {
            System.out.println("Rejected: " + new String(body, "UTF-8") + ", deliveryTag: " + envelope.getDeliveryTag() + ", messageId: " + properties.getMessageId());
            channel.basicNack(envelope.getDeliveryTag(), true, true);
        }
    });