web-dev-qa-db-ja.com

スプリングリトライ(スプリングブート)で遅延時間を構成する方法

@Retryable?このメソッド(getCurrentRate)は3回呼び出されます。最初は5分、その後は10分、最後は15分です。どうすれば設定できますか?

@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;
    }

}
8
John Joe

この構成でそれを達成できます:

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

呼び出し:

  1. 5分後〜Delay = 300000
  2. 10分後〜Delay = 300000 * 2 = 600000
  3. 15分後〜Delay = 600000 * 2 = 1200000 with Max Delay of 900000
15
Pedro Tavares