Queue is locking even though it is declared as non-exclusive (RabbitMQ)

Viewed 33

I'm using the .NET client for RabbitMQ to implement the RPC pattern for a client and server. The client and server both declare the queues using the same parameters so that messages sent before the server is up won't be lost and vice versa. However, declaring the queue on the client side throws an OperationInterruptedException:

operation queue.declare caused a channel exception resource_locked: cannot obtain exclusive access to locked queue '151' in vhost '/'. It could be originally declared on another connection or the exclusive property value does not match that of the original declaration.

This question is similar to RabbitMQ: ACCESS_REFUSED even if the queue is non-exclusive, but the answer there did not work for me. The BasicConsumer defaults to being non-exclusive in the .NET client. If I declare the queue only in the server, the code works without issues. I don't want to have to make sure the server starts before the client, though.

What is causing this exception? Neither queue is declared as exclusive.

(Both code samples have been compressed for brevity.)

Client:

public MyClient(){
        try {
            ConnectionFactory factory = new() {
                HostName = "localhost"
            };
            IConnection connection = factory.CreateConnection();
            channel = connection.CreateModel();
            sendUpdatesMessenger = new("sendUpdates", channel);
            getUpdatesMessenger = new("getUpdates", channel);
            getUpdatesReceiver = new("getUpdates", "localhost", channel);
            sendUpdatesReceiver = new("sendUpdates", "localhost", channel);
            string bodyStr = "test example string";
            string messageId = new Guid().ToString();
            bool success = getUpdatesMessenger.SendMessage(messageBody: bodyStr, routingKey: "151", correlationId: messageId, replyQueue: "myReplyQueue");
            //Do stuff based on success or failure
        } catch (Exception ex){
            Debug.WriteLine("Exception " + ex);
        }

}


public bool SendMessage(string messageBody, string routingKey, string correlationId, string replyQueue = ""){
        try{
            IBasicProperties props = channel.CreateBasicProperties();
            props.CorrelationId = correlationId;
            props.ReplyTo = replyQueue;
            channel.QueueDeclare(queue: routingKey, durable: true, exclusive: false, autoDelete: false);
            channel.BasicPublish(exchange: exchange, routingKey: routingKey, basicProperties: props, body: Encoding.ASCII.GetBytes(messageBody));
            return true;
        }
        catch(Exception ex){
            Debug.WriteLine("Error: " + ex);
            return false;
        }
    }

Server:

public bool BindQueue(string routingKey){
        try {
            channel.QueueDeclare(queue: routingKey, durable: true, autoDelete: false);
            channel.QueueBind(queue: routingKey, routingKey: routingKey, exchange: exchangeName);
            queueNames.Add(routingKey);
            return true;
        }
        catch (Exception ex){
            Console.WriteLine(ex);
            return false;
        }
    }

0 Answers
Related