Reputation: 131
Currently DecorateConsumer.java in resilience4j-all library is not providing a method to decorate for Retry(only supports Circuit Breaker, RateLimiter and BulkHead).
Retry.java class also does not provide this option
I have a consumer function public void call(String key) which internally performs a rest Call with the value of key.
But the resilience4j docs mention "You can decorate any Callable, Supplier, Runnable, Consumer, CheckedRunnable, CheckedSupplier, CheckedConsumer or CompletionStage with a Retry."
How can we decorate a Consumer Functional Interface with Retry in resilience4j
Upvotes: 1
Views: 1940
Reputation: 413
Indeed, there is no decorateConsumer, but you can write it yourself:
public static <T> Consumer<T> decorateConsumer(Retry retry, Consumer<T> originalConsumer) {
return (T input) -> retry.executeRunnable(() -> originalConsumer.accept(input));
}
Upvotes: 0
Reputation: 1917
You could do
String key = "key";
Runnable runnable = () -> helloWorldService.sayHelloWorldWithName(key);
Decorators
.ofRunnable(runnable)
.withRetry(Retry.ofDefaults("id"))
.run();
or just
Runnable runnable = () -> helloWorldService.sayHelloWorldWithName(key);
retry.executeRunnable(runnable);
Upvotes: 0