Search code examples
rabbitmqamqp

RabbitMQ - basic reject with metadata


I've got a consumer that rejects messages and knows exactly why those messages were rejected. She'd like to provide the "why" as well as the "what" to the producer when rejecting a message.

What's a good queue architecture for nack'ing messages but also sending back metadata describing why the message failed?

(At a higher level, if the producer isn't doing anything with the 'nacked reason codes, I'm thinking logging the reason codes from the consumer would suffice for visibility, so the question becomes moot. Still, seems like an interesting question assuming otherwise.)


Solution

  • You can use the RPC model as described here: https://www.rabbitmq.com/tutorials/tutorial-six-java.html In this way you can send-back to the publisher a message with the reason.

    You can also considerer Dead Letter Exchanges extension, but you can't change the message, so you are just informed that your message has been rejected.

    With a little work, you can create an exchange where you redirect the nack messages, and using the header property message to write the reason, like that:

     Map<String, Object> myHeader = new HashMap<String, Object>();
     myHeader("reason", "can't access to database");//<-- just an example
     AMQP.BasicProperties.Builder bob = new AMQP.BasicProperties.Builder();
     bob.headers(myHeader);  
    

    In this way you can maintain the original message and modify only the header. (similar to Dead Letter Message)

    hope it helps