Service not receiving messages after Message Queuing service restarted

后端 未结 1 1514
北荒
北荒 2021-01-04 21:40

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

相关标签:
1条回答
  • 2021-01-04 22:08

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题