First of, let me start with a disclaimer. I wont claim to be a master at working with AMQP principles but I have found alot of guidance by reading through the introduction series at the RabbitMQ documentation page here for new beginners.

That said, poison messages are a developers worst nightmare whichever way you looks at it. They usually occur when one doesn’t use a bus to handle the messages, i.e when the developers hard wires the code required to deal with message handling. A quick overview of how a message gets to the queue is as below.

Instance of a type ->Serialize ->convert to by array – >publish.

The consumption of messages is exactly the reverse of the above and the impact of poison messages happens when the message is being converted from Json string to an instance of a type. This means that its impossible to complete the process of message consumption and hence the acknowledgement flag is not send back to RabbitMQ. Therefore that message cannot be dequeued  hence stalling the consumption of the next message in line and by extension the rest of the messages.

There are two remedies to this issues all centered around the use of Newtonsoft Javascript  framework.

  1. Before deserializing the messages compare against a schema and on fail just mark the message as in wrong format and move forward.
  2. Simply confirm that the message is a valid JSON string and proceed as necessary. Below is my implementation of this approach which has ensured that I don’t run into situations where my messages get stuck in the queue as a result of some poison messages sitting on the top.
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Linq;
static bool IsSchemaValid(string json)
        {           
            try
            {
                JObject tweet = JObject.Parse(json);

                return true;
            }
            catch(Exception e)
            {
                errorLogger.Log(LogLevel.Error, e.Message + " : " + e.StackTrace);
                return false;
            }
        }

 

Leave a Reply

Your email address will not be published. Required fields are marked *