Programming and So

Tips and tricks in Java

Spring Batch 2.0: Retry a tasklet

leave a comment »

Retry strategies in Spring Batch 2.0 allow chunk reprocessing under several fails. However, there is no core support to Tasklet steps retry and so, AOP techniques must be used.

Below both retry patterns (chunk and tasklet) are exposed.

Chunk retry: retry three times if some exception occurs on “chunk” execution

<step id="step">
    <tasklet>
        <chunk reader="itemReader"
               writer="itemWriter"
               commit-interval="1"
               retry-limit="3">
            <retryable_exception_classes>
                java.lang.Exception
            </retryable_exception_classes>
        </chunk>
    </tasklet>
</step>

Tasklet retry: retry three times if some exception occurs on “tasklet” execution

<step id="step">
    <tasklet ref="retryableStep" />
</step>

<bean id="retryableStep" class="foo.bar.RetryableStep" />

<aop_config>
    <aop_pointcut id="transactional" expression="execution(* foo..RetryableStep.execute(..))" />
    <aop_advisor pointcut-ref="transactional" advice-ref="retryAdvice" order="-1"/>
</aop_config>

<bean id="retryAdvice"
      class="org.springframework.batch.retry.interceptor.RetryOperationsInterceptor">
  <property name="retryOperations">
    <bean class="org.springframework.batch.retry.support.RetryTemplate">
      <property name="retryPolicy">
        <bean class="org.springframework.batch.retry.policy.SimpleRetryPolicy">
          <property name="maxAttempts" value="3"/>
          <property name="retryableExceptionClasses">
	            <list>
              <value>java.lang.Exception</value>
            </list>
          </property>
        </bean>
      </property>
    </bean>
  </property>
</bean>

Written by angelborroy

May 20, 2009 at 11:04 am

Posted in java

Tagged with

Leave a Reply