Search code examples
spring-bootspring-amqpspring-rabbit

What's the correct exception type to NACK a message using annotation based listener in spring-boot with amqp?


I'm using spring boot with spring-amqp and annotation based listener to consume message from a rabbitmq broker. I've a spring component which contains a method like this:

  @RabbitListener(queues = "tasks")
  public void receiveMessage(@Payload Task task) {...}

I'm using the AUTO mode to acknowledge messages after successful execution of receiveMessage(...). If i detect a special error, i'm throwing AmqpRejectAndDontRequeueException to get this message into a configured dead letter queue. Now i need to nack a message only, so that the message gets requeued into the main queue of rabbitmq and another consumer has the possibility to work on that message again.

Which exception should i throw for that? I wouldn't like to use channel.basicNack(...) like described here (http://docs.spring.io/spring-integration/reference/html/amqp.html) if possible.


Solution

  • As long as defaultRequeueRejected is true (the default) in the container factory, throwing any exception other than AmqpRejectAndDontRequeueException will cause the message to be rejected and requeued.

    The exception must not have a AmqpRejectAndDontRequeueException in its cause chain (the container traverses the causes to ensure there is no such exception).