[英]How to enable Spring retry using version Spring Retry of 1.0.3?
要啟用Spring重試,可以在Java批注中啟用重試:Configuration中的@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.創建一個
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())
除了發布的答案外,我們還需要一個代碼來傳遞可重試操作的參數。 這可以在FailureProneOperation構造函數中完成:
public FailureProneOperation(OrderSkuLedger orderSkuLedger) {
this.orderSkuLedger = orderSkuLedger;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.