I had successful experiments with spring boot configuration for resillence4j but I want to try functional configuration.
So I have implemented following code:
RetryConfig config = RetryConfig.custom()
.maxAttempts(6)
.waitDuration(Duration.ofMillis(100))
.build();
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("name1");
Retry.decorateCheckedSupplier(retry, myService::bar);
myService.bar();
@Service
@Slf4j
public class MyService {
public String bar() throws InterruptedException {
log.error("Bar is called");
Thread.sleep(3000);
if (true) {
throw new RuntimeException("bar exception");
}
return "Success";
}
}
When I start the application I see single error:
Exception in thread "main" java.lang.RuntimeException: bar exception
Why retry was not triggered ?
The issue was that I had to use decorator instead of raw function. Correct implemtation is:
CheckedFunction0<String> decoratedFunction = Retry.decorateCheckedSupplier(retry, myService::bar);
decoratedFunction.apply();