Skip to content

Latest commit

 

History

History
17 lines (12 loc) · 1.14 KB

File metadata and controls

17 lines (12 loc) · 1.14 KB

9.5 声明式重试

有时一些业务逻辑的发生你每次都想重试它。最经典的例子就是远程调用,为了这个目的Spring Batch提供了一个Aop拦截器用来封装一个方法调用RetryOperationsRetryOperationsInterceptor依据RetryPolicy 提供的RepeatTemplate 执行拦截方法和重试失败。

下面是一个示例使用Spring AOP命名空间的声明式迭代重复一个服务调用的一个方法称为remoteCall (AOP如何配置拦截器,更多细节见Spring用户指南):

<aop:config>
    <aop:pointcut id="transactional"
        expression="execution(* com..*Service.remoteCall(..))" />
    <aop:advisor pointcut-ref="transactional"
        advice-ref="retryAdvice" order="-1"/>
</aop:config>

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

在上面的示例中在拦截器内使用了一个默认的RetryTemplate 。改变策略和监听,你只需要在拦截器中注入一个RetryTemplate 实例。