如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

huangapple go评论95阅读模式
英文:

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

在我们的应用程序中,我们采用了以下方法:

  1. 接收消息
  2. 启动后台的 Timer,延长消息的可见性另外 60s(参见示例
  3. 在主线程中执行任务
  4. 如果任务成功完成 - 取消 Timer 并从队列中移除消息
  5. 如果任务失败,那么我们停止计时器,但不移除消息,以便它重新变为可见状态并再次尝试
  6. 经过一定数量的重试后 - 移除消息

与Sampath的回答主要区别在于,您无需等待7天或任何其他预定义的时间间隔的结束。它还实现了重试策略。

英文:

In our application, we have used the next approach

  1. receive the message
  2. start background Timer extending the message visibility for another 60s (see example)
  3. do the job in the main thread
  4. if the job is done successfully - cancel Timer and remove message from the queue
  5. 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
  6. 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

据我所知,时间范围是从 0604800,即 0 秒到 7 天。我已经参考了这个 MSDOC 上的 Azure 队列存储文档以及 git 参考 @SreedharPelluru

如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

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

输出:

如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

注意:

  • 在 Azure 门户中,可以设置 消息 永不 过期
英文:

>AFAIK, The time range is between 0 to 604800i.e : 0 sec to 7 days. I have referred to this MSDOC for Azure Queue Storage. and git reference @SreedharPelluru.

如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

     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:

如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

如何将VisibilityTimeout设置为无限,并手动触发Azure存储队列中消息的重新出现

Note:

huangapple
  • 本文由 发表于 2023年5月25日 08:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定