如何从消息队列中删除消息(仅当其格式良好时)?

发布于 2024-10-19 02:33:02 字数 1358 浏览 4 评论 0原文

我想从一个队列中获取消息并将其发送到数据库。我只想在特定格式下才这样做。

如果我直接使用 Receive 方法,并且在访问消息的 Body 时发生一些异常,那么由于 Receive 方法,我会丢失消息MessageQueue 从队列中删除消息。

为了避免消息丢失,现在我首先Peek消息,如果其格式正确,我使用Receive方法将其从队列中删除以将其发送到数据库。

我写的代码是这样的:

 Message msg = _queue.Peek(new TimeSpan(0, 0, LoggingService.Configuration.ReceiveTimeout));

// LogMessage is my own class which is adding some more stuff to original message from MessageQueue                
LogMessage message = null;

                if (msg != null)
                {
                    if (!(msg.Formatter is BinaryMessageFormatter))
                        msg.Formatter = new BinaryMessageFormatter();

                    message = LogMessage.GetLogMessageFromFormattedString((string) msg.Body);

                    // Use Receive method to remove the message from queue. This line will we executed only if the above line does not
                    // throw any exception i.e. if msg.Body does not have any problem
                    Message wellFormattedMsg =
                         _queue.ReceiveById(msg.Id);

                      SendMessageToDatabase(message);
                }

先用Peek,再用Receive,这样的逻辑对吗?或者还有其他更好的方法来实现同样的目标吗?请注意,我不想一次收到所有消息。 MessageQueue 是非事务性的。

I want to take message from one queue and send it to database. I want to do it only if it's in specific format.

If i use Receive method directly and some exception occurs while accessing Body of the Message, I lose the message since Receive method of the MessageQueue removes the message from the queue.

To avoid loss of message, now i first Peek the message, and if its well formatted, I use Receive method to remove it from the queue to send it to database.

Code I have written is like this:

 Message msg = _queue.Peek(new TimeSpan(0, 0, LoggingService.Configuration.ReceiveTimeout));

// LogMessage is my own class which is adding some more stuff to original message from MessageQueue                
LogMessage message = null;

                if (msg != null)
                {
                    if (!(msg.Formatter is BinaryMessageFormatter))
                        msg.Formatter = new BinaryMessageFormatter();

                    message = LogMessage.GetLogMessageFromFormattedString((string) msg.Body);

                    // Use Receive method to remove the message from queue. This line will we executed only if the above line does not
                    // throw any exception i.e. if msg.Body does not have any problem
                    Message wellFormattedMsg =
                         _queue.ReceiveById(msg.Id);

                      SendMessageToDatabase(message);
                }

Is this logic right to first using Peek and then Receive? Or is there any other better way f achieving the same thing? Please note that I dont want to get all messages at a time. MessageQueue is non transactional.

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

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

发布评论

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

评论(2

柳絮泡泡 2024-10-26 02:33:02

这与我每次手动将一条消息出队时所采用的方法相同,并且我没有遇到任何问题。

您似乎没有处理的一件事是如何处理队列上不具有所需格式的消息。您打算将其留在队列中吗?如果是这样,您最终可能会得到一个非常大的队列,并且在查看队列中尚未预期的消息时会遇到各种问题。如果无法删除这些消息,则将那些不具有所需格式的消息也出队并将它们存储在其他地方似乎更有意义。

This is the same approach that I take when manually dequeuing message one at a time and I've not come across any issues with it.

The one thing that you do not appear to dealing with is how to handle a message on the queue that does not have the required format. Is your intention to leave it on the queue? If so, you might end up with a very large queue and have all sorts of issues with peeking at messages further up the queue that have not yet been expected. It would appear to make more sense to also de-queue those messages that do not have the required format and store them elsewhere if they cannot be deleted.

鼻尖触碰 2024-10-26 02:33:02

“如果我直接使用 Receive 方法,并且在访问消息正文时发生一些异常,我会丢失消息,因为 MessageQueue 的 Receive 方法会从队列中删除消息。”

您应该使用事务接收,以便在事务中止时消息返回到队列。

干杯
约翰·布瑞克威尔

"If i use Receive method directly and some exception occurs while accessing Body of the Message, I lose the message since Receive method of the MessageQueue removes the message from the queue."

You should be using transactional receives so that the message returns to the queue when/if the transaction aborts.

Cheers
John Breakwell

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