无限超时的Peek MSMQ消息 [英] Peek MSMQ message with unlimited timeout

查看:111
本文介绍了无限超时的Peek MSMQ消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing an application that peeks message from Microsoft Message Queue (MSMQ). I want my application peeks MSMQ messages in turn(from the first message to the last message ). After peeks the last message completly, the main thread will be blocked until MSMQ have a new message arrive.
I've already used MessageQueue.Peek(TimeSpan, Cursor, PeekAction) Method with TimeSpan=MessageQueue.InfiniteTimeout and have a problem: MessageQueue.InfiniteTimeout value approximates 49 days but I want my app will wait for new message with a very very long time (about 1000 days), I've already switched TimeSpan = TimeSpan.MaxValue but have no success. Could you give me some solutions or advices? thanks!
My code like this:

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);
}

推荐答案

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

You can try to catch the specific error, log it and the loop will continue to Peek the next message.

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);
}

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

I don't know you specific use case but waiting so long time for a message on the queue is not usual.

这篇关于无限超时的Peek MSMQ消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆