1

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

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

这两个规范都基于仅从版本 1.1.2 开始的…annotation.RetryConfiguration。如何在以前的版本中启用 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>
4

2 回答 2

1

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

1.创建一个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.将可重试代码包装在RetryCallback

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

3.执行可重试代码

retryTemplate.execute(new FailureProneOperation())
于 2016-03-05T02:21:11.447 回答
0

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

public FailureProneOperation(OrderSkuLedger orderSkuLedger) {
    this.orderSkuLedger = orderSkuLedger;
}
于 2016-03-08T18:52:35.607 回答