6
votes

Team,

I am doing a technical poc for reading records from a flat file and inserting the data to database.

I am using chunk task and running this job using spring batch admin successfully.

I have to implement the retry policy along with a feature to set the time interval between each retry. I am stuck up with setting the time interval between each retry as chuck doesn't support it directly. Is there any work around for this?

My code is

<batch:job id="importDataJob" job-repository="jobRepository">
  <batch:step id="importDataStep">
    <batch:tasklet transaction-manager="transactionManager">
      <batch:chunk reader="dataReader" writer="dataWriter"  commit-interval="1" retry-limit="3">
        <batch:retryable-exception-classes>
          <batch:include class="javax.naming.ServiceUnavailableException" />
        </batch:retryable-exception-classes>
      </batch:chunk>
    </batch:tasklet>
  </batch:step>
</batch:job>
1
you will need to define the retry-policy and backoff policy to set the interval. By default spring only allow to set retry attempts. Refer this link for more help javabeat.net/articles/… - Kshitij
also refer to this one for backoff-policy forum.springsource.org/… - Kshitij
@Kshitij : Thanks for the link Kshitij. I thought about the same , but there is no option to set the backoff policy with chunk. - Abhilash
you will need to create a RetryTemplate where in you can configure backoffpolicy. Of what i know is you will need to create your own ItemWriter and the ReptryTemplate will be wired inside it and will than use a callback method. See this link for more details books.google.co.in/… However, an importnt page is missing in the link.Its upto u to find it. - Kshitij
@Kshitij : I want the retry functionality not only in writer , but in my reader also. I am googling the same for last one week and have gone through almost all possible search results. So please don't post google search links. But I would like to appreciate your attitude and effort to support. I even thought about moving out of chunk and writing my own tasklet (Any way I don't need chunk processing, I have put the chunk size as 1). But in that approach I may have to handle the restarting features in the code. :( - Abhilash

1 Answers

6
votes

In your case the configuration will look like:

Spring Batch 2.x

<bean id="stepParent" class="org.springframework.batch.core.step.item.FaultTolerantStepFactoryBean" abstract="true">
  <property name="backOffPolicy">
    <bean class="org.springframework.batch.retry.backoff.FixedBackOffPolicy"
      <property name="backOffPeriod" value="2000" />
    </bean>
  </property>
</bean>

<batch:job id="importDataJob" job-repository="jobRepository">
  <batch:step id="importDataStep" parent="stepParent">
     ...
  </batch:step>
</batch:job>

Unfortunately, batch namespace does not support setting backOffPolicy directly to step, see BATCH-1441.

Spring Batch 3.0

In Spring Batch 3.0 some classes have moved to other packages. This is the the configuration fragment:

<bean id="stepParent"
  class="org.springframework.batch.core.step.factory.FaultTolerantStepFactoryBean"         
  abstract="true">

  <property name="backOffPolicy">
    <bean class="org.springframework.retry.backoff.FixedBackOffPolicy">
      <property name="backOffPeriod" value="2000"/>
    </bean>
  </property>

</bean>