jbablog.com

the personal blog of John BouAntoun


Getting a Clustered MSMQ Queue Length with .Net and COM

So in reference to my previous post, I finally found a way to extract the queue length of an MSMQ Queue behind a clustered server, and it involves, of all things, COM. There doesn’t seem to be any other non-hackish way to get a queue length fast from a clustered server, the links discussed in my previous post work fine for non clustered environments, but this is the solution for a clustered environment.

  • First:
    Add a COM Reference to the Microsoft Message Object 3.0 Type Library (if on Windows XP or Server 2003).If you’re stuck on vista then manually browse to C:\windows\system32\mqoa.dll and add that as a reference.
  • Second:
    Use the following code and pay special attention to the format of the FormatName parameter and the server parameter. It took me a long time of fiddling with string formats to get the correct one and no one else on the lazy web seems to have mentioned the specific format needed.
    int queueCount;
    MSMQ.MSMQManagement mgmt = new MSMQ.MSMQManagement();
    object oMissing = Type.Missing;
    object server = (object) "MSMQServer";
    object formatName = (object) "Direct=OS:MSMQServer\private$\queueName";
    mgmt.Init(ref server, ref oMissing, ref formatName);
    Console.WriteLine("response " + mgmt.MessageCount.ToString() + Environment.NewLine);
    int.TryParse(mgmt.MessageCount.ToString(), out queueCount);

Apologies for the bad vertical spacing, wordpress keeps on insisting on closing my <code> section early if I insert empty newlines in it.

Why do I always have to find the boundary cases, or ‘Reading an MSMQ length form .net’

Watch out, a coding related post.

So I coded up a little MSMQ dumping utility the other day, in part to learn about MSMQ and in part to pick up some domain knowledge around one of the more fragile systems at work. And before you start laughing, yes I know it’s been over 3 years since I wrote a real app, but anyhow I gave it a crack.

Coded the utility up in about four hours, including time to acquire domain knowledge and MSMQ knowledge and ended up with this app:

Wait Queue Dumper

Now this thing works perfectly on the staging machine, but refuses to return the Queue Count on the live server. Enter the MSMQ from the plumber’s mate blog. Full of lots of little handy hints on how to make MSMQ performance counters actually work.

Why can MSMQ installs get so broken, so often, that an MSMQ blogger can have so much material to talk about when fixing it? I don’t know if I should be happy I found a plentiful source for potential solutions, or worried about the many ways a seemingly simple operation can go wrong?