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

2023-09-07 10:02:09 作者:说什么也徒劳。

我想利用消息从一个队列,并传送给数据库。我想这样做,只有当它在特定的格式。

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.

如果我使用直接接收方法和在访问车身消息时出现一些异常,我失去了消息因为接收 MessageQueue 从队列中删除的消息。方法

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我写是这样的:

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

这是正确的逻辑首先使用窥视,然后接收?还是有其他更好的办法˚F达到同样的事情?请注意,我不希望得到的所有消息的时间。 MessageQueue是非事务。

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.

推荐答案

这是同样的方法,我走的时候手动出队的消息一次,我还没有遇到任何问题随之而来。

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.