4

I have a call to get the count of a MSMQ that is failing.

After some research, I found this question: Reading MSMQ message count with ruby

The answer there indicates that if a Queue is empty and Closed that you cannot get "Performance Metrics" (including message count).

So my question now is, how can I programatically "Open" (ie "Un-Close") a MSMQ using .NET and C#?


Update: Incase it is relevant, here is my code to get the message count:

private static int GetMessageCount(string queueName, string machine)
{
    MSMQManagement queue = new MSMQManagement();

    string formatName = @"DIRECT=OS:" + machine + @"\PRIVATE$\" + queueName;
    queue.Init(machine, null, formatName);
    return queue.MessageCount;
}

The error occurs on queue.Init. The error message is: "The queue is not open or may not exist."

This code works just fine on another queue that is setup just the same (but is not empty).

Community
  • 1
  • 1
Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • This SO question has a blog link and an answer that describe the ways I have seen (though not great if many messages could be in the queue) http://stackoverflow.com/questions/2619208/is-there-a-better-way-to-count-the-messages-in-an-message-queue-msmq Also, this surveys some methods http://www.codeproject.com/Articles/346575/Message-Queue-Counting-Comparisions – hatchet - done with SOverflow Jan 03 '13 at 21:34

2 Answers2

5

To get around the "queue is not open" error you can open the queue by using standard msmq calls and do a peek on the message with a small timeout. you have to catch the timeout exception "Timeout for the requested operation has expired." but after the timeout you can query the queue with the MSMQManagement object even though it has 0 messages:

        MSMQ.MSMQApplication q = new MSMQ.MSMQApplication();
        object obj = q.ActiveQueues;
        foreach (object oFormat in (object[])q.ActiveQueues)
        {
            object oMissing = Type.Missing;
            object oMachine = System.Environment.MachineName;
            MSMQ.MSMQManagement qMgmt = new MSMQ.MSMQManagement();
            object oFormatName = oFormat; // oFormat is read only and we need to use ref
            qMgmt.Init(ref oMachine, ref oMissing, ref oFormatName);
            outPlace.Text += string.Format("{0} has {1} messages queued \n", oFormatName.ToString(), qMgmt.MessageCount.ToString());
        }
        foreach (object oFormat in (object[])q.PrivateQueues)
        {
            object oMissing = Type.Missing;
            object oMachine = System.Environment.MachineName;
            MSMQ.MSMQManagement qMgmt = new MSMQ.MSMQManagement();
            queue = new MessageQueue(oFormat.ToString());
            object oFormatName = queue.FormatName; // oFormat is read only and we need to use ref
            TimeSpan timeout=new TimeSpan(2);
           try
           {
                Message msg = queue.Peek(timeout);
            }
            catch
            {// being lazy and catching everything for this example
            }
            qMgmt.Init(ref oMachine, ref oMissing, ref oFormatName);
              outPlace.Text += string.Format("{0}  {1} {2}\n", oFormat.ToString(), queue.FormatName.ToString(), qMgmt.MessageCount.ToString());
        }
    }
lowestvoice
  • 51
  • 1
  • 2
0

An other way to get the number of messages in a queue might be to use the GetAllMessages method of the MessageQueue class. It returns a Message[] which is a static snapshot of all the messages in your queue. After that you can read the Length param to get the number of messages.

Here is the msdn link: http://msdn.microsoft.com/en-gb/library/system.messaging.messagequeue.getallmessages.aspx