Java Reference
In-Depth Information
for (Customer customer : customers) {
final Customer curCustomer = customer;
retryTemplate.execute(new RetryCallback<Customer>() {
public Customer doWithRetry(RetryContext retryContext) {
return customerDao.save(curCustomer);
}
});
}
}
...
}
The code in Listing 4-51 depends significantly on elements that need to be injected (getters and
setters were left out of the example). To get a better view of what is happening here, Listing 4-52 shows
the configuration for this example.
Listing 4-52. Configuration for CustomerDao Retry
<beans:bean id="timeoutPolicy"
class="org.springframework.batch.retry.policy.TimeoutRetryPolicy">
<beans:property name="timeout" value="30000"/>
</beans:bean>
<beans:bean id="timeoutRetryTemplate"
class="org.springframework.batch.retry.support.RetryTemplate">
<beans:property name="retryPolicy" ref="timeoutPolicy"/>
</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">
<step id="retryStep">
<tasklet>
<chunk reader="itemReader" writer="retryItemWriter"
processor="itemProcessor" commit-interval="20"/>
</tasklet>
</step>
</job>
Most of the configuration in Listing 4-52 should be straightforward. You configure the
org.springframework.batch.retry.policy.TimeoutRetryPolicy bean with a timeout value set to 30
seconds; you inject that into RetryTemplate as the retryPolicy and inject the template into the
ItemWriter you wrote for Listing 4-51. One thing that is interesting about the configuration of this retry ,
however, is that there is no retry configuration in the job. Because you write your own retry logic, you
don't use the retry-limit and so on in the chunk configuration.
The final way to configure item-retry logic is to use Spring's AOP facilities and Spring Batch's
org.springframework.batch.retry.interceptor.RetryOperationsInterceptor to declaratively apply
 
Search WWH ::




Custom Search