Giriş
RetryPolicy determines when an operation should be retried. A SimpleRetryPolicy is used to retry a fixed number of times.
constructor
Örnek
Şöyle yaparız
public class PayApiRetryTemplate extends RetryTemplate implements InitializingBean {
  private final PayApiConnProp prop;
  private final Class<? extends ApiException>[] exceptions;
    @Override
    public void afterPropertiesSet() throws Exception {
        this.setBackOffPolicy(...);
        this.setRetryPolicy(retryPolicy());
    }
   
  private Map<Class<? extends Throwable>, Boolean> includedExceptions() {
    Map<Class<? extends Throwable>, Boolean> includedExceptions = new HashMap<>();
    for (Class<? extends ApiException> exception : this.exceptions) {
      includedExceptions.put(exception, true);
    }
    return includedExceptions;
  }
  private RetryPolicy retryPolicy() {
    return new SimpleRetryPolicy(this.prop.getRetry().getMaxAttempts(), 
      includedExceptions());
  }
}setMaxAttempts metodu
Örnek
Şöyle yaparız
import org.springframework.retry.annotation.EnableRetry;import org.springframework.retry.backoff.FixedBackOffPolicy;import org.springframework.retry.policy.SimpleRetryPolicy;import org.springframework.retry.support.RetryTemplate;@SpringBootApplication@EnableRetry@EnableJpaRepositories(basePackages = "com.betterjavacode.retrydemo.daos")public class RetrydemoApplication {@Beanpublic RetryTemplate retryTemplate(){RetryTemplate retryTemplate = new RetryTemplate();FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();backOffPolicy.setBackOffPeriod(100);SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();simpleRetryPolicy.setMaxAttempts(2);retryTemplate.setRetryPolicy(simpleRetryPolicy);retryTemplate.setBackOffPolicy(backOffPolicy);return retryTemplate;}}
 
Hiç yorum yok:
Yorum Gönder