Reputation: 129
I want to create single consumer(Generic Listener) for multiple queues.The consumer should listen multiple queues.
Lets see in the example
channel.ExchangeDeclare(exchange: "logs", type: "fanout");
var queueName = "QeueueName.Instance1";
channel.QueueBind(queue: queueName,
exchange: "logs",
routingKey: "");
Console.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
};
I want to associate the consumer with dynamic no of queues and they will increase time to time so how i will associate consumer to future created queues.I have created a window service for the same so do i have to loop all the queues and associate with consumer and for the future created queues I should add them in the consumer queue list.
Upvotes: 2
Views: 6575
Reputation: 1857
When I first read your question I didn't think you could bind one consumer to multiple queues, but I just tried this and it works fine:
ConnectionFactory factory = new ConnectionFactory()
{
VirtualHost = "testHost1",
UserName = "guest",
Password = "guest",
Port = 5672,
};
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.ExchangeDeclare("testExchange1", ExchangeType.Fanout);
channel.QueueDeclare("testQueue1");
channel.QueueDeclare("testQueue2");
channel.QueueBind("testQueue1", "testExchange1", "");
channel.QueueBind("testQueue2", "testExchange1", "");
var consumer1 = new EventingBasicConsumer(channel);
consumer1.Received += Consumer1OnReceived;
channel.BasicConsume("testQueue1", false, consumer1);
channel.BasicConsume("testQueue2", false, consumer1);
Note that your code doesn't include a call to BasicConsume()
. Your consumer won't receive anything without it.
Upvotes: 6