Its states the following in the RabbitMQ documentatation
"As a rule of thumb, sharing Channel instances between threads is something to be avoided. Applications should prefer using a Channel per thread instead of sharing the same Channel across multiple threads."
Currently we are looking at the prefetch count where it has been recommended that if you have a small number of consumers and autoack=false, then we should consume many messages at once. However we find that the prefetch has not effect if the consumer sends manual acknowledgements back using a single thread of execution. However if we wrap the consumer processing in a Task, we find that the prefetch count does matter and substantially improves consumer performance.
See the following example where we are wrapping the consumption of the message by the consumer in a Task object:
class Program
{
public static void Main()
{
var factory = new ConnectionFactory()
{
HostName = "172.20.20.13",
UserName = "billy",
Password = "guest",
Port = 5671,
VirtualHost = "/",
Ssl = new SslOption
{
Enabled = true,
ServerName = "rabbit.blah.com",
Version = System.Security.Authentication.SslProtocols.Tls12
}
};
var connection = factory.CreateConnection();
var channel = connection.CreateModel();
channel.BasicQos(0, 100, false);
channel.ExchangeDeclare(exchange: "logs", type: "fanout");
var queueName = channel.QueueDeclare().QueueName;
Console.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var _result = new Task(() => {
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
System.Threading.Thread.Sleep(80);
channel.BasicAck(ea.DeliveryTag, false);
});
_result.Start();
};
channel.BasicConsume(queue: "test.queue.1", autoAck: false, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
The question I have is how do people implement consumers with the .NET rabbitmq client that avail of prefetch counts?, Do you have to manual ack using a Task of some sort?, is it safe