如何从消息队列中删除消息(仅当其格式良好时)?
我想从一个队列中获取消息并将其发送到数据库。我只想在特定格式下才这样做。
如果我直接使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与我每次手动将一条消息出队时所采用的方法相同,并且我没有遇到任何问题。
您似乎没有处理的一件事是如何处理队列上不具有所需格式的消息。您打算将其留在队列中吗?如果是这样,您最终可能会得到一个非常大的队列,并且在查看队列中尚未预期的消息时会遇到各种问题。如果无法删除这些消息,则将那些不具有所需格式的消息也出队并将它们存储在其他地方似乎更有意义。
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.
“如果我直接使用 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