I have a RabbitMQ SimpleRpcServer and I want to know how to properly dispose it when I'm done with it. The pattern has a main loop that blocks until it receives a message, handles the messages, and then blocks again. This means that in order to break the loop, I have to send a specially-coded message that the handler can use to break out of the loop.
I've read that RabbitMQ channels should be accessed from the same thread that created them. If this is true (I can't find a source), does this mean that to dispose my SimpleRpcServer, I'll have to create a new channel specifically to send the close message to the main loop?
Here's pseudo code:
Main Loop (actual code here):
//I run this on a ThreadPool thread
foreach (var evt in m_subscription) //blocks until a message appears
{
ProcessRequest(evt);
}
Handler:
private void ProcessRequest(evt)
{
if(evt.BasicProperties.ContentType == "close") //close flag
{
base.Close();
}
else
{
//process message
}
}
Disposal code (Creates a new channel specifically for canceling the main loop):
//I call this from the main thread
public void Dipose()
{
var channel = new Channel();
var props = new Properties("close"); //close flag
channel.BasicPublish(queueName, props);
channel.Dispose();
}
Note that I left out some initialization and disposal code. I know that this code won't compile.