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;
}
}
You can achieve that with this configuration:
@Retryable(
maxAttempts=3,
value=RuntimeException.class,
backoff = @Backoff(
delay = 300000,
multiplier = 2,
maxDelay = 900000
)
)
Invocations:
Delay = 300000
Delay = 300000 * 2 = 600000
Delay = 600000 * 2 = 1200000 with Max Delay of 900000
@Sivakumar Neelam Veera
You can express the retry logic using the RetryTemplate class. There you can inject your dependencies and use them programatically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.