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.
