Kirill
Kirill

Reputation: 459

Detect queue in RabbitMQ

I use 2 queues in the channel. I declare 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 can I detect which queue recieved the message: Name1 or Name2 ?

Upvotes: 3

Views: 1702

Answers (1)

eracube
eracube

Reputation: 2639

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

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

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

Option 2: It also might be easier to have different Models and Consumers per queue which makes it easier to track which queue it is handling.

Upvotes: 3

Related Questions