英文:
How to Set VisibilityTimeout to Indefinite and Manually Trigger Message Reappearance In Azure Storage Queue
问题
收到消息后,是否可能将 visibilityTimeout 设置为无限,并手动触发消息重新出现?
这样做的原因是,我的应用在删除消息之前会处理它,而完成处理的时间可能变化很大。我需要确保不会处理同一条消息两次。
我正在使用 C# 和 .NET Core 进行我的应用程序开发。
英文:
After receiving the message, is it possible to set visibilityTimeout to indefinite and manually trigger the message to reappear?
The reason for this is that my application processes the message before deleting it and the time it will complete may vary greatly. I need to make sure I don't process the same message twice.
I'm using c# and .net core for my application.
答案1
得分: 1
在我们的应用程序中,我们采用了以下方法:
- 接收消息
- 启动后台的
Timer
,延长消息的可见性另外60s
(参见示例) - 在主线程中执行任务
- 如果任务成功完成 - 取消
Timer
并从队列中移除消息 - 如果任务失败,那么我们停止计时器,但不移除消息,以便它重新变为可见状态并再次尝试
- 经过一定数量的重试后 - 移除消息
与Sampath的回答主要区别在于,您无需等待7天或任何其他预定义的时间间隔的结束。它还实现了重试策略。
英文:
In our application, we have used the next approach
- receive the message
- start background
Timer
extending the message visibility for another60s
(see example) - do the job in the main thread
- if the job is done successfully - cancel
Timer
and remove message from the queue - if the job has failed, then we stop the timer, but don't remove the message so it became visible and give it another try
- after a certain number of retries - remove the message
The main difference from Sampath's answer is that you don't need to wait until the end of 7 days or any other predefined time interval. It also implements the retry strategy
答案2
得分: 0
据我所知,时间范围是从
0
到604800
,即 0 秒到 7 天。我已经参考了这个 MSDOC 上的 Azure 队列存储文档以及 git 参考 @SreedharPelluru。
string connectionString = "connectionstring";
QueueClient queueClient = new QueueClient(connectionString, "QueueName");
queueClient.CreateIfNotExists();
queueClient.SendMessage("Hello, World!");
QueueMessage message = queueClient.ReceiveMessage();
queueClient.UpdateMessage(message.MessageId, message.PopReceipt, message.MessageText, TimeSpan.FromDays(7));
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
输出:
注意:
英文:
>AFAIK, The time range is between 0
to 604800
i.e : 0 sec to 7 days. I have referred to this MSDOC for Azure Queue Storage. and git reference @SreedharPelluru.
string connectionString = "connectionstring";
QueueClient queueClient = new QueueClient(connectionString, "QueueName");
queueClient.CreateIfNotExists();
queueClient.SendMessage("Hello, World!");
QueueMessage message = queueClient.ReceiveMessage();
queueClient.UpdateMessage(message.MessageId, message.PopReceipt, message.MessageText, TimeSpan.FromDays(7));
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
Output:
Note:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论