Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure delay time in Spring-retry (Spring Boot)

Is it possible to configure the @Retryable ? This methods (getCurrentRate) will be invoked 3 times. At first is 5 min, after that 10 min, lastly 15 min. How can I configure that ?

@Retryable(maxAttempts=3,value=RuntimeException.class,backoff = @Backoff(delay = 1000))

Example

public class RealExchangeRateCalculator implements ExchangeRateCalculator {

    private static final double BASE_EXCHANGE_RATE = 1.09;
    private int attempts = 0;
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

   @Retryable(maxAttempts=3,value=RuntimeException.class,backoff = @Backoff(delay = 1000))
    public Double getCurrentRate() {

        System.out.println("Calculating - Attempt " + attempts + " at " + sdf.format(new Date()));
        attempts++;

        try {
            HttpResponse<JsonNode> response = Unirest.get("http://rate-exchange.herokuapp.com/fetchRate")
                .queryString("from", "EUR")
                .queryString("to","USD")
                .asJson();

            switch (response.getStatus()) {
            case 200:
                return response.getBody().getObject().getDouble("Rate");
            case 503:
                throw new RuntimeException("Server Response: " + response.getStatus());
            default:
                throw new IllegalStateException("Server not ready");
            }
        } catch (UnirestException e) {
            throw new RuntimeException(e);
        }
    }

    @Recover
    public Double recover(RuntimeException e){
        System.out.println("Recovering - returning safe value");
        return BASE_EXCHANGE_RATE;
    }

}
like image 752
John Joe Avatar asked Aug 29 '17 16:08

John Joe


People also ask

How do I enable retry in spring boot?

First, you need to enable Spring Retry. You can achieve this by adding the @EnableRetry annotation to your @SpringBootApplication or @Configuration class. You can now use @Retryable to annotate any method to be a candidate or retry and @Recover to specify fallback methods.

What is backoff in spring Retry?

Spring Uniform Random Backoff The above logic means that the delay period will be random based on initial delay and maxDelay variables set from the retry configuration that you implement.

What is backoff delay?

Only the delay() set: the backoff is a fixed delay with that value. When delay() and maxDelay() are set the backoff is uniformly distributed between the two values. With delay() , maxDelay() and multiplier() the backoff is exponentially growing up to the maximum value.

What is RetryTemplate?

public class RetryTemplate extends Object implements RetryOperations. Template class that simplifies the execution of operations with retry semantics. Retryable operations are encapsulated in implementations of the RetryCallback interface and are executed using one of the supplied execute methods.


1 Answers

You can achieve that with this configuration:

@Retryable(
  maxAttempts=3,
  value=RuntimeException.class,
  backoff = @Backoff(
    delay = 300000,
    multiplier = 2,
    maxDelay = 900000
  )
)

Invocations:

  1. After 5m ~ Delay = 300000
  2. After 10m ~ Delay = 300000 * 2 = 600000
  3. After 15m ~ Delay = 600000 * 2 = 1200000 with Max Delay of 900000
like image 193
Pedro Tavares Avatar answered Sep 28 '22 11:09

Pedro Tavares