Detecting a queue in RabbitMQ

I am using 2 queues per channel. I am declaring 2 queues (Name1 and Name2):

channel.QueueDeclare(queue: "Name1",
    durable: false,
    exclusive: false,
    autoDelete: false,
    arguments: null);

channel.QueueDeclare(queue: "Name2",
    durable: false,
    exclusive: false,
    autoDelete: false,

var consumer = new EventingBasicConsumer(channel);                                         arguments: null);
consumer.Received += (model, ea) =>
    {    
        var body = ea.Body;
        var message = Encoding.UTF8.GetString(body);
        Console.WriteLine(message);
    }

channel.BasicConsume(queue: "Name2",
    noAck: true,
    consumer: consumer);

channel.BasicConsume(queue: "Name1",
    noAck: true,
    consumer: consumer);

      

How do you determine which queue received the message: Name1 or Name2?

+3


source to share


1 answer


In the code below, the ea parameter should have your answer.

consumer.Received += (model, ea) =>
{ 
     string pQueueName = ea.RoutingKey;   
}

      



It is the BasicDeliverEventArgs class in the RabbitMQ.Client.Events namespace that has a member variable called RoutingKey that provides information about the queue name. Also note that the routing key is used when the message was originally posted.

Option 2: It might also be easier to have different Models and Consumers per queue, making it easier to keep track of the queue it is being processed on.

+1


source







All Articles