微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 WaitAndRetryAsync 定义执行

如何解决在 WaitAndRetryAsync 定义执行

目标:
如果您已经尝试了第三次并且没有成功。那你想用另一种方法
我想阻止显示错误消息网页。

问题:
是否可以在 WaitAndRetryAsync 中输入类似于 catch 方法方法

RetryPolicy<HttpResponseMesssage> httpWaitAndRetryPolicy = Policy
    .HandleResult<HttpResponseMessage>(r => !r.IsSuccessstatusCode)
    .WaitAndRetryAsync
        (3,retryAttempt => TimeSpan.FromSeconds(Math.Pow(2. retryAttempt)/2));

谢谢!

解决方法

您可以在您的政策中使用 at org.apache.hadoop.util.Shell.runCommand(Shell.java:1008) at org.apache.hadoop.util.Shell.run(Shell.java:901) at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:1213) at org.apache.hadoop.util.SysInfoWindows.getSystemInfoInfoFromShell(SysInfoWindows.java:86) at org.apache.hadoop.util.SysInfoWindows.refreshIfNeeded(SysInfoWindows.java:101) at org.apache.hadoop.util.SysInfoWindows.getPhysicalMemorySize(SysInfoWindows.java:153) at org.apache.hadoop.yarn.util.ResourceCalculatorPlugin.getPhysicalMemorySize(ResourceCalculatorPlugin.java:64) ,然后使用 ExecuteAsync 来处理这样的最终响应:

ContinueWith

根据@TheodorZoulias 的评论,使用 RetryPolicy<HttpResponseMessage> .Handle<HttpRequestException>() .Or<TaskCanceledException>() .WaitAndRetryAsync (3,retryAttempt => TimeSpan.FromSeconds(Math.Pow(2,retryAttempt) / 2)) .ExecuteAsync(() => { //do stuff that you want retry }).ContinueWith(x => { if (x.Exception != null) { //means exception raised during execute and handle it } // return your HttpResponseMessage },scheduler: TaskScheduler.Default); 的最佳做法是将 ContinueWith 显式设置为默认值,因为 TaskScheduler 将调度程序更改为 ContinueWith 并可能导致死锁。

,

首先,WaitAndRetryAsync 返回 AsyncRetryPolicy<T>,而不是 RetryPolicy<T>,这意味着您发布的代码无法编译。

在 polly 的情况下,策略的定义和该策略的执行是分开的。因此,您首先要定义一个策略(或多个策略的组合),然后在需要时执行它。

定义

AsyncRetryPolicy<HttpResponseMessage> retryInCaseOfNotSuccessResponsePolicy = Policy
    .HandleResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
    .WaitAndRetryAsync
        (3,retryAttempt => TimeSpan.FromSeconds(Math.Pow(2.retryAttempt) / 2));

执行

HttpResponseMessage serviceResponse = null;
try
{
    serviceResponse = await retryInCaseOfNotSuccessResponsePolicy.ExecuteAsync(
        async ct => await httpClient.GetAsync(resourceUri,ct),default);
}
catch (Exception ex)
    when(ex is HttpRequestException || ex is OperationCanceledException)
{
    //TODO: log
}

if (serviceResponse == null)
{
    //TODO: log
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。