We have a service that receives messages from n message queues. However, if the Message Queuing service is restarted, the message retrieval service stops receiving messages
Upon receiving the exception that is the result of the service restarting, you have to release the old MessageQueue
, i.e. unwiring your ReceiveCompleted
event, disposing the MessageQueue
, etc. Then create a new instance of the MessageQueue
and hook up to the ReceiveCompleted
event again on the new MessageQueue
instance.
Alternatively, you can use a polling method that creates a new instance on a certain interval, calls MessageQueue.Receive(TimeSpan)
, will wait for an incoming message or until the timeout occurs. In which case you handle the message and destroy the MessageQueue
instance and start the iteration again.
By recreating the MessageQueue
each time, you ensure a built in recovery. Also, the overhead of creating the MessageQueue
is minimal due to internal caching of the underlying queue.
Pseudo-code...
while (!notDone)// or use a timer or periodic task of some sort...
{
try
{
using (MessageQueue queue = new MessageQueue(queuePath))
{
Message message = queue.Receive(TimeSpan.FromMilliseconds(500));
// process message
}
}
catch (MessageQueueException ex)
{
// handle exceptions
}
}