SimpleRetryTemplateSupplier

import java.util.Map;

import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

import com.citi.simpliciti.tempest.util.RawSerializableSupplier;

public class SimpleRetryTemplateSupplier implements RawSerializableSupplier<RetryTemplate> {

  private int maxAttempts;
  private int backOffPeriod;
  private Map<Class<? extends Throwable>, Boolean> retryableExceptions;

  private transient RetryTemplate retryTemplate;

  public SimpleRetryTemplateSupplier(int maxAttempts, int backOffPeriod,
      Map<Class<? extends Throwable>, Boolean> retryableExceptions) {
    this.maxAttempts = maxAttempts;
    this.backOffPeriod = backOffPeriod;
    this.retryableExceptions = retryableExceptions;
  }


  @Override
  public RetryTemplate get() {
    if (retryTemplate == null) {
      SimpleRetryPolicy policy = new SimpleRetryPolicy(maxAttempts, retryableExceptions);

      FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
      fixedBackOffPolicy.setBackOffPeriod(backOffPeriod);

      retryTemplate = new RetryTemplate();
      retryTemplate.setRetryPolicy(policy);
      retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
    }
    return retryTemplate;

  }
}
 @Bean
  public SimpleRetryTemplateSupplier tpsPositionRetryTemplateSupplier() {
    return new SimpleRetryTemplateSupplier(maxAttempts, backOffPeriod,
        ImmutableMap.of(RestClientException.class, true, JsonSyntaxException.class, true));
  }

 

posted @ 2018-07-02 16:04  tonggc1668  阅读(22)  评论(0编辑  收藏  举报