【发布时间】:2011-04-29 15:25:35
【问题描述】:
在我开始处理队列中的消息之前,我需要收集所有“旧”消息并处理它们。之后我进入我的读取/处理循环。
GetAllMessages 将返回一个消息数组,但是它不会将它们从队列中删除。 Purge 将从队列中删除所有消息。我需要将两者都作为交易进行。这可能吗?
【问题讨论】:
标签: msmq
在我开始处理队列中的消息之前,我需要收集所有“旧”消息并处理它们。之后我进入我的读取/处理循环。
GetAllMessages 将返回一个消息数组,但是它不会将它们从队列中删除。 Purge 将从队列中删除所有消息。我需要将两者都作为交易进行。这可能吗?
【问题讨论】:
标签: msmq
听起来您需要在(重新)启动服务后处理剩余消息。
我实现的一个解决方案只是使用可配置的接收超时来控制处理循环,因此您的服务不需要担心队列的状态 - 它会处理那里的任何内容。
这是一个简单的 C# 示例
...
Message msg;
MessageQueueTransaction currentTransaction = new MessageQueueTransaction();
TimeSpan receiveTimeOut = new TimeSpan(0, 0, 30);
while (MyService.IsRunning)
{
try
{
currentTransaction.Begin();
msg = this.sourceQueue.Receive(receiveTimeOut, currentTransaction);
// process your message here
currentTransaction.Commit();
}
catch(MessageQueueException mqex)
{
switch(mqex.MessageQueueErrorCode)
{
case MessageQueueErrorCode.IOTimeout :
// That's okay ... try again, maybe there's a new message then
break;
default :
// That's not okay ... abort transaction
currentTransaction.Abort();
}
}
}
我计划将超时延长为动态的,因为它会在每次抛出超时异常时增加超时,并在处理完消息后立即重置。
Programming best Practices with MSMQ 中给出了一个简短的介绍,尽管它并不完全支持我提出的解决方案。
再挖掘一下,我发现了an answer proposing MSMQ Activation,我自己还没有尝试过。
【讨论】: