swathi manda
swathi manda

Reputation: 131

How to decorate a consumer functional interface with retry in resilience4j

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

Answers (2)

Volodymyr Masliy
Volodymyr Masliy

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

Robert Winkler
Robert Winkler

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

Related Questions