Search code examples
javaspringrabbitmqspring-amqpspring-rabbit

Put unacknowledged message(on exception) to a different queue in RabbitMQ


On exception while processing the message from RabbitMQ, i just wanted to unacknowledg and put back the particular message to a difference queue instead or re-queue to same queue or completely discard the message(as per the last Boolean flag@requeue in basicNack).

The whole idea is later on i can get the count of unack message and check the message format etc instead of re-queue again and again to same channel and also i want to sent unacknowledged signal to current channel.

FYI I set the channel ack mode as manual(i.e container.setAcknowledgeMode(AcknowledgeMode.MANUAL);)

This is what i am doing now.

public class My***Listener implements ChannelAwareMessageListener{

try{

    @Override
    public void onMessage(Message message,Channel channel) throws Exception {   
    String s = new String(message.getBody());
    //some logic
    //after successful ack manually
    channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
catch(Exception e){
      //currently on exception i am unack the channel
      channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
}

Any help is highly appreciable.


Solution

  • You can send them to a dead letter queue. It's a pretty standard pattern.

    https://www.rabbitmq.com/dlx.html