How to check for open MSMQ

Is there any way to check if open MSMQ is empty? For private MSMQ, this is easy:

private bool IsQueueEmpty(string path) { bool isQueueEmpty = false; var myQueue = new MessageQueue(path); try { myQueue.Peek(new TimeSpan(0)); isQueueEmpty = false; } catch (MessageQueueException e) { if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) { isQueueEmpty = true; } } return isQueueEmpty; } 

How do I do the same validation for public MSMQ? If I try to check public MSMQ with the code above, this will give me an error at its peak:

System.ArgumentOutOfRangeException: The length cannot be less than zero.

+4
source share
3 answers

The Peek method is only available on remote computers when you use the direct format name to access the queue. You should be able to use the same code if you do not rely on directory services to get into the queue.

Direct queue names usually look something like this: DIRECT=URLAddressSpecification/QueueName

+5
source

I just started working with message queues, but my colleague has this nice way to check if there are any queues:

 if (MessageQueue.Exists(fullQueuePath)) { // FYI, GetMessageQueue() is a helper method we use to consolidate the code using (var messageQueue = GetMessageQueue(fullQueuePath)) { var queueEnum = messageQueue.GetMessageEnumerator2(); if (queueEnum.MoveNext()) { // Queue not empty } else { // Queue empty } } } 

The advantage of using this method is that it does not throw an exception, and I do not think it requires you to wait for a timeout to occur.

+7
source

Leo, are you sure about that? You can not use the path name with remote Peek? When returning an error, an invalid format name was not specified, which would be expected if this were the case. In fact, this error appears in the line "isQueueEmpty = false" - try / catch does not distinguish between the peek and isQueueEmpty lines. I am sure that the call to isQueueEmpty receives an exception that translates to a negative number. Now your decision may be right - many remote calls in MSMQ require a format name instead of path names. Therefore, if you use the format name to create myQueue, isQueueEmpty should work.

Greetings

John Broadwell

0
source

All Articles