简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM