【问题标题】:Resilience4j returning a CompletableFuture around tried methodResilience4j 围绕尝试的方法返回 CompletableFuture
【发布时间】:2020-06-02 15:48:46
【问题描述】:

我不知道如何用 Resilience4j 包装一个同步方法,以便它返回一个 CompletableFuture,尽管这似乎是 Resilience4j 的 target 区域的一部分。 特别是因为我要包装的同步方法可以抛出异常。 我想要的伪代码:

boolean void syncMethod() throws Exception {
    // May throw Exception due to connection/authorization problems.
}

CompletableFuture<Boolean> asyncResilience4jWrapper() {
    CompletableFuture<Boolean> result = 
        ...
            Resilience4j magic around "syncMethod()". 
            Trying 4 calls, interval between calls of 100 ms. 
        ...;
    return result;
}

Resilience4j 应该尝试调用该方法 4 次直到它放弃,调用之间的间隔为 100 毫秒,然后完成异步调用。 asyncResilience4jWrapper 调用者应该只返回一个 CompletableFuture ,它不会阻塞并且不关心任何这些。

【问题讨论】:

    标签: java resilience4j


    【解决方案1】:
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
    TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
    
    CompletableFuture<Boolean> future = Decorators.ofCallable(() -> syncMethod)
        .withThreadPoolBulkhead(threadPoolBulkhead)
        .withTimeLimiter(timeLimiter, scheduledExecutorService)
        .withCircuitBreaker(circuitBreaker)
        .withFallback(asList(TimeoutException.class, CallNotPermittedException.class, BulkheadFullException.class),
          throwable -> "Hello from Recovery")
        .get().toCompletableFuture();
    

    只需在 CircuitBreaker 下方添加 withRetry

    【讨论】:

    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2018-10-06
    • 2017-10-28
    • 2019-02-04
    相关资源
    最近更新 更多