您的位置:首页 > 产品设计 > UI/UE

Get MSMQ Queue Counts in C#

2012-01-06 18:29 495 查看
http://aspalliance.com/2086_Get_MSMQ_Queue_Counts_in_C

I’mworkingwithNServiceBusandMSMQforoneofmyprojects,andIwantedtobeabletoshowasimpledashboardwiththenumbersofmessagesineachoftherelevantqueuesfortheapplication.Unfortunately,thereisn’tasimple".Count()”methodorproperty
inthebuilt-inSystem.MessagingnamespaceforMSMQqueues,soifyouwanttogetthemessagecountthereareafewwaystogoaboutit.YoucanuseCOMinterop,butjustas

thisblog’sauthor,Ididn’twanttotakethatdependency.Intheend,theresultIcameupwithisfromthatpost’scomments,whichistospecifyaMessagePropertyFilterontheQueue,andthenwhenyoucallGetAllMessages()itwillusethisfilterand
willavoidpullingbackthefullmessagebodycontentsaswellasavoidremovingthemessagesfromthequeue.Here’smysimplefunctionforfetchingthecountforagivenqueue:

protectedintGetMessageCount(MessageQueueq)


{


varfilter=newMessagePropertyFilter()


{


AdministrationQueue=false,


ArrivedTime=false,


CorrelationId=false,


Priority=false,


ResponseQueue=false,


SentTime=false,


Body=false,


Label=false,


Id=false


};


q.MessageReadPropertyFilter=filter;


returnq.GetAllMessages().Length;


}


IcreatedasimpleMVCControllertodisplaythecounts,withanactionlikethisone:

publicActionResultIndex()


{


stringmachine=Environment.MachineName;


string[]queues=new[]{machine+@"\private$\queue1",


machine+@"\private$\queue2",


machine+@"\private$\queue3"};




Dictionary<string,int>qcounts=newDictionary<string,int>();






foreach(varqueueinqueues)


{


varmessageQueue=newMessageQueue(queue);


qcounts.Add(queue,GetMessageCount(messageQueue));


}




returnView(qcounts);


}


Andjusttomakethingscomplete,here’stheView:

@modelSystem.Collections.Generic.Dictionary<string,int>


@{


ViewBag.Title="QueueCounts";


}


<h2>QueueCounts</h2>


<table>


<thead>


<tr>


<td>Queue</td>


<td>MessageCount</td>


</tr>


</thead>


<tbody>


@foreach(KeyValuePair<string,int>keyValuePairinModel)


{


<tr>


<td>@keyValuePair.Key</td>


<td>@keyValuePair.Value</td>


</tr>


}


<tr>


</tr>


</tbody>


</table>


Withthat,youcanquicklyviewthecountsofthequeuesonthelocalmachine.I’massumingthiswillworkforremotequeuesjustthesame,providedyouhavethenecessarysecuritycredentialsfortheappdomainyourwebappisrunningunder,butIadmitI’ve
notyettriedthat.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: