查看来自 Rabbit MQ 站点的消费者示例代码...
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;
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();
}
最初看起来消息是按顺序 1 接 1 从队列中出来的,并由 Received 部分中的代码连续处理。
但是我现在看到的结果表明它们可能按顺序 1 接 1 地关闭,但是同时处理,这是正确的吗?
问候
汤姆
最佳答案
使用 RabbitMQ 可以控制应通过调用并发处理的消息数:
channel.BasicQos(0, <MaxConcurrentConsumerThreads>, false);
因此,如果时间顺序很重要,请调用 channel.BasicQos(0, 1, false);
确保一次只处理 1 条消息。
关于c# - RabbitMQ 事件处理基本消费者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411153/