I have 20 queues in rabbitmq instance. If I create IModel per queue consumer like this:
for(int i = 0; i < 20; i++)
{
var model = _connection.CreateModel();
var cns = new EventingBasicConsumer(model);
ch.BasicConsume("queue_" + i, false, cns);
...
}
Application uses 37 threads.
If I reuse the same IModel for all consumers:
var model = _connection.CreateModel();
for(int i = 0; i < 20; i++)
{
var cns = new EventingBasicConsumer(model);
ch.BasicConsume("queue_" + i, false, cns);
...
}
Application uses 18 threads.
Is it a safe and good approach to decrease the number of application threads?
I count the number of threads using Process.GetCurrentProcess().Threads.Count.
I checked threads in
dottrace: in the 1st case there are 20 threads created by
RabbitMQ.Client with name like
WorkPool-Session#1:Connection(guid-here),amqp://1.1.1.1:5672 , in the 2nd case - only one such thread.