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

Time:11-26

Is there a way using C# to identify whether a private MSMQ has exceeded it's storage limit (KB)?

In the following example I created a private MSMQ using the Computer Management console and I set the storage limit to 100 KB.

enter image description here

I send messages to the queue using a simple c# program which works fine. I would like to be able to figure out when the limit has been reached in order to stop sending messages.

MessageQueue msgQ =new MessageQueue(".\\Private$\\name_of_queue");
msgQ.Send(msg);

CodePudding user response:

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:

Query Description
Bytes in Queue Shows 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 Queue Shows the total number of bytes that reside in the journal queue. For the computer queue instance, this represents the computer journal queue.

The above queries were found on MSDN in a now deprecated section of MSMQ Queue Object. However, I believe the queries are still valid.

  • Related