繁体   English   中英

Resilience4j 使用 CompletionStage 重试

[英]Resilience4j retry with 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

正如预期的那样!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM