【问题标题】:Azure storage queue - retry mechanism implementationAzure 存储队列 - 重试机制实现
【发布时间】:2023-03-09 16:55:01
【问题描述】:

我正在使用 nuget 包 "Microsoft.Azure.Storage.Queue" Version="11.1.7" 创建 Azure 存储队列客户端,如下所示,

AsyncLazy<CloudQueue> qClient = new AsyncLazy<CloudQueue>( async () =>
    {
        var myStorageAccount = CloudStorageAccount.Parse("ConnectionString");
        var myQueue = myStorageAccount .CreateCloudQueueClient()
            .GetQueueReference("QueueName");
        await myQueue.CreateIfNotExistsAsync();
        return myQueue;
    });

希望在通过上面的“qClient”实例向队列发布消息时加入重试机制来克服任何暂时性故障。

如何在上述创建延迟队列连接的方式中加入重试机制?

【问题讨论】:

    标签: azure-storage lazy-loading lazy-initialization azure-storage-queues retrypolicy


    【解决方案1】:

    可以参考这个official document,也可以使用CloudBlobClient.DefaultRequestOptions属性

    我写了一个代码示例,也许它可以启发你:

                AsyncLazy<CloudQueue> qClient = new AsyncLazy<CloudQueue>(async () =>
                {
                    var myStorageAccount = CloudStorageAccount.Parse("ConnectionString");
                    var myQueue = myStorageAccount.CreateCloudQueueClient();
                    myQueue.DefaultRequestOptions = new QueueRequestOptions
                    {
                        RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), 4),
                        // For Read-access geo-redundant storage, use PrimaryThenSecondary.
                        // Otherwise set this to PrimaryOnly.
                        LocationMode = LocationMode.PrimaryThenSecondary,
                        // Maximum execution time based on the business use case.
                        MaximumExecutionTime = TimeSpan.FromSeconds(20)
                    };
                    var queue = myQueue.GetQueueReference("QueueName");
                    await queue.CreateIfNotExistsAsync();
                    return queue;
                });
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 2014-01-28
      • 2020-12-12
      • 2019-08-17
      相关资源
      最近更新 更多