我无法了解如何用Resilience4j包装同步方法,以便它返回一个CompletableFuture,尽管这似乎是Resilience4j的目标区域的一部分。特别是因为我想包装的同步方法可能引发异常。我想要的伪代码:
boolean void syncMethod() throws Exception {
// May throw Exception due to connection/authorization problems.
}
CompletableFuture<Boolean> asyncResilience4jWrapper() {
CompletableFuture<Boolean> result =
...
Resilience4j magic around "syncMethod()".
Trying 4 calls, interval between calls of 100 ms.
...;
return result;
}Resilience4j应该尝试调用该方法4次,直到它放弃为止,每次调用间隔为100 ms,然后完成异步调用。asyncResilience4jWrapper调用者应该只返回一个不阻塞的CompletableFuture,而不关心这些。
发布于 2020-05-29 12:35:01
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
TimeLimiter timeLimiter = TimeLimiter.of(Duration.ofSeconds(1));
CompletableFuture<Boolean> future = Decorators.ofCallable(() -> syncMethod)
.withThreadPoolBulkhead(threadPoolBulkhead)
.withTimeLimiter(timeLimiter, scheduledExecutorService)
.withCircuitBreaker(circuitBreaker)
.withFallback(asList(TimeoutException.class, CallNotPermittedException.class, BulkheadFullException.class),
throwable -> "Hello from Recovery")
.get().toCompletableFuture();只需将withRetry添加到CircuitBreaker下面即可。
https://stackoverflow.com/questions/62084143
复制相似问题