I am setting up a standard standalone thread listening to RabbitMQ in C#. Suppose the method for listening in the thread looks like this:
public void Listen()
{
using (var channel = connection.CreateModel())
{
var consumer = SetupQueues(channel);
while (true)
{
var ea = consumer.Queue.Dequeue(); // blocking call
handler.HandleMessage(channel, ea);
}
}
}
What is an elegant way of halting consumption of messages gracefully in the C# client for RabbitMQ? Keep in mind I have found nothing of use in the RabbitMQ examples/docs or these SO questions:
- How to stop consuming message from selective queue - RabbitMQ
- How to pause and resume consumption gracefully in rabbitmq, pika python
- What is the best way to safely end a java application with running RabbitMQ consumers
The issue here is consumer.Queue.Dequeue()
is a blocking call. I have tried these options:
Calling
channel.BasicCancel(string tag)
. This causes aSystem.IO.EndOfStreamException
in the blocking call. I do not want to use this exception as part of the control flow for obvious reasons.Calling
consumer.Queue.Dequeue(int millisecondsTimeout, out T result)
and checking a flag in between loop iterations. This can work but seems hacky.
I want to let the thread exit gracefully and clean up any unmanaged resources I might have, so no thread aborting, etc.
Any help is appreciated. Thanks