2
@RabbitListener(queues="XYZ")
public void rabbitMsgReceiver(@Payload final UserProfile up, Message msg, Channel channel)  {

}

If the message that is received is not a valid JSON of UserProfile, then ListenerExecutionFailedException occurs and the message goes to the unack state. How can I send the message into the dead letter queue?

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Prashant
  • 97
  • 3
  • 11
  • 1
    Are you sure it goes into unack state instead of being rejected and disappearing from the queue? – Adam Michalik Nov 28 '18 at 14:44
  • 1
    You should not use MANUAL acks; let the container handle the acks. A message conversion exception is considered fatal and will be routed to the DLQ, by default. – Gary Russell Nov 28 '18 at 15:26
  • @AdamMichalik yes it is going to the unack state. – Prashant Nov 28 '18 at 15:42
  • @GaryRussell if i don't use MANUAL ack and use AUTO ack then this msg never goes to the dead letter queue. Msg remins into the ready state and Listener received this again and again and throw exception. – Prashant Nov 28 '18 at 15:52
  • Please show the stack trace - with the default error handler, if the exception cause chain contains a `MessageConversionException` it is treated as fatal and the message is rejected instead of nack'd. – Gary Russell Nov 28 '18 at 16:12

1 Answers1

1

You can configure a dead letter queue policy in order to send unacked message to dlq instead of putting them to the original queue

https://www.rabbitmq.com/dlx.html#using-policies

example for your queue XYZ, suppose your set XYZ-dlq for it :

sudo rabbitmqctl set_policy XYZ-dlq "^XYZ$" '{"dead-letter-exchange":"", "dead-letter-routing-key":"XYZ-dlq"}' --apply-to queues 
Lho Ben
  • 2,053
  • 18
  • 35