繁体   English   中英

无限超时的Peek MSMQ消息

[英]Peek MSMQ message with unlimited timeout

我正在编写一个从Microsoft Message Queue(MSMQ)偷窥消息的应用程序。 我希望我的应用程序依次查看MSMQ消息(从第一条消息到最后一条消息)。 完全查看完最后一条消息后,主线程将被阻塞,直到MSMQ有新消息到达为止。
我已经使用了带有TimeSpan = MessageQueue.InfiniteTimeout的 MessageQueue.Peek(TimeSpan,Cursor,PeekAction)方法,并遇到了一个问题:MessageQueue.InfiniteTimeout值大约为49天,但是我希望我的应用将等待很长一段时间的新消息(大约1000天),我已经切换了TimeSpan = TimeSpan.MaxValue,但是没有成功。 您能给我一些解决方案或建议吗? 谢谢!
我的代码是这样的:

 while (true) { if (isTheFirst) { try { ret = mq.Peek(MessageQueue.InfiniteTimeout, cursor, PeekAction.Current); Console.WriteLine(ret.Id); isTheFirst = false; } catch (MessageQueueException e) { // what can we do? } } else { try { // because MessageQueue.InfiniteTimeout value approximate 49 days, so if // during 49 days, Message Queue didn't receive any message, my app will // thrown exception but i want my app to continue wait for new message arrive ret = mq.Peek(MessageQueue.InfiniteTimeout, cursor, PeekAction.Next); Console.WriteLine(ret.Id); } catch (MessageQueueException ex) { // what can we do? } } Thread.Sleep(1000); } 

您可以尝试捕获特定的错误,将其记录下来,循环将继续窥视下一条消息。

while (true) {
  if (isTheFirst) {
    //... omitted for brievety
  } else {
    try {
      ret = mq.Peek(MessageQueue.InfiniteTimeout, cursor, PeekAction.Next);
      Console.WriteLine(ret.Id);
    } catch (MessageQueueException e) {
      if(e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
      {
          Console.WriteLine("Log here that Timeout has occured");
          continue; // will skip Thread.Sleep(1000); and whatever code you put after
      }
      else
      {
         Console.WriteLine("Log exception and rethrow");
         throw;
      }
    }
  }
  Thread.Sleep(1000);
}

我不知道您的特定用例,但通常不会等那么长时间等待队列中的消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM