Azure队列在DeleteMessage上抛出异常
我正在开发一个基于 Azure 的项目进行一些研究,并且在从 CloudQueue 实例删除消息时遇到了一些问题。代码相当简单,所以我有点困惑为什么当我尝试从队列中删除消息时会抛出异常。
这是为队列生成数据的代码:
foreach (var cell in scheme(cells))
{
string id = Guid.NewGuid().ToString();
var blob = sweepItemContainer.GetBlobReference(id);
using (BlobStream stream = blob.OpenWrite())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, cell);
}
sweepItemQueue.AddMessage(new CloudQueueMessage(id), new TimeSpan(1, 0, 0));
}
这是使用队列中的数据的代码:
var msgs = sweepItemsQueue.GetMessages(MsgAmt);
foreach (var msg in msgs)
{
_handleMessage(msg, sweepItemsContainer);
sweepItemsQueue.DeleteMessage(msg);
mergeItemsQueue.AddMessage(new CloudQueueMessage(msg.AsString), new TimeSpan(1, 0, 0));
}
我不明白消息为什么不能存在于队列中。除了其他消费者之外,没有其他任何东西会改变队列。但我确信他们无法得到相同的消息(只要时间跨度没有用完),那么这是怎么发生的呢?
I'm working on an Azure based project for some research and have been running into some issues when deleting messages from a CloudQueue instance. The code is fairly straightforward, so I'm a bit baffled as to why an exception is being thrown when I try to delete a message from the queue.
Here is the code that produces data for the queue:
foreach (var cell in scheme(cells))
{
string id = Guid.NewGuid().ToString();
var blob = sweepItemContainer.GetBlobReference(id);
using (BlobStream stream = blob.OpenWrite())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, cell);
}
sweepItemQueue.AddMessage(new CloudQueueMessage(id), new TimeSpan(1, 0, 0));
}
Here is the code that consumes the data from the queue:
var msgs = sweepItemsQueue.GetMessages(MsgAmt);
foreach (var msg in msgs)
{
_handleMessage(msg, sweepItemsContainer);
sweepItemsQueue.DeleteMessage(msg);
mergeItemsQueue.AddMessage(new CloudQueueMessage(msg.AsString), new TimeSpan(1, 0, 0));
}
I don't see how the message cannot exist in the queue. Nothing else is mutating the queue besides other consumers. But I am assured that they cannot get the same message (so long as the timespan doesn't run out), so how is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要担心两个超时:消息在队列中存在的时间(您在
.AddMessage()
调用中指定的时间)以及调用时设置的可见性超时.GetMessages()
(默认情况下为 30 秒,有一个 重载,允许您指定超时)。当您调用.GetMessages()
时,所有返回的消息在“visibilityTimeout”期间对其他消费者都是不可见的。一旦此period 完成后,您尚未删除的所有消息对所有其他使用者都可见。要检查这是否是问题,我将尝试使用
.GetMessages()
的重载,其最大可见性超时为2 小时。如果这是问题所在,您可以将此值微调到更合理的数字。另一种选择是一次只检索一条消息。There are two timeouts that you need to worry about, how long the message lives in the queue (which you've specified in the your
.AddMessage()
call and the visibility timeout that is set when you call.GetMessages()
(by default this is 30 seconds, there is an overload that allows you to specify the timeout). When you call.GetMessages()
all of the messages returned are invisible to other consumers for the period 'visibilityTimeout'. Once this period finishes all of the messages you haven't already deleted become visible to all other consumers.To check if this is the problem I would try using the overload of
.GetMessages()
with it's maximum visibility timeout of 2 hours. If this is the problem you can fine tune this value down to a more sensible number. Another option would be to just retrieve one message at a time.Steve Marx 的另一个回答,基本上是查看存储异常并继续。我也在其他框架中看到过这一点:
史蒂夫·马克思博客文章
Another answer from Steve Marx, basically look at the storage exception and move on. I have seen this in other frameworks too.:
Steve Marx blog post