Difference between Step, Tasklet and Chunk in Spring Batch

Priyanka Mokashi picture Priyanka Mokashi · Oct 14, 2016 · Viewed 27.8k times · Source

What is the difference between Step, Tasklet and Chunk in spring batch.?

Also, How to execute step in parallel via Spring Batch. ?

Answer

Asoub picture Asoub · Oct 14, 2016

Well that's actually a good question. Here's an example of configuration:

<job id="sampleJob" job-repository="jobRepository">
    <step id="step1" next="step2">
        <tasklet transaction-manager="transactionManager">
            <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
        </tasklet>
    </step>
    <step id="step2">
         <tasklet ref="myTasklet"/>
    </step>
</job>

You have a Job, this job is made of steps. Most of the time, these steps are successive. You define in what order your work must be done with steps: you do step 1, then step 2, then step 3, you can do step 4 if step 3 failed, or go directly to step 5, etc.

What is done in the Step is represented by a tasklet, they do the task.

In spring batch, you'll mostly do chunk oriented processing: with a reader, a processor, and a writer. From the official documentation:

Chunk oriented processing refers to reading the data one at a time, and creating 'chunks' that will be written out, within a transaction boundary

But you can make your own tasklet, and set it in your step. For example, a tasklet that executes a SQL query. (example here: Tasklet to delete a table in spring batch )

So, the steps are ordered in a job, each step contains a tasklet, which does a task. One of those tasklet (and probably the most used one) is the chunk oriented processing tasklet.

If you're curious, here is the ChunkOrientedTasklet's doc. As you can see, it implements the Tasklet interface.

For more information: http://docs.spring.io/spring-batch/reference/html/configureStep.html

And yes, spring batch is well made for parrallel processing, using flows: http://docs.spring.io/spring-batch/reference/html/scalability.html