RabbitMq: Messages are not appearing in a RabbitMq queue when adding DLX or making it as Durable

42 views Asked by At

I have queue configured in my RabbitMq server which is bound to default exchange. The queue name is "MobileBankingResponse". It worked as expected, before I have configured DLX/DLQ for it. Here is the code that describes how I have configured DLX/DLQ.

        channel = connection.CreateModel();

        channel.ExchangeDeclare(ApplicationSettings.Instance.MqName + "DLX", ExchangeType.Fanout);

        channel.QueueDeclare(queue: ApplicationSettings.Instance.MqName + "DLQ",
            durable: false,
            exclusive: false,
            autoDelete: false,
            arguments: null);

        channel.QueueBind(queue: ApplicationSettings.Instance.MqName + "DLQ",
            exchange: ApplicationSettings.Instance.MqName + "DLX",
            routingKey: "");

        var queueArgs = new Dictionary<string, object>
        {
            { "x-dead-letter-exchange", ApplicationSettings.Instance.MqName + "DLX" }
        };

        channel.QueueDeclare(queue: ApplicationSettings.Instance.MqName, 
            durable: false, 
            exclusive: false, 
            autoDelete: false, 
            arguments: queueArgs);

Basically what happens is the sender of a message is successfully publishing the message to queue, but it does not appear in the queue.

Here is the method for publishing the message.

public ResponseBase SendMessage(string message, string callType, string queueName, IConfiguration configuration)

{

    var response = new ResponseBase() { ResponseStatusCode = default};

    ConnectionFactory factory = new ConnectionFactory();
    factory.UserName = configuration["MqLogin"];
    factory.Password = configuration["MqPassword"];
    factory.VirtualHost = "/";
    factory.HostName = configuration["MqLink"];
    factory.Port =int.Parse(configuration["MqPort"]);

    try
    {
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: queueName,
                           durable: false,
                           exclusive: false,
                           autoDelete: false,
                           arguments: null);

            var body = Encoding.UTF8.GetBytes(message);

            Dictionary<string, object> headers = new Dictionary<string, object>();

            headers.Add(configuration["MqHeaderName"], callType);

            IBasicProperties basicProperties = channel.CreateBasicProperties();
            basicProperties.Headers = headers;

            channel.BasicPublish(exchange: "",
                           routingKey: queueName,
                           basicProperties: basicProperties,
                           body: body);
        }

        return response;
    }
    catch (Exception)
    {
         return new ResponseBase() { ResponseStatusCode = 1, ResponseMessage = "Connection lost" };
    }
}

After noticing the issue, I have removed DLX from the queue and it started working as expected again. Continuing further I have noticed that the issue is not only the DLX, making the queue as durable is also blocking the messages. See screenshot below.

Thanks in advance for any help.

Screenshot

0

There are 0 answers