【问题标题】:Resilience4j returning a CompletableFuture around tried method with parameterResilience4j 返回一个 CompletableFuture 围绕带参数的尝试方法
【发布时间】:2020-09-20 15:27:03
【问题描述】:

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

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

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

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

真正困难的部分似乎是让它运行一个带有参数的方法,抛出一个异常!

【问题讨论】:

    标签: java resilience4j


    【解决方案1】:

    只是做

    CompletableFuture<Boolean> asyncResilience4jWrapper(Parameter param) {
       return CompletableFuture<Boolean> future = Decorators.ofCallable(() -> syncMethod(param))
        .withThreadPoolBulkhead(threadPoolBulkhead)
        .withTimeLimiter(timeLimiter, scheduledExecutorService)
        .withCircuitBreaker(circuitBreaker)
        .withRetry(retry)
        .withFallback(asList(TimeoutException.class, CallNotPermittedException.class, BulkheadFullException.class),
          throwable -> "Hello from Recovery")
        .get().toCompletableFuture();
    }
    

    【讨论】:

    • 它对你有用吗?如果是,请采纳答案
    • 我再也没有回来进行真正的测试,我被从项目中移出并且不再拥有源代码。对不起。我会尝试测试它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多