开发者

is it possible to GetAllMessages and Purge as a transaction

Before I start processing the messages in the queue i need to harvest off all the "old" messages and deal with them. After that I get into my read/process loop.

GetAllMessages will return an arr开发者_Python百科ay of messages, howerver it does not remove them from the queue. Purge will remove all messages from the queue. I need to do both as a transaction. Is this possible?


It sounds like you need to deal with processing leftover messages after (re)starting a service.

A solution I implemented simply uses a configurable receive timeout to control the processing loop so your service doesn't need to bother about the state of the queue - it will process whatever is there.

Here's a simple C# sample

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

I planned to extend the timeout to be dynamic in that it would increase the timeout every time the timeout exception throws and reset it as soon as a message is processed.

A short introduction is given in Programming best Practices with MSMQ although it doesn't exactly back my proposed solution.


Digging a little more I found an answer proposing MSMQ Activation which I didn't try out myself yet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜