[置顶] spring-retry 分布式重试

环境   spring boot  

<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
  <version>1.1.5.RELEASE</version><!--$NO-MVN-MAN-VER$ --> </dependency>

配置代码:

package com.gomefinance.jie.reserveFunds;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

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

/**
 * Created by Administrator on 17/4/11.
 */
@SuppressWarnings("deprecation")
@SpringBootApplication
public class Application  extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {  
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  
        return builder.sources(Application.class);  
    }  
	
	@Override
    public void customize(ConfigurableEmbeddedServletContainer container) {  
        container.setPort(8081);
    }
    public static void main(String[] args){

        SpringApplication.run(Application.class, args);
    }

    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        //最大尝试次数
        retryPolicy.setMaxAttempts(5);
        retryTemplate.setRetryPolicy(retryPolicy);

        //设置backoff 策略 重试时间间隔策略
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(3000); // 默认时间间隔
        backOffPolicy.setMultiplier(2);    //乘数
        backOffPolicy.setMaxInterval(15000);
        retryTemplate.setBackOffPolicy(backOffPolicy);

        return retryTemplate;
    }
}

测试代码:

	@Autowired
	private RetryTemplate retryTemplate;
public String doSubmit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		/*JSONObject jsonParams = getParams(request);
		String url = jsonParams.getString("url");
		jsonParams.remove("url");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("channel", CHANNEL);
		jsonObject.put("biz_data", jsonParams);
		return HttpClientUtil.postLoan(url, jsonObject.toJSONString());*/
		retryTemplate.execute(new RetryCallback<String, Exception>(){
			public String doWithRetry(RetryContext context) throws Exception {
				System.out.println("doWithRetry==="+ DateUtil.formatDateTime(new Date()));

				throw new Exception();
//				return "ss";
			}
		},new RecoveryCallback<String>(){
			public String recover(RetryContext context) throws Exception {
//				logger.info("{}还款求请常亮重试结束,channelOrderId={},fileName={}", applyId,fileName);
				System.out.println("game over==="+ DateUtil.formatDateTime(new Date()));
				return null;
			}
		});
		return null;
	}

测试结果:

doWithRetry===2017-05-16 19:17:42
doWithRetry===2017-05-16 19:17:45
doWithRetry===2017-05-16 19:17:51
doWithRetry===2017-05-16 19:18:03
doWithRetry===2017-05-16 19:18:18
game over===2017-05-16 19:18:18



你可能感兴趣的:(java,分布式,retry,重试,spring-retry)