如何使用版本 Spring Retry of 1.0.3 启用 Spring retry?

How to enable Spring retry using version Spring Retry of 1.0.3?

要启用 Spring 重试,可以在 Java 注释中启用重试:配置中的 @EnableRetry 或在 XML 配置文件中指定重试:

<context:annotation-config />
<aop:aspectj-autoproxy />
<bean class="org.springframework.retry.annotation.RetryConfiguration" />

两种规范均基于...annotation.RetryConfiguration,仅从 1.1.2 版本开始。如何在以前版本的 XML 配置中启用重试?由于兼容性问题,我无法使用 1.1.2 版。重试配置如下:

<aop:config>
    <aop:pointcut id="retrySave"
        expression="execution( * sfweb.service.webServiceOrders.WsOrderCreationServiceImpl.saveLedger(..))" />
    <aop:advisor pointcut-ref="retrySave" advice-ref="retryAdvice"
        order="-1" />
</aop:config>

<bean id="retryAdvice"
    class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
</bean>

Spring Retry 1.0.3 没有基于 AspectJ 的 AOP 支持。因此,方面风格的重试将不适用于该版本。相反,可重试代码需要包装在 RetryCallback 的实例中。通用方法如下:

1. Create a RetryTemplate

SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
simpleRetryPolicy.setMaxAttempts(maxAttempts);

FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(backOffPeriod);

RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
retryTemplate.setRetryPolicy(simpleRetryPolicy);

2. Wrap the retryable code in a RetryCallback

class FailureProneOperation implements RetryCallback<Void> {
  public void doWithRetry(RetryContext context) throws Exception {
    ...
  }
}

3. Execute the retryable code

retryTemplate.execute(new FailureProneOperation())

除了发布的答案之外,我们还需要一个代码来传递可重试操作的参数。这可以在 FailureProneOperation 构造函数中完成:

public FailureProneOperation(OrderSkuLedger orderSkuLedger) {
    this.orderSkuLedger = orderSkuLedger;
}