MsmqLogDistributor

Topics: Logging Application Block
May 30, 2007 at 12:43 AM
This object is implemented in Enterprise Library 3.1. I first noticed this in an earlier version and checked to see if it has been revised in 3.1. The IsQueueEmpty() method is used in the loop in order to determine whether a batch of message items are pending. From what I understand, it seems wrong to use Peek in order to generate the Timeout exception to indicate empty queues. Since this potentially means the exception is thrown repeatedly for a queue that can experience no activity, I wanted to find another way. Is there an alternative way to achieve IsQueueEmpty()?

This is the snippet from the Microsoft.Practices.EnterpriseLibrary.Logging.MsmqDistributor.MsmqLogDistributor
		private bool IsQueueEmpty()
		{
			bool empty = false;
			try
			{
				using (MessageQueue msmq = CreateMessageQueue())
				{
					msmq.Peek(new TimeSpan(0));
				}
			}
			catch (MessageQueueException e)
			{
				if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
				{
					empty = true;
				}
			}
 
			return empty;
		}
 
		private void ReceiveQueuedMessages()
		{
			this.isCompleted = false;
			while (!IsQueueEmpty())
			{
			// Remaining lines omitted.