RetryTemplateConfig.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.shkpr.service.alambizplugin.configuration;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Primary;
  6. import org.springframework.retry.backoff.ExponentialBackOffPolicy;
  7. import org.springframework.retry.backoff.FixedBackOffPolicy;
  8. import org.springframework.retry.policy.SimpleRetryPolicy;
  9. import org.springframework.retry.support.RetryTemplate;
  10. @Configuration
  11. @EnableAutoConfiguration
  12. public class RetryTemplateConfig {
  13. @Primary
  14. @Bean(name = "Normal3Retry")
  15. public RetryTemplate normal3Retry(){
  16. RetryTemplate template = new RetryTemplate();
  17. SimpleRetryPolicy policy = new SimpleRetryPolicy();//默认最大重试次数为3
  18. ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
  19. backOffPolicy.setInitialInterval(200L);
  20. backOffPolicy.setMaxInterval(200L);
  21. backOffPolicy.setMultiplier(2.0);//第一次间隔为T1=200ms,第二次为T2=2*T1=400ms,第三次为T3=2*T2=800ms
  22. template.setBackOffPolicy(backOffPolicy);
  23. template.setRetryPolicy(policy);
  24. return template;
  25. }
  26. @Bean(name = "FixedPeriod3Retry")
  27. public RetryTemplate fixedPeriod3Retry(){
  28. RetryTemplate template = new RetryTemplate();
  29. SimpleRetryPolicy policy = new SimpleRetryPolicy();//默认最大重试次数为3
  30. FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
  31. fixedBackOffPolicy.setBackOffPeriod(500L); //每次重试固定间隔500ms
  32. template.setBackOffPolicy(fixedBackOffPolicy);
  33. template.setRetryPolicy(policy);
  34. return template;
  35. }
  36. }