2

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!

Jan
  • 1,032
  • 11
  • 26

1 Answers1

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();
}
Robert Winkler
  • 1,734
  • 9
  • 8