问题
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