To check how many messages are in a MSMQ Queue

偏执的太偏执、 2022-07-18 00:16 138阅读 0赞

MSMQ用遍历的方式得到消息队列中消息总数在消息总数巨大的时候效率很低下,推荐下面两种方法获取MSMQ队列中消息总数。

方法一:使用MSMQ Management Object,定义在Windows/System32下的mqoa.dll(Microsoft Message Queue 3.0 object library)

  1. MSMQManagement _manager = new MSMQManagement();
  2. _manager.Init("MACHINE", null, @"DIRECT=OS:MACHINE\PRIVATE$\sample");
  3. Console.WriteLine(_manager.MessageCount);
  4. Marshal.ReleaseComObject(_manager);

方法二:使用性能监视器

  1. public int? CountQueue(MessageQueue queue, bool isPrivate)
  2. {
  3. int? Result = null;
  4. try
  5. {
  6. //MSMQ.MSMQManagement mgmt = new MSMQ.MSMQManagement();
  7. var mgmt = new MSMQ.MSMQManagementClass();
  8. try
  9. {
  10. String host = queue.MachineName;
  11. Object hostObject = (Object)host;
  12. String pathName = (isPrivate) ? queue.FormatName : null;
  13. Object pathNameObject = (Object)pathName;
  14. String formatName = (isPrivate) ? null : queue.Path;
  15. Object formatNameObject = (Object)formatName;
  16. mgmt.Init(ref hostObject, ref formatNameObject, ref pathNameObject);
  17. Result = mgmt.MessageCount;
  18. }
  19. finally
  20. {
  21. mgmt = null;
  22. }
  23. }
  24. catch (Exception exc)
  25. {
  26. if (!exc.Message.Equals("Exception from HRESULT: 0xC00E0004", StringComparison.InvariantCultureIgnoreCase))
  27. {
  28. if (log.IsErrorEnabled) { log.Error("Error in CountQueue(). Queue was [" + queue.MachineName + "\\" + queue.QueueName + "]", exc); }
  29. }
  30. Result = null;
  31. }
  32. return Result;
  33. }

参考材料:

MSMQManagement

http://stackoverflow.com/questions/3869022/is-there-a-way-to-check-how-many-messages-are-in-a-msmq-queue

http://stackoverflow.com/questions/8214308/msmqget-the-count-of-messages-from-msmq-queue-in-remote-machine

Direct Format Names

Message Queuing Object Libraries

发表评论

表情:
评论列表 (有 0 条评论,138人围观)

还没有评论,来说两句吧...

相关阅读

    相关 How Many Tables

    Problem Description: 今天是伊格内修斯的生日。他邀请了很多朋友。现在是晚餐时间。伊格内修斯想知道他至少需要多少张桌子。你必须注意,并非所有的朋友都彼此认识