Configure retry policy pattern

44 Views Asked by At

Is it possible to configure a retry policy of the RetryConfiguration.

I would like to retry the calls of my service with a quadratic pattern. Like waiting 0.5s, then 1s, then 2s, then 3s...ect, between each calls.

If it is not possible with a Retry pattern, what can I do?

Thanks.

According to the java documentation, I think it's not possible. But I prefer to ask directly.

1

There are 1 best solutions below

0
Ksivakumar On

Unfortunately, there is no convenience API to achieve this.

But you could still do this by following these steps:

  1. Implement your own RetryProvider say QuadraticRetryProvider which implements the interfaces RetryProvider and GenericDecorator.The most important point is that you should override the getRetry method where you add an intervalFunction. Refer to the public DefaultRetryProvider class for more details.
@Override
    public Retry getRetry( @Nonnull final ResilienceConfiguration configuration )
    {
        ...

        final RetryConfig customRetryConfig =
            RetryConfig
                .custom()
                    .intervalFunction(IntervalFunction.ofExponentialBackoff(your-interval, your-multiplier))
                ...
                .build();

    }
  1. Create a new resilience decoration strategy that uses the QuadraticRetryProvider.
final Resilience4jDecorationStrategy newStrategy = Resilience4jDecorationStrategy.builder().decorators(Arrays.asList(new DefaultBulkheadProvider(),
new DefaultTimeLimiterProvider(),
new DefaultRateLimiterProvider(),
new DefaultCircuitBreakerProvider(),
new DefaultCachingDecorator(),
new QuadraticRetryProvider())).build();
  1. Use the strategy to decorate and execute your operation
ResilienceDecorator.setDecorationStrategy(newStrategy);
final RetryConfiguration retryConfiguration = RetryConfiguration.of(attempts, waitDuration);
final ResilienceConfiguration configuration =
            ResilienceConfiguration.of("quadratic-retry-config").retryConfiguration(retryConfiguration);
final Callable<?> wrappedCallable = ResilienceDecorator.decorateCallable(()-> your-operation(), configuration);