Search code examples
spring-bootspring-retryresilience4j

Resiliance4j - retry custom IntervalFunction with multiple waitDuration?


Resilience4j version: 1.4.0 Java version: 11

I want to achieve the exact thing mentioned here at : https://resilience4j.readme.io/docs/retry#use-a-custom-intervalfunction

If you don't want to use a fixed wait duration between retry attempts, you can configure an IntervalFunction which is used instead to calculate the wait duration for every attempt.

Do we need to configure it in yml file ? or do we need to overwrite IntervalFunction ? I need to have 3 retries exponentially, each with different wait duration, for eg : 1st retry should be after 30 sec, 2nd retry after 45 min, 3rd retry should be after 2hrs. Where to configure these timestamps ? I am newbie, please suggest.

Service class:

@Retry(name = "retryService1", fallbackMethod = "retryfallback")
@Override
public String customerRegistration(DTO dto) throws ExecutionException, InterruptedException {
    String response = restTemplate("/register/customer", dto, String.class); // this throws 500 error code
    return response ;
}

public String retryfallback(DTO dto, Throwable t) {
    logger.error("Inside retryfallback, cause - {} {}", t.toString(), new Date());
    return "Inside retryfallback method. Some error occurred while calling service for customer registration";
}

application.yml

resilience4j.retry.instances:
    retryService1:
      maxRetryAttempts: 3
      waitDuration: 10s
      enableExponentialBackoff: true
      exponentialBackoffMultiplier: 2

Thanks.


Solution

  • it's currently only possible by using a RetryConfigCustomizer.

    @Bean
    public RetryConfigCustomizertestCustomizer() {
        return RetryConfigCustomizer
            .of("retryService1", builder -> builder.intervalFunction(intervalWithExponentialBackoff));
    }