Home > OS >  Rabbit MQ Exception with .NEt core
Rabbit MQ Exception with .NEt core

Time:11-29

The following is the exception i receive .Net core and rabbitMQ : The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=405, text='RESOURCE_LOCKED - cannot obtain exclusive access to locked queue 'demo-queue' in vhost '/'. It could be originally declared on another connection or the exclusive property value does not match that of the original declaration.', classId=50, methodId=10

enter image description here

Producer :

var factory = new ConnectionFactory
        {

            Uri = new Uri("amqp://guest:guest@localhost:5672")

        };

        using var connection= factory.CreateConnection();   
        using var channel=  connection.CreateModel();
        channel.QueueDeclare("demo-queue",durable:true,exclusive:true,autoDelete:false, arguments:null);
        var message = new
        {
            Name = "Producer",
            Message = "Hello!"
        };
        var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));


        channel.BasicPublish("", "demo-queue",null,body);

Consumer:

  var factory = new ConnectionFactory
        {

            Uri = new Uri("amqp://guest:guest@localhost:5672")

        };

        using var connection = factory.CreateConnection();
        using var channel = connection.CreateModel();
        channel.QueueDeclare("demo-queue", durable: true,
            exclusive: true, autoDelete: false,
            arguments: null);
        var consumer = new EventingBasicConsumer(channel);
        consumer.Received  = (sender, e) =>
         {
             var body = e.Body.ToArray();
             var message = Encoding.UTF8.GetString(body);
             Console.WriteLine(message);
         };


        channel.BasicConsume("demo-queue",true,consumer);

Is it the right practice or right way IF I Change the name of the queue that I have given in producer or consumer? which is the right way to handle this?

CodePudding user response:

Exchange and queu need to be connected. If you do not do this, the message cannot know the queue address it will go to. After QueueDeclare, it must be bind with the exchange address. I have implemented it this way. If you want, you can look at the repo I created here. https://github.com/oguzhanKomcu/RabbitMQ_Sample

I think this is the missing function.

public void BindQueu(string exName, string queuName, string routingKey)
     {
             channel.QueueBind(queuName,exName, routingKey);
     }

CodePudding user response:

In the QueueDeclare section, you said properties that are different from the queue you created.

Edit:

All your mistakes are in this line of code. Because you created a queue. Now you want to open it with the wrong properties. I think exclusive: false will solve the problem

//channel.QueueDeclare("demo-queue",durable:true,exclusive:true,autoDelete:false, arguments:null);

  channel.QueueDeclare(queue: "demo-queue",
                       durable: true,
                       exclusive: false,
                       autoDelete: false,
                       arguments: null);
  • Related