是否可以将 GetAllMessages 和 Purge 作为事务处理

发布于 2024-11-04 01:16:01 字数 164 浏览 0 评论 0原文

在开始处理队列中的消息之前,我需要收集所有“旧”消息并处理它们。之后我进入读取/处理循环。

GetAllMessages 将返回消息数组,但是它不会将它们从队列中删除。 Purge 将从队列中删除所有消息。我需要将两者作为交易来完成。这可能吗?

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 array 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2024-11-11 01:16:01

听起来您需要在(重新)启动服务后处理剩余消息。

我实现的解决方案只是使用可配置的接收超时来控制处理循环,因此您的服务不需要担心队列的状态 - 它将处理那里的任何内容。

这是一个简单的 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();
        }
    }
}

我计划将超时延长为动态,因为每次超时异常抛出时都会增加超时,并在处理消息后立即重置它。

使用 MSMQ 编程最佳实践中给出了简短的介绍,尽管它没有不完全支持我提出的解决方案。


进一步挖掘后,我发现了 一个建议 MSMQ 激活的答案,但我自己还没有尝试过。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文