Send 调用完成后出现故障/异常,Azure 服务总线是否会继续重试?

发布于 2025-01-14 23:36:03 字数 1183 浏览 2 评论 0原文

下面是批量发送消息的代码,下面的代码块是创建服务总线客户端时重试策略的设置。 在测试涉及瞬时网络故障的场景时,我们看到日志显示发送任务出现故障,但在服务总线中看到了分配给该批次消息的消息 ID 的消息。是否有可能即使进入故障后,重试仍然在后台发生?据我了解,只有在SDK完成重试过程后才会发生该故障。这是正确的吗?

辅助查询 - 如何记录内部重试尝试?

//Code for sending. Uses Azure.Messaging.ServiceBus
 await tempSender.SendMessagesAsync(batch).ContinueWith(async (tStat) =>
                    {
                        if (tStat.IsFaulted || tStat.IsCanceled)
                        {
                            _logger.Log($"Service Bus Send Task Faulted for batch {batchId}", EventLevel.Error);
                        }
                        else
                        {
                            _logger.Log($"Sent batch of size {batch.Count}", EventLevel.LogAlways);
                            onCompletionCallBack(messages);

                        }
                    });
//Retry policy
protected ServiceBusRetryOptions GetRetryPolicy() => new ServiceBusRetryOptions()
        {
            Delay = TimeSpan.FromSeconds(0.1),
            MaxDelay = TimeSpan.FromSeconds(30),
            MaxRetries = 5,
            Mode = ServiceBusRetryMode.Exponential,
        };

The following is the code for sending batched messages and the code block following is the setting for retry policy while creating the service bus client.
While testing a scenario involving transient network fault, we see the log that the send task was faulted but the messages were seen in the service bus for the message-id that were assigned to the messages of the batch. Is it possible that the retry kept happening in the background even after entering the fault? I am of the understanding that the fault will happen only AFTER the retry procedures are completed by the SDK. Is that correct?

Secondary query - How do I log the internal retry attempts?

//Code for sending. Uses Azure.Messaging.ServiceBus
 await tempSender.SendMessagesAsync(batch).ContinueWith(async (tStat) =>
                    {
                        if (tStat.IsFaulted || tStat.IsCanceled)
                        {
                            _logger.Log(
quot;Service Bus Send Task Faulted for batch {batchId}", EventLevel.Error);
                        }
                        else
                        {
                            _logger.Log(
quot;Sent batch of size {batch.Count}", EventLevel.LogAlways);
                            onCompletionCallBack(messages);

                        }
                    });
//Retry policy
protected ServiceBusRetryOptions GetRetryPolicy() => new ServiceBusRetryOptions()
        {
            Delay = TimeSpan.FromSeconds(0.1),
            MaxDelay = TimeSpan.FromSeconds(30),
            MaxRetries = 5,
            Mode = ServiceBusRetryMode.Exponential,
        };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2025-01-21 23:36:03

您的代码可以简化为以下内容:

try
{
  await tempSender.SendMessagesAsync(batch);

  _logger.Log($"Sent batch of size {batch.Count}", EventLevel.LogAlways);

  onCompletionCallBack(messages);
}
catch (Exception exception)
{
  _logger.Log($"Service Bus Send Task Faulted for batch {batchId}", EventLevel.Error);
}

在所有重试都用尽之前,Service Bus SDK 不会引发异常。有可能消息已发送,但尚未收到确认,这将解释您所观察到的情况。

Your code could be simplified to the following:

try
{
  await tempSender.SendMessagesAsync(batch);

  _logger.Log(
quot;Sent batch of size {batch.Count}", EventLevel.LogAlways);

  onCompletionCallBack(messages);
}
catch (Exception exception)
{
  _logger.Log(
quot;Service Bus Send Task Faulted for batch {batchId}", EventLevel.Error);
}

Service Bus SDK will not throw an exception until all retries are exhausted. There's a chance that the messages have been dispatched, but the acknowledgement hasn't been received, which will explain what you're observing.

﹉夏雨初晴づ 2025-01-21 23:36:03

辅助查询 - 如何记录内部重试尝试?

瞬时失败和重试尝试不会直接显示在您的应用程序中。要观察它们,您需要 启用 SDK 日志记录

Secondary query - How do I log the internal retry attempts?

Transient failures and retry attempts are not directly surfaced to your application. To observe them, you would need to enable SDK logging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文