why be normal?
Welcome to Sign in | Help
in Search

Counting queue items in MSMQ

Last post 04-01-2009, 8:22 by Anonymous. 1 replies.
Sort Posts: Previous Next
  •  06-03-2005, 11:02 1034

    Counting queue items in MSMQ

    Don't you just <tone type="sarcasm">love</tone> it when the .NET Framework fails to provide some functionality that you need in order to accomplish a simple task?

    In the project I'm working on currently, I am looking to implement MSMQ (Microsoft Message Queuing), touted by many to be one of the most underrated but useful technologies, as a reliable back-logging mechanism for items that cannot be written to database should the network connection fail.

    The .NET Framework has a wonderful System.Messaging namespace with the classes required to send/receive messages to/from queues, but the problem is there is no facility for me to even check if a private queue has items in the first place, much like how the MSMQ MMC console represents. Woah. Talk about over turning your car because of a small stone.

    It is at such a time where one has to painfully revert to the APIs of old and muck around in unfamiliar WIN32 or COM territory. Having become a professional developer only with .NET, these situations become frustratingly time-consuming activities for me - I've been there more times than I like. Thanks be to God, we still have our world of legacy (ok, maybe that's a harsh word) developers who are willing to share these information.

    I (or Google, actually) managed to hit almost on the dot with the blog of this MSMQ guru, Yoel. Three options he gives, and I decide to take the second idea (with Microsoft Message Queue 3.0 Object Library) since it appears to be more elegant than the first, and actually having some code over the third. Hey, .NET is suppose to make COM InterOp [almost] seamless so it shouldn't be too hard!

    Well it's gonna take more effort on my part if this is going to work as I need it too. First of all I don't even know what the code is really trying to do. If you Google for "MSMQManagementClass" the only 2 results returned are Yoel's own blog pages. Looks like MSMQ is really underrated. I had a tough time trying to figure out what those "o" parameter variables refer to.

    Of course, the truth is one should be searching for "
    MSMQManagement"; the prior is a COM InterOp wrapper name. Once I got some proper Platform SDK reference I trialed and errored with the parameters (since I only wanted to work with a specific queue) until I could reliably grab the queue without provoking any COMException and "generic COM errors".

    Some hours gone by so I can settle down on this code. Just so i can count.

    object machineName = Type.Missing; // Set this to make parameter optional.
    object noFormat = Type.Missing; // Set this to make parameter optional.
    object pathName = @".\private$\QueueName";
    MSMQ.MSMQManagementClass msmqMgmt = new MSMQManagementClass();
    msmqMgmt.Init(ref machineName, ref pathName, ref noFormat);

    MessageBox.Show(msmqMgmt.MessageCount.ToString() + " items");

    Filed under: ,
  •  04-01-2009, 8:22 5566 in reply to 1034

    Re: Counting queue items in MSMQ

    Didn't work for me.

    I got a 'General Exception', which isn't exactly helpful.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems