是否可以修改我的重试模板以抛出AssertionError
时重试?如果可以,怎么办?
这是我的RetryTemplate
bean,没有抓住AssertionErrors
:
@Configuration
@EnableRetry
public class TestConfig {
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(1000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(5);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
}
其他异常会导致正确的重试,因此它必须是可抛出的类型。
答案 0 :(得分:0)
默认情况下,SimpleRetryPolicy
仅对Exception及其子类具有约束力。您可以这样指定异常:
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(1000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(5, Collections.singletonMap(AssertionError.class, true));
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}