首页 > 解决方案 > Multiple RabbitMQ Consumers DotNet Core

问题描述

We have an application that needs to have multiple consumers for the same query. The Data is of such nature what the "Competing Consumer" is not a problem as the messages can get processed in any order and independent. My question is, as RabbitMQ does its own Thread Management etc, what is the best approach to create multiple consumers picking messages of the same queue? Looking at this basic example of a simple consumer:

var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
    channel.QueueDeclare(queue: "hello",
                            durable: false,
                            exclusive: false,
                            autoDelete: false,
                            arguments: null);

    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
        var body = ea.Body.ToArray();
        var message = Encoding.UTF8.GetString(body);
        Console.WriteLine(" [x] Received {0}", message);
    };
    channel.BasicConsume(queue: "hello",
                            autoAck: true,
                            consumer: consumer);

    Console.WriteLine(" Press [enter] to exit.");
    Console.ReadLine();
}

I believe multiple instances of channel.BasicConsume(...) needs to be created, but what is the best way to do that? With a TaskScheduler or Multiple Threads or what?

I can introduce service layers and tidy everything up but just need to know what is the safest way to go about it.

All the reaseach I found so far points not not creating multiple consumers but in our case its a requirement.

标签: c#.net-corerabbitmq

解决方案


一种简单的方法是,您可以拥有两个这样的控制台应用程序,然后将它们一起运行。


推荐阅读