15 Şubat 2021 Pazartesi

SpringCloud Feign Retrier Arayüzü

Giriş
Bir başka örnek burada

Örnek
Elimizde şöyle bir kod olsun.
class Custom implements Retryer {

  private final int maxAttempts;
  private final long backoff;
  int attempt;

  public Custom() {
    this(2000, 3);
  }

  public Custom(long backoff, int maxAttempts) {
    this.backoff = backoff;
    this.maxAttempts = maxAttempts;
    this.attempt = 1;
  }

  public void continueOrPropagate(RetryableException e) {
    if (attempt++ >= maxAttempts) {
      throw e;
    }

    try {
      Thread.sleep(backoff);
    } catch (InterruptedException ignored) {
      Thread.currentThread().interrupt();
    }
  }

  @Override
  public Retryer clone() {
    return new Custom(backoff, maxAttempts);
  }
}
Şöyle yaparız
@Configuration
public class FeignClientConfig {

  @Bean
  public Retryer retryer() {
    return new Custom();
  }
}

Hiç yorum yok:

Yorum Gönder