是否可以配置@Retryable
?此方法(getCurrentRate)将被调用3次。首先是5分钟,之后10分钟,最后15分钟。我该如何配置?如何在Spring-retry(Spring Boot)中配置延迟时间
@Retryable(maxAttempts=3,value=RuntimeException.class,backoff = @Backoff(delay = 1000))
例
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;
}
}
延迟由退避注释,退避= @Backoff加入(延迟= 300000,乘法器= 2),该值是在毫秒, 没有明确的设置,默认值是1000ms的固定延迟 只有延迟()设置:退避是一个固定的延迟与该值 当设置delay()和maxDelay()时,退避均匀分布在这两个价值es 使用delay(),maxDelay()和multiplier(),退避指数增长到最大值 –