How to Check How Many Messages Are in a Msmq Queue

Is there a way to check how many messages are in a MSMQ Queue?

You can read the Performance Counter value for the queue directly from .NET:

using System.Diagnostics;

// ...
var queueCounter = new PerformanceCounter(
"MSMQ Queue",
"Messages in Queue",
@"machinename\private$\testqueue2");

Console.WriteLine( "Queue contains {0} messages",
queueCounter.NextValue().ToString());

How to identify if a private message queue (MSMQ) has exceeded its message storage limit using C#?

Maximum Size of Queue

Use the MessageQueue.MaximumQueueSize Property to get the queue's maximum size.

The maximum size, in kilobytes, of the queue. The Message Queuing
default specifies that no limit exists.

So, something like this should work:

var msgQ = new MessageQueue(".\\Private$\\name_of_queue");
long size = msgQ.MaximumQueueSize;

Size of Queue

Use the PerformanceCounter to get the current size of the queue:

var bytesCounter = new PerformanceCounter( 
"MSMQ Queue",
"Bytes in Queue",
Environment.MachineName + "\\private$\\queue-name");

Looks like there are two different queries to get the size of the current queue:



















QueryDescription
Bytes in QueueShows the total number of bytes that currently reside in the queue. For the computer queue instance, this represents the dead letter queue.
Bytes in Journal QueueShows the total number of bytes that reside in the journal queue. For the computer queue instance, this represents the computer journal queue.

Is there a better way to count the messages in an Message Queue (MSMQ)?

In C# the answer appears to be no - what you are doing is one of the only ways to do this and all the others are similar.

There are ways to do this using WMI or COM - have a look at the MSMQManagement com component. This has a MessageCount property.


I found the following post that may give you some other ideas for slightly better pure C # implementations:

Counting Messages in an MSMQ MessageQueue from C#

While the above appears to be all true, I should note that I've never tried to do this with MSMQ - I've only ever done standard reading from the queues.

Count messages in an msmq sub-queue

The error is as follows (from http://support.microsoft.com/kb/304287):
MQ_ERROR_UNSUPPORTED_FORMATNAME_OPERATION (0xC00E0020).
MQMgmtGetInfo won't understand the subqueue format name.

The retry subqueue only really exists as a logical division of private$\file-queue queue.
You can call a count on the file-queue but not subqueues within it.

Cheers
John

Is it possible to determine how many messages are in a POSIX message queue?

You said in comments that you are using msgget() to create the message queue. In that case, you can use msgctl() to get the number of messages in the queue, via the returned msqid_ds::msg_qnum struct field.

Find out which program reads from a given MSMQ queue

For a given service account, set read-message and peek-message permissions on the queue, and then ensure that only the consumer runs under that service account.

UPDATE

On Windows server 2008 or Windows 7 or higher, MSMQ has a dedicated system event log which records everything the MSMQ subsystem does. It may be possible to see what user account is accessing the queue via this.

MSMQ console showing message count but no messages for private queue

As you noted, you can see a message count on an outgoing queue if source journaling is in use. The invisible messages are there in case they need to be moved to the DLQ.

I would expect your problem to be similar - there should be a visible message in the outgoing queue and an invisible message in the destination queue because delivery hasn't completed. I assume a handshaking or storage acknowledgement has been lost along the way. Or maybe the message has been processed and removed from the queue but MSMQ couldn't update the sender of the fact.

Check the outgoing queues on the remote machines sending TO this queue.

MSMQ max count message notification

The only (partially) built-in solution would be to set up the MSMQ Queue performance counter which gives you this information for private queues on the server.

There are a number of other solutions, including a SCOM management pack, and some third party solutions like evtools, or you could roll you own using System.Messaging.

Hope this is of help.



Related Topics



Leave a reply



Submit