public static void main(String[] args) throws Throwable {
RetryTemplate retryTemplate = new RetryTemplate();
//指数退避策略 等待时间= 等待时间*倍数 ,即每一次的等待时间是上一次等待时间的n倍,
//到达最大的等待时间之后就不在增加了,一直都是以最大的等待时间在等待。默认执行3次
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
//初始等待时间
exponentialBackOffPolicy.setInitialInterval(1000);
//时间等待倍数
exponentialBackOffPolicy.setMultiplier(2);
//最大等待时间
exponentialBackOffPolicy.setMaxInterval(5000);
retryTemplate.setBackOffPolicy(exponentialBackOffPolicy);
String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
@Override
public String doWithRetry(RetryContext context) throws Throwable {
//需要重试的代码
System.out.println("开始执行======");
throw new TimeoutException();
}
}, new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) throws Exception {
//重试失败后执行的代码
return "failed callback";
}
});
System.out.println(execute);
}
/**
* 根据不同的异常选择不同的重试策略
*
* @throws Throwable
*/
public static void exceptionClassifier() throws Throwable {
RetryTemplate retryTemplate = new RetryTemplate();
ExceptionClassifierRetryPolicy policy = new ExceptionClassifierRetryPolicy();
policy.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
@Override
public RetryPolicy classify(Throwable classifiable) {
if (classifiable instanceof NullPointerException) {
//NullPointerException 异常就是SimpleRetryPolicy策略
return new SimpleRetryPolicy();
}
if (classifiable instanceof TimeoutException) {
//如果是TimeoutException 异常就是AlwaysRetryPolicy(一直执行直到成功)策略
return new AlwaysRetryPolicy();
}
//不执行直接走 RecoveryCallback。recover()
return new NeverRetryPolicy();
}
});
retryTemplate.setRetryPolicy(policy);
String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
@Override
public String doWithRetry(RetryContext context) throws Throwable {
//需要重试的代码
System.out.println("开始执行======");
throw new TimeoutException();
}
}, new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) throws Exception {
//重试失败后执行的代码
return "failed callback";
}
});
System.out.println(execute);
}
public static void simpleExceptionRetry() throws Throwable {
Map<Class<Exception>, Boolean> classBooleanMap = Collections.singletonMap(Exception.class, true);
RetryTemplate retryTemplate = new RetryTemplate();
//重试的代码块如果有NullPointerException就重复执行,最多执行5遍。
//如果发生的异常不是NullPointerException 就不会重复执行直接执行RecoveryCallback。recover()
SimpleRetryPolicy policy2 = new SimpleRetryPolicy(5, Collections.singletonMap(NullPointerException.class, true));
retryTemplate.setRetryPolicy(policy2);
String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
@Override
public String doWithRetry(RetryContext context) throws Throwable {
//需要重试的代码
System.out.println("开始执行======");
throw new TimeoutException();
}
}, new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) throws Exception {
//重试失败后执行的代码
return "failed callback";
}
});
System.out.println(execute);
}
public static void simpleRetry() throws Throwable {
RetryTemplate retryTemplate = new RetryTemplate();
//最多重试3次
SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(3);
retryTemplate.setRetryPolicy(simpleRetryPolicy);
String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
@Override
public String doWithRetry(RetryContext context) throws Throwable {
//需要重试的代码
System.out.println("开始执行======");
int i = 1 / 0;
return "success";
}
}, new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) throws Exception {
//重试失败后执行的代码
return "failed callback";
}
});
System.out.println(execute);
}
/**
* 超时时间内重试策略
*
* @throws Throwable
*/
public static void timeOutRetry() throws Throwable {
RetryTemplate retryTemplate = new RetryTemplate();
TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy();
retryTemplate.setRetryPolicy(timeoutRetryPolicy);
//在这个时间内如果失败会不停的重试
timeoutRetryPolicy.setTimeout(10);
String execute = retryTemplate.execute(new RetryCallback<String, Throwable>() {
@Override
public String doWithRetry(RetryContext context) throws Throwable {
//需要重试的代码
System.out.println("开始执行======");
TimeUnit.SECONDS.sleep(3);
int i = 1 / 0;
return "success";
}
}, new RecoveryCallback<String>() {
@Override
public String recover(RetryContext context) throws Exception {
//重试失败后执行的代码
return "failed callback";
}
});
System.out.println(execute);
}
RetryTemplate用法
最新推荐文章于 2024-04-07 10:06:29 发布