使用Spring-Retry指定特定于例外的退避策略

时间:2015-09-01 05:32:06

标签: java spring exception-handling spring-retry

我正在使用Spring-Retry进行一些数据库操作。在SQLRecoverableException上我重试三次(这假设导致异常的任何内容如果失败三次都是非瞬态的),在SQLTransientException上我无限期重试(如果没有,则程序无法执行任何操作访问数据库,因此它可以继续重试,直到用户决定重新启动服务器),并在任何其他异常我不重试。我使用指数退避策略,基本重试为100毫秒,最大重试次数为30,000毫秒。

private static final int MAX_RECOVERABLE_RETRIES = 3;
private static final long INITIAL_INTERVAL = 100;
private static final long MAX_INTERVAL = 30 * 1000;
private static final double MULTIPLIER = 2.0;

public static RetryTemplate databaseTemplate() {
    RetryTemplate template = new RetryTemplate();
    ExceptionClassifierRetryPolicy retryPolicy = new ExceptionClassifierRetryPolicy();
    Map<Class<? extends Throwable>, RetryPolicy> policyMap = new HashMap<>();
    NeverRetryPolicy baseException = new NeverRetryPolicy();
    SimpleRetryPolicy recoverablePolicy = new SimpleRetryPolicy();
    recoverablePolicy.setMaxAttempts(MAX_RECOVERABLE_RETRIES);
    AlwaysRetryPolicy transientPolicy = new AlwaysRetryPolicy();
    policyMap.put(Exception.class, baseException);
    policyMap.put(SQLRecoverableException.class, recoverablePolicy);
    policyMap.put(SQLTransientException.class, transientPolicy);
    retryPolicy.setPolicyMap(policyMap);
    template.setRetryPolicy(retryPolicy);
    ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(INITIAL_INTERVAL);
    backOffPolicy.setMaxInterval(MAX_INTERVAL);
    backOffPolicy.setMultiplier(MULTIPLIER);
    template.setBackOffPolicy(backOffPolicy);
    return template;
}

理想情况下,我想对所有SQLRecoverableExceptions使用100ms的固定退避,并且仅将指数退避策略应用于SQLTransientExceptions。我可以通过嵌套重试来实现这一点,但这会大大增加代码复杂性 - 如果没有其他选项,我宁愿将指数退避应用于SQLRecoverableExceptionSQLTransientException例外。

我是否可以使用单个重试模板将不同的退避策略应用于不同的异常?

3 个答案:

答案 0 :(得分:4)

确实,ExceptionClassifierRetryPolicy是要走的路。我没有设法让它与policyMap一起使用。

以下是我如何使用它:

@Component("yourRetryPolicy")
public class YourRetryPolicy extends ExceptionClassifierRetryPolicy
{
    @PostConstruct
    public void init()
    {
        final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
        simpleRetryPolicy.setMaxAttempts( 3 );

        this.setExceptionClassifier( new Classifier<Throwable, RetryPolicy>()
        {
            @Override
            public RetryPolicy classify( Throwable classifiable )
            {
                    if ( classifiable instanceof YourException )
                    {
                            return new NeverRetryPolicy();
                    }
                    // etc...
                    return simpleRetryPolicy;
            }
        });
    }
}

然后,您只需将其设置在重试模板上:

@Autowired
@Qualifier("yourRetryPolicy")
private YourRetryPolicy yourRetryPolicy;

//...

RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy( yourRetryPolicy );

答案 1 :(得分:0)

接受的答案仅处理特定于异常的RetryPolicy实例。对于特定于异常的BackOffPolicy实例,Spring没有提供任何开箱即用的功能。 幸运的是,实现起来很简单。

import org.springframework.classify.Classifier
import org.springframework.classify.ClassifierSupport
import org.springframework.classify.SubclassClassifier
import org.springframework.retry.RetryContext
import org.springframework.retry.backoff.BackOffContext
import org.springframework.retry.backoff.BackOffInterruptedException
import org.springframework.retry.backoff.BackOffPolicy
import org.springframework.retry.backoff.NoBackOffPolicy

class ExceptionClassifierBackoffPolicy implements BackOffPolicy {

    private static class ExceptionClassifierBackoffContext implements BackOffContext, BackOffPolicy {
        Classifier<Throwable, BackOffPolicy> exceptionClassifier
        RetryContext retryContext
        Map<BackOffPolicy, BackOffContext> policyContextMap = [:]

        ExceptionClassifierBackoffContext(Classifier<Throwable, BackOffPolicy> exceptionClassifier, RetryContext retryContext) {
            this.exceptionClassifier = exceptionClassifier
            this.retryContext = retryContext
        }

        @Override
        BackOffContext start(RetryContext context) {
            return null
        }

        @Override
        void backOff(BackOffContext backOffContext) throws BackOffInterruptedException {
            def policy = exceptionClassifier.classify(retryContext.lastThrowable)
            def policyContext = policyContextMap.get(policy)
            if (!policyContext) {
                policyContext = policy.start(retryContext)
                policyContextMap.put(policy, policyContext)
            }
            policy.backOff(policyContext)
        }
    }
    private Classifier<Throwable, BackOffPolicy> exceptionClassifier = new ClassifierSupport<Throwable, BackOffPolicy>(new NoBackOffPolicy());

    void setPolicyMap(Map<Class<? extends Throwable>, BackOffPolicy> policyMap) {
        exceptionClassifier = new SubclassClassifier<Throwable, BackOffPolicy>(policyMap, new NoBackOffPolicy());
    }

    @Override
    BackOffContext start(RetryContext context) {
        return new ExceptionClassifierBackoffContext(exceptionClassifier, context)
    }

    @Override
    void backOff(BackOffContext backOffContext) throws BackOffInterruptedException {
        def classifierBackOffContext = (ExceptionClassifierBackoffContext) backOffContext
        classifierBackOffContext.backOff(backOffContext)
    }
}

然后:

BackOffPolicy backOffPolicy = new ExceptionClassifierBackoffPolicy()
def policyMap = [
        (RuntimeException): new FixedBackOffPolicy(backOffPeriod: 1000),
        (IOException)     : new ExponentialRandomBackOffPolicy(initialInterval: 500, maxInterval: 360000, multiplier: 2)
] as Map<Class<? extends Throwable>, BackOffPolicy>
backOffPolicy.policyMap = backoffPolicyMap

答案 2 :(得分:0)

聚会有点晚,但当您需要的是根据不同的情况有不同的退避期时 异常的类型,扩展 FixedBackoffPolicy 应该可以解决问题并且非常简单。 一些类似的东西:

首先你创建你的退避策略类,它接收一个带有 每种异常类型的不同退避期:

public class MultipleExceptionsBackoffPolicy extends FixedBackoffPolicy 
{
    private Classifier<Throwable, Long> classifier;

    public MultipleExceptionsBackoffPolicy (final Map<Class<? extends Throwable>, Long> throwableBackoffMap) {
        classifier = new SubclassClassifier<>(throwableBackoffMap, 5_000L) // default is 5s
    }

    @Override
    protected void doBackOff() exception BackOffInterruptedException {
        final backoff = classifier.classify(RetrySynchronizationManager.getContext().getLastThrowable());
        setBackOffPeriod(backoff);
        super.doBackOff();
    }
}

那么你必须创建一个自定义拦截器,例如:

@Bean
public Object myCustomInterceptor (){
    var exBackoffMap = Map.of(
        ExceptionTypeOne.class, 2_000L, // If ExceptionTypeOne happens, backoff period is 2s
        ExceptionTypeTwo.class, 7_000L // and if ExceptionTypeTwo happens, the backoff period is 7s
    )
    return RetryInterceptorBuilder
                .stateless()
                .retryPolicy(new SimpleRetryPolicy(3)) // always 3 attempts no matter what
                .backOffPolicy(new MultipleExceptionsBackoffPolicy(exBackoffMap))
                .build();
}

最后,您只需在可重试中配置自定义拦截器:

@Retryable(interceptor="myCustomInterceptor")
public @interface MyRetryable {}