您可以使用RetryTemplate重试AssertionError吗?

时间:2019-04-26 15:14:11

标签: java spring

是否可以修改我的重试模板以抛出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;
    }
}

其他异常会导致正确的重试,因此它必须是可抛出的类型。

1 个答案:

答案 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;
    }