We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Number of messages on a MessageQueue

josgood
josgood asked
on
Medium Priority
745 Views
Last Modified: 2012-06-27
How do I find the number of messages currently on a MessageQueue?
Comment
Watch Question

Top Expert 2006
Commented:
Quoted from, http://community.sgdotnet.org/forums/thread/18308.aspx:

"Unfortunately, the current managed MSMQ APIs (System.Messaging) does not support the MSMQ admin APIs yet (not even in .NET 2.0). So, basically we have three options:

1. Use the performance counters API to get the counters. The following code will do the trick:

  using System.Diagnostics;
  PerformanceCounterCategory myCat = new PerformanceCounterCategory("MSMQ Queue");
  PerformanceCounter cntr = new PerformanceCounter();
  cntr.CategoryName = "MSMQ Queue";
  cntr.CounterName = "Messages in Queue";

  foreach (string inst in myCat.GetInstanceNames())
  {
    cntr.InstanceName = inst;
    Console.Write(inst + " = ");
    Console.WriteLine(cntr.NextValue().ToString());
  }

2. Using COM interop and MSMQ object. The following code will do it, after adding a reference to Microsoft Message Queue 3.0 object library:

  using System.Windows.Forms; // for SystemInformation
  object oMissing = Type.Missing;
  object oMachine = SystemInformation.ComputerName;
  MSMQ.MSMQApplicationClass msmqApp = new MSMQ.MSMQApplicationClass();

  foreach (object oFormat in (object[])msmqApp.ActiveQueues)
  {
    MSMQ.MSMQManagementClass qMgmt = new MSMQ.MSMQManagementClass();
    object oFormatName = oFormat; // oFormat is read only and we need to use ref
    qMgmt.Init(ref oMachine, ref oMissing, ref oFormatName);
    Console.WriteLine(oFormatName.ToString() + " = " + qMgmt.MessageCount.ToString());
  }

3. This method is more reliable than performance counters, but works on MSMQ 3.0 only.

  Calling MQMgmtGetInfo directly to get the active queues (PROPID_MGMT_MSMQ_ACTIVEQUEUES) if needed, and to get the number of messages in each queue (PROPID_MGMT_QUEUE_BYTES_IN_QUEUE). Marshaling the parameters to this call (especially the third one, MQMGMTPROPS structure may be a little tricky, so I was too lazy to write a sample this time."

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2007

Author

Commented:
Thank you very much for your excellent answer!  I am impressed with your completeness -- you clearly deserve your handle.

I''l try answer #3 first and will respond again soon.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.