【发布时间】:2010-12-26 11:53:39
【问题描述】:
我目前遇到了丢失消息的问题。此错误很少发生,但经常发生足以令人讨厌。以下是问题的背景:
- 我已打开 goldmine_service_queue 上的消息日志,这是 Windows 2003 服务器上的一个 MSMQ。
- 我可以证明该消息已插入到 goldmine_service_queue,因为该消息出现在消息日志中。此信息提供有关消息何时消失的时间信息。
- 日志记录函数使用http://logging.apache.org/log4net/index.html
- 日志不显示错误。
- 工作函数(如下所示)在 Windows 服务的线程中执行。它负责从队列中查看消息(工作项)并进行处理。
- 从日志中,我强烈怀疑我的问题可能与 MessageQueue.Peek 和超时行为有关。
超时和消息接收是否可能同时发生?是否有更好的方法来处理服务停止检查以帮助避免此错误?
private void workerFunction()
{
logger.Info("Connecting to queue: " + Settings.Default.goldmine_service_queue);
MessageQueue q = new MessageQueue(Settings.Default.goldmine_service_queue);
q.Formatter = new ActiveXMessageFormatter();
while (serviceStarted)
{
Message currentMessage = null;
try
{
currentMessage = q.Peek(new TimeSpan(0,0,30));
}
catch (System.Messaging.MessageQueueException mqEx)
{
if (mqEx.ToString().Contains("Timeout for the requested operation has expired"))
{
logger.Info("Check for service stop request");
}
else
{
logger.Error("Exception while peeking into MSMQ: " + mqEx.ToString());
}
}
catch (Exception e)
{
logger.Error("Exception while peeking into MSMQ: " + e.ToString());
}
if (currentMessage != null)
{
logger.Info(currentMessage.Body.ToString());
try
{
ProcessMessage(currentMessage);
}
catch (Exception processMessageException)
{
logger.Error("Error in process message: " + processMessageException.ToString());
}
//Remove message from queue.
logger.Info("Message removed from queue.");
q.Receive();
//logPerformance(ref transCount, ref startTime);
}
}//end while
Thread.CurrentThread.Abort();
}
【问题讨论】:
标签: c# windows-services msmq