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

时间:2016-03-05 01:08:22

标签: java spring spring-aop spring-annotations spring-aspects

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

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

这两个规范都基于...... annotationConfiguration,它仅从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>

2 个答案:

答案 0 :(得分: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())

答案 1 :(得分:0)

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

my_unsigned_char = my_unsigned_char | 1;