Java Reference
In-Depth Information
retry logic to elements of your batch job. Listing 4-53 shows how to declare the aspect's configuration to
apply retry logic to any save methods.
Listing 4-53. Applying Retry Logic Using AOP
<aop:config>
<aop:pointcut id="saveRetry"
expression="execution(* com.apress.springbatch.chapter4.*.save(..))"/>
<aop:advisor pointcut-ref="saveRetry" advice-ref="retryAdvice"
order="-1"/>
</aop:config>
<beans:bean id="retryAdvice"
class="org.springframework.batch.retry.interceptor.RetryOperationsInterceptor"/>
This configuration leaves out the definition of how many retries to attempt, and so on. To add those,
all you need to do is configure the appropriate RetryPolicy into RetryOperationsInterceptor (which has
an optional dependency for a RetryPolicy implementation).
RetryPolicy is similar to CompletionPolicy earlier in that it allows you to programmatically decide
something—in this case, when to retry. The org.springbatch.retry.RetryPolicy used in either the AOP
interceptor or in the regular retry approaches is important, but one thing to note is that you may not
want to just retry over and over if it isn't working. For example, when Google's Gmail can't connect back
to the server, it first tries to reconnect immediately, then it waits 15 seconds, then 30 seconds, and so on.
This approach can prevent multiple retries from stepping on each other's toes. Fortunately, Spring Batch
provides a BackoffPolicy interface to implement this type of decay. You can implement an algorithm
yourself by implementing the BackoffPolicy interface or use the ExponentialBackOffPolicy provided by
the framework. Listing 4-54 shows the configuration required for BackOffPolicy .
Listing 4-54. Applying Retry Logic Using AOP
<beans:bean id="timeoutPolicy"
class="org.springframework.batch.retry.policy.TimeoutRetryPolicy">
<beans:property name="timeout" value="30000"/>
</beans:bean>
<beans:bean id="backoutPolicy"
class="org.springframework.batch.retry.backoff.ExponentialBackOffPolicy"/>
<beans:bean id="timeoutRetryTemplate"
class="org.springframework.batch.retry.support.RetryTemplate">
<beans:property name="retryPolicy" ref="timeoutPolicy"/>
<beans:property name="backOffPolicy" ref="backOffPolicy"/>
</beans:bean>
<beans:bean id="retryItemWriter"
class="com.apress.springbatch.chapter4.RetryItemWriter">
<beans:property name="customerDao" ref="customerDao"/>
<beans:property name="retryTemplate" ref="timeoutRetryTemplate"/>
</beans:bean>
<job id="flowJob">
 
Search WWH ::




Custom Search