如何解决Resilience4j 使用 CompletionStage 重试
private CompletionStage<org.asynchttpclient.Response> executeWithRetries(Request request) {
RetryConfig retryConfig = RetryConfig.<org.asynchttpclient.Response>custom()
.maxAttempts(5)
.intervalFunction(IntervalFunction
.ofExponentialBackoff(TimeUnit.SECONDS.toMillis(2),1.2))
.build();
Retry retry = Retry.of("proxy-retry",retryConfig);
supplier<CompletionStage<org.asynchttpclient.Response>> retryablesupplier = Retry.decorateCompletionStage(
retry,Executors.newScheduledThreadPool(10),() -> executeCall(request));
return retryablesupplier.get();
}
我正在使用此方法,希望 executeCall 在抛出异常时至少重试 3 次。 executeCall(request) 方法返回一个 CompletionStage。
当我尝试对这段代码进行单元测试时,executeCall(request) 方法的调用次数仅为一次(我在此方法中抛出异常)。
我如何确保它至少重试 5 次(这是默认值)
解决方法
可能您在供应商中抛出异常,而不是在供应商.get() 代码返回的未来内。我试过以下代码:
import java.util.concurrent.*;
import java.util.function.*;
import io.github.resilience4j.retry.*;
public class Main {
private static final ScheduledExecutorService scheduledExecutorService =
Executors.newScheduledThreadPool(10);
public static void main(final String[] args) {
RetryConfig retryConfig = RetryConfig.custom()
.maxAttempts(5)
.intervalFunction(
IntervalFunction.ofExponentialBackoff(10,1.2))
.build();
Retry retry = Retry.of("proxy-retry",retryConfig);
Supplier<CompletionStage<String>> supplier =
() -> CompletableFuture.supplyAsync(() -> {
System.out.println("failing code");
throw new RuntimeException();
});
retry.executeCompletionStage(scheduledExecutorService,supplier);
}
}
输出为:
failing code
failing code
failing code
failing code
failing code
正如预期的那样!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。