4

Is there any way to check if a public MSMQ is empty? For a private MSMQ it's 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 would I do the same check for a public MSMQ? If I try to check a public MSMQ with the code above it gives me an error on the Peak:

System.ArgumentOutOfRangeException: Length cannot be less than zero.

Justin
  • 17,670
  • 38
  • 132
  • 201
  • I should add, a public MSMQ path would be something like net.msmq://servername/mypublicqueue – Justin Sep 10 '10 at 16:15

4 Answers4

7

I just started working with Message Queues but my coworker has this nice way for checking if a queue is empty:

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 benefit of using this method is that it doesn't throw an exception, and I don't think it requires you to wait for a timeout to occur.

John B
  • 20,062
  • 35
  • 120
  • 170
5

The Peek method is only available on remote machines when you use a direct format name to access the queue. You should be able to use the same code, so long as you're not relying on directory services to get you to the queue.

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

LBushkin
  • 129,300
  • 32
  • 216
  • 265
  • Thanks for the response. I thought every public queue uses directory services?? I changed the path to DIRECT=TCP:myip\mypublicqueue but it gives me: The queue does not exist or you do not have sufficient permissions to perform the operation – Justin Sep 13 '10 at 14:00
  • @Justin: The direct name would be something like: `DIRECT=OS:servername\public$\mypublicqueuename` or `DIRECT:TCP:ipaddress\public$\mypublicqueuename`. You can search MSDN for the specific direct format name to use - it can vary by OS version, and other factors. – LBushkin Sep 13 '10 at 20:39
0

Leo, you sure about that? You can't use a path name with a remote Peek? The error returned doesn't say invalid format name, which would be expected if that was the case. In fact the error appears to be on the "isQueueEmpty = false" line - the try/catch doesn't differentiate between the peek and the isQueueEmpty lines. I bet the isQueueEmpty call is receiving an exception which translates to an negative number. Now your solution, though, may be correct - a lot of remote calls in MSMQ require format names instead of path names. So if you use a format name for creating myQueue, the isQueueEmpty should work.

Cheers

John Breakwell

John Breakwell
  • 4,667
  • 20
  • 25
  • `isQueueEmpty` is a boolean - I doubt it's raising an exception. As for the limitations of `Peek` - I can't find the documentation right now, but I'm pretty sure there's a table or something that identifies when `Peek` is available. – LBushkin Sep 10 '10 at 16:59
  • Ah, of course, I misread the code. No wonder I couldn't find isQueueEmpty in the docs! Apologies. – John Breakwell Sep 11 '10 at 00:59
0

To check if the queue is empty the simplest way is to use the method: GetAllMessages(). If there are zero messages, then the queue is empty.

 string queueName = ".\private$\testqueue";   
 if (MessageQueue.Exists(queueName))
 {   
    MessageQueue mq = new MessageQueue(queueName);
    var allMessages = mq.GetAllMessages();
    if (allMessages.Length > 0)
    {
         //Queue is not empty
    }
    else
    {
         //Queue is empty
    }
 }
 else
 {
      //Queue does not exist
 }
  • Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit](https://stackoverflow.com/posts/65763734/edit) your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Jan 17 '21 at 17:55