John
John

Reputation: 215

How to use Dead letter in RabbitMQ?

I've setup RabbitMQ and have followed the tutorials and I've got a basic pub/sub setup going on. I've now got to to the resilience part and handling bad actors. Eg, messages that can never be actioned. Rather than deleting them completely I'm wanting them to be redirected to a dead letter queue so I can inspect them later.

With MSMQ its a simple process of adding .error on the queue namespace but Rabbit seems to be more indepth and low level.

I've got the following configured for my consumer constructor code

    {
        _logger = loggerFactory.CreateLogger<AsyncUpdateProcessor>();

        InboundQueueName = options.Value.InboundQueueName;

        var factory = new ConnectionFactory
        {
            HostName = options.Value.Hostname,
            UserName = options.Value.Username,
            Password = options.Value.Password
        };

        // create connection  
        _connection = factory.CreateConnection();

        // create channel  
        _channel = _connection.CreateModel();

        _channel.ExchangeDeclare("some.exchange.name", "direct");

        Dictionary<String, Object> args = new Dictionary<String, Object>();
        args.Add("x-dead-letter-exchange", "some.exchange.name");
        _channel.QueueDeclare(queue: InboundQueueName,
            durable: false,
            exclusive: false,
            autoDelete: false,
            args);

    }

But the declare queue errors

RabbitMQ.Client.Exceptions.OperationInterruptedException: 'The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text='PRECONDITION_FAILED - inequivalent arg 'x-dead-letter-exchange' for queue 'msg.inbound' in vhost '/': received the value 'some.exchange.name' of type 'longstr' but current is none', classId=50, methodId=10'

Upvotes: 0

Views: 1321

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

Message is clear, you have some other queue msg.inbound, which is created before the current queue InboundQueueName, so you you to either delete the msg.inbound or recrate it with the x-dead-letter-exchange

Upvotes: 0

Related Questions