AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=2086&pId=-1
Get MSMQ Queue Counts in C#
page
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 66033/ 48

I’m working with NServiceBus and MSMQ for one of my projects, and I wanted to be able to show a simple dashboard with the numbers of messages in each of the relevant queues for the application.  Unfortunately, there isn’t a simple ".Count()” method or property in the built-in System.Messaging namespace for MSMQ queues, so if you want to get the message count there are a few ways to go about it.  You can use COM interop, but just as this blog’s author, I didn’t want to take that dependency.  In the end, the result I came up with is from that post’s comments, which is to specify a MessagePropertyFilter on the Queue, and then when you call GetAllMessages() it will use this filter and will avoid pulling back the full message body contents as well as avoid removing the messages from the queue.  Here’s my simple function for fetching the count for a given queue:

protected int GetMessageCount(MessageQueue q)
{
    var filter = new MessagePropertyFilter()
                     {
                         AdministrationQueue = false,
                         ArrivedTime = false,
                         CorrelationId = false,
                         Priority = false,
                         ResponseQueue = false,
                         SentTime = false,
                         Body = false,
                         Label = false,
                         Id = false
                     };
    q.MessageReadPropertyFilter = filter;
    return q.GetAllMessages().Length;
}

I created a simple MVC Controller to display the counts, with an action like this one:

public ActionResult Index()
{
    string machine = Environment.MachineName;
    string[] queues = new []{machine + @"\private$\queue1", 
        machine + @"\private$\queue2", 
        machine + @"\private$\queue3"};
 
    Dictionary<string, int> qcounts = new Dictionary<string, int>();
 
 
    foreach (var queue in queues)
    {
        var messageQueue = new MessageQueue(queue);
        qcounts.Add(queue, GetMessageCount(messageQueue));
    }
 
    return View(qcounts);
}

And just to make things complete, here’s the View:

@model System.Collections.Generic.Dictionary<string,int>
@{
    ViewBag.Title = "Queue Counts";
}
<h2>Queue Counts</h2>
<table>
    <thead>
        <tr>
            <td>Queue</td>
            <td>Message Count</td>
        </tr>
    </thead>
    <tbody>
    @foreach (KeyValuePair<string, int> keyValuePair in Model)
    {
    <tr>
        <td>@keyValuePair.Key</td>
        <td>@keyValuePair.Value</td>
    </tr>
    }
    <tr>
    </tr>
    </tbody>
</table>
With that, you can quickly view the counts of the queues on the local machine.  I’m assuming this will work for remote queues just the same, provided you have the necessary security credentials for the appdomain your web app is running under, but I admit I’ve not yet tried that.


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 11:14:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search