In the docs the section on working with CompletableFuture looks like
Supplier<String> decoratedSupplier = CircuitBreaker
.decorateSupplier(circuitBreaker, () -> "This can be any method which returns: 'Hello");
CompletableFuture<String> future = CompletableFuture.supplyAsync(decoratedSupplier)
.thenApply(value -> value + " world'");
Doesn't seem like there is a straightforward to work with existing asynchronous code that may already return CompletableFuture, for example lets say I wanted to use a circuit-breaker on something that implements:
public interface Client {
public CompletableFuture<String> get();
}
Would the recommended way to do this be:
SupplierdecorateSupplierOfCompletableFuture method that does Supplier.get() after the call to CircuitBreakerUtils.isCallPermitted(circuitBreaker)???
Thanks.
Hi, thx for evaluating this library. If you like this library, we would love to get your GitHub Star.
I will add a new method decorateCompletableFuture to the CircuitBreaker.
static <T> Supplier<CompletableFuture<T>> decorateCompletableFuture(CircuitBreaker circuitBreaker, Supplier<CompletableFuture<T>> supplier){
return () -> {
CircuitBreakerUtils.isCallPermitted(circuitBreaker);
StopWatch stopWatch = StopWatch.start(circuitBreaker.getName());
return supplier.get().whenComplete((returnValue, throwable) -> {
if (returnValue != null) {
circuitBreaker.onSuccess(stopWatch.stop().getProcessingDuration());
} else {
circuitBreaker.onError(stopWatch.stop().getProcessingDuration(), throwable);
}
});
};
}
You can use it as follows:
Supplier<CompletableFuture<String>> supplier = CircuitBreaker.decorateCompletableFuture(circuitBreaker, Client::get);
CompletableFuture<String> decoratedCompletableFuture = supplier.get();
Kind regards,
Robert
I hope this works for you.
Hi! Have recently made the same thing in my fork of this library. You can find commit here. Implementation covers cases when exception occurs in CompletionStage supplier itself.
This commit also contains implementation of AsyncRetry addressing retries with completion stage.
Probably it would help as well.
@RobWin would it be ok to contribute this code in this repo?
Hi,
very nice. Your solution is better than mine.
We would be happy, if you would like to contribute it.
Just create a PR, but please change the version
馃憤
Most helpful comment
Hi! Have recently made the same thing in my fork of this library. You can find commit here. Implementation covers cases when exception occurs in CompletionStage supplier itself.
This commit also contains implementation of AsyncRetry addressing retries with completion stage.
Probably it would help as well.
@RobWin would it be ok to contribute this code in this repo?