2

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.

2
  • Yes, and? What's the problem you're having? RabbitMQ is not a programming language, so you're going to need to show us the code you need help with. Commented Apr 19, 2018 at 17:13
  • Man i have updated my question plz help if u have any idea abt this, its urgent, I m stuck into this. Commented Apr 19, 2018 at 17:34

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.