Search code examples
rabbitmqamqpspring-amqp

How does the 'Publisher returns" happen/work in Spring AMQP?


I am working on RabbitMQ integration. I have a microservice which receives messages from other services. I am currently looking into how to handle messages which encounter exceptions during processing.

The scenario could be:

  1. ServiceA sends message to engine's queue.
  2. Engine processes the message received.
  3. During processing, engine encountered an exception (say a NullPointerException)
  4. Engine returns the message to ServiceA for reprocessing
  5. ServiceA holds the message until the exception in the engine is resolved (resending to engine can be manually triggered)

I bumped into Spring AMQP documentation about Publisher Returns but I could not totally grasp the context. I would like to know how this works and if this could be a solution to address above item #4. Or is there other solution for this?

Thank you in advance!


Solution

  • For #4 on your list the solution is quite simple - don't acknowledge the message automatically, rather then when the processing is finished. In that way - if the client (subscriber) dies (for whatever reason) during processing of the message then that message is re-queued (so sent to ServiceA for reprocessing in your case).

    If you want to explicitly re-queue the message you could do negative acknowledgment (search for it here).

    In any case of re-queuing (manual or automatic) you should be careful that the single message that causes subscribers to die doesn't end up being processed forever by subscriber(s), that is - make sure that the exception that happened during processing was a random and not a guaranteed event. Example for this would be a message containing invalid XML - you process it, see it's invalid, handle the exception and re-queue, but then again another (or the same) subscriber gets it, and handles the same exception since the content of the message and the XML inside it didn't change and so on...