如何根据异常更改spring重试模板固定退避策略
How to change the spring retry template fixed back off policy based on the exception
我正在使用 Spring 重试模板进行重试。
这是我的重试方法,这里我的重试间隔应该是基于Exception的。例如,如果抛出 DataException,则重试应该发生 1000(1 秒)时间间隔,或者如果抛出 MQException,则重试应该发生 5000(5 秒)时间间隔。
如何根据异常更改FixedBackOffPolicy实例。
主要流程:
private void retryTest(){
retryTemplate.execute(context -> {
log.info("Processing request...");
retryTest1();
return true;
},
context -> {
log.info("Recovering request...");
return true;
});
}
private void retryTest1(){
throw new DataException("Data exception");
//throw new MQException("MQ Message exception");
}
重试模板初始化:
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(1000L);
retryTemplate.setBackOffPolicy(new CustomBackOffPolicy()); // Custom backoff policy
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(5);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
public class CustomBackOffPolicy implements BackOffPolicy {
@Autowired
RetryTemplate retryTemplate;
@Override
public BackOffContext start(RetryContext retryContext) {
RetryPolicy retryPolicy = (RetryPolicy) retryContext;
return null;
}
@Override
public void backOff(BackOffContext backOffContext) throws BackOffInterruptedException {
try {
Thread.sleep(1000l);
} catch (InterruptedException var2) {
throw new BackOffInterruptedException("Thread interrupted while sleeping", var2);
}
}
您需要定制 BackOffPolicy
;您可以从 start()
方法中的 RetryContext
获取异常类型。
您可以使用与 ExceptionClassifierRetryPolicy
中使用的技术类似的技术,该技术根据异常类型委托不同的重试策略。
我正在使用 Spring 重试模板进行重试。
这是我的重试方法,这里我的重试间隔应该是基于Exception的。例如,如果抛出 DataException,则重试应该发生 1000(1 秒)时间间隔,或者如果抛出 MQException,则重试应该发生 5000(5 秒)时间间隔。
如何根据异常更改FixedBackOffPolicy实例。
主要流程:
private void retryTest(){
retryTemplate.execute(context -> {
log.info("Processing request...");
retryTest1();
return true;
},
context -> {
log.info("Recovering request...");
return true;
});
}
private void retryTest1(){
throw new DataException("Data exception");
//throw new MQException("MQ Message exception");
}
重试模板初始化:
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(1000L);
retryTemplate.setBackOffPolicy(new CustomBackOffPolicy()); // Custom backoff policy
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(5);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
public class CustomBackOffPolicy implements BackOffPolicy {
@Autowired
RetryTemplate retryTemplate;
@Override
public BackOffContext start(RetryContext retryContext) {
RetryPolicy retryPolicy = (RetryPolicy) retryContext;
return null;
}
@Override
public void backOff(BackOffContext backOffContext) throws BackOffInterruptedException {
try {
Thread.sleep(1000l);
} catch (InterruptedException var2) {
throw new BackOffInterruptedException("Thread interrupted while sleeping", var2);
}
}
您需要定制 BackOffPolicy
;您可以从 start()
方法中的 RetryContext
获取异常类型。
您可以使用与 ExceptionClassifierRetryPolicy
中使用的技术类似的技术,该技术根据异常类型委托不同的重试策略。