Resilience4j returning a CompletableFuture around tried method with parameter

安稳与你 提交于 2020-06-27 03:56:17

问题


I can't find out how to wrap a synchronous method with Resilience4j so that it returns a CompletableFuture, although this seems to be part of Resilience4j's target area. Especially since the synchronous method I want to wrap can throw an Exception. What I want in pseudo code:

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 should just try to call the method 4 times until it gives up, with intervals between the calls of 100 ms and then complete the asynchronous call. The asyncResilience4jWrapper caller should just get back a CompletableFuture which doesn't block and don't care about any of that.

The really hard part seems to be to get it running for a method with a parameter, throwing an exception!


回答1:


just do

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();
}


来源:https://stackoverflow.com/questions/62147297/resilience4j-returning-a-completablefuture-around-tried-method-with-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!