Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch How to set time interval between each call in a Chunk tasklet

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>
like image 387
Abhilash Avatar asked Apr 27 '12 10:04

Abhilash


People also ask

What is the difference between Tasklet and chunk in Spring Batch?

When the job having error, is to be recovered by only re-running the target job, tasklet model can be chooseed to make recovery simple. In chunk model, it should be dealt by returning the processed data to the state before executing the job and by creating a job to process only the unprocessed data.

How does chunk size work in Spring Batch?

Spring Batch collects items one at a time from the ItemReader into a configurable-sized chunk. Spring Batch then sends the chunk to the ItemWriter and goes back to using the ItemReader to create another chunk, and so on, until the input is exhausted.

What is Tasklet in Spring Batch?

In Spring batch, the Tasklet is an interface, which will be called to perform a single task only, like clean or set up resources before or after any step execution. In this example, we will show you how to use Tasklet to clean up the resource (folders) after a batch job is completed.

What is remote chunking in Spring Batch?

Remote ChunkingThe manager is an implementation of a Spring Batch Step with the ItemWriter replaced by a generic version that knows how to send chunks of items to the middleware as messages.


1 Answers

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>
like image 113
dma_k Avatar answered Oct 05 '22 12:10

dma_k