我无法了解如何用Resilience4j包装同步方法,以便它返回一个CompletableFuture,尽管这似乎是Resilience4j的目标区域的一部分。特别是因为我想包装的同步方法可能引发异常。我想要的伪代码:
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 ms,然后完成异步调用。asyncResilience4jWrapper调用者应该只返回一个不阻塞的CompletableFuture,而不关心这些。
真正困难的部分似乎是让它运行一个带有参数的__的方法,抛出一个异常!
发布于 2020-06-02 11:42:39
就这么做
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
复制相似问题