0

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.

Molay
  • 1,154
  • 2
  • 19
  • 42

1 Answers1

2

it's currently only possible by using a RetryConfigCustomizer.

@Bean
public RetryConfigCustomizertestCustomizer() {
    return RetryConfigCustomizer
        .of("retryService1", builder -> builder.intervalFunction(intervalWithExponentialBackoff));
}
Robert Winkler
  • 1,734
  • 9
  • 8
  • if you don't mind, can you please give me some more details about it. Don't have sufficient document about it. I am wondering how can i pass multiple intervals or waitDurations ? Builder accepts only 1 duration, builder -> builder.intervalFunction(IntervalFunction.of(Duration.ofMinutes(2)))) – Molay May 22 '20 at 13:11
  • 1
    You can implement your own IntervalFunction. It's an interface. You can provide any class. You dont have to use our builder. – Robert Winkler May 24 '20 at 16:17