3

I want to implement DLX in rabbit MQ which sets the retry count for the failed messages. If the retry count is greater then 5 then the message will automatically be discarded.

Below is the snippet for the DLX.

        string WORK_EXCHANGE = "DeadExchange"; // dead letter exchange
        string RETRY_QUEUE = "RetryQueue";
        int RETRY_DELAY = 10000;

        var queueArgs = new Dictionary<string, object> {
            { "x-dead-letter-exchange", WORK_EXCHANGE },
            { "x-message-ttl", RETRY_DELAY }
        };

        var properties = channel.CreateBasicProperties();
        properties.Persistent = true;

        channel.ExchangeDeclare(exchange: WORK_EXCHANGE,
                                type: ExchangeType.Direct);

        channel.QueueBind(queue: queueName, exchange: WORK_EXCHANGE, routingKey: routingKey, arguments: null);

        channel.ExchangeDeclare(exchange: RETRY_EXCHANGE,
                                type: ExchangeType.Direct);

        channel.QueueDeclare(queue: RETRY_QUEUE, durable: true, exclusive: false, autoDelete: false, arguments: queueArgs);
        channel.QueueBind(queue: RETRY_QUEUE, exchange: RETRY_EXCHANGE, routingKey: routingKey, arguments: null);
        channel.BasicPublish(exchange: RETRY_EXCHANGE, routingKey: routingKey, mandatory: true, basicProperties: properties, body: message);

I want to read x-Death property as shown in below queue message.

So far I have reached

(BasicDeliveryEventArgs)ea.BasicProperties.Headers["x-death"]

Queue Message Properties

TrinTrin
  • 430
  • 5
  • 12

1 Answers1

0

You could retrieve count in this way :

    private long? GetRetryCount(IBasicProperties properties)
    {
        if (properties.Headers.ContainsKey("x-death"))
        {
            var deathProperties = (List<object>)properties.Headers["x-death"];
            var lastRetry = (Dictionary<string, object>)deathProperties[0];
            var count = lastRetry["count"];
            return (long)count;
        }
        else
        {
            return null;
        }
    }

I hope it will be useful for someone

Sergii Lebid
  • 63
  • 1
  • 4