In Spring Batch, a Tasklet is an interface that performs a single task within a Step. A typical use case for implementing a Tasklet is the setup up or cleaning of resources before or after the execution of a Step.
In fact, Spring Batch offers two different ways for implementing a step of a batch job: using Chunks or using a Tasklet.
In the Spring Batch Job example, we saw that a batch job consists out of one or more Steps. And a Tasklet represents the work that is done in a Step.
The Tasklet interface has one method: execute(). A Step calls this method repeatedly until it either finishes or throws an exception.
The Spring Batch framework contains some implementations of the Tasklet interface. One of them is a “chunk oriented processing” Tasklet. If you look at the ChunkOrientedTasklet you can see it implements the Tasklet interface.
So let’s recap the above:
Question
Answer
When do I use a Tasklet?
When you need to execute a single granular task.
How does a Tasklet work?
Everything happens within a single transaction boundary that either finishes or throws an error.
Is a Tasklet often used?
It is not used very often. In most cases, you will use chunks to handle large volumes.
What is a typical Tasklet use case?
Usually used to setup up or clean resources before or after the main processing.
To show you how a Spring Batch Tasklet works let’s create a simple example.
We then change the batch job so that it reads multiple CSV files. When the Job finishes we clean up the input files using a Tasklet.
2. General Project Overview
We will use the following tools/frameworks:
Spring Batch 4.1
Spring Boot 2.1
Maven 3.6
Our Maven project has the following structure:
The Maven and Spring Boot setup are identical to a previous Spring Batch example so we will not cover them in this post.
3. Creating a Spring Batch Tasklet
To create a Spring Batch Tasklet you need to implement the Tasklet interface.
Let’s start by creating a FileDeletingTasklet that will delete all files in a directory. Add the execute() method that walks over the available files and tries to delete them.
When all files are deleted we return the FINISHED status so that the Step that calls the FileDeletingTasklet can finish.
We also add a constructor that sets the directory that needs to be cleaned.
Now that our Spring Batch Tasklet is created let’s change the CapitalizeNamesJobConfig to include it.
We add a deleteFilesStep Bean that uses the FileDeletingTasklet. We then adapt the capitalizeNamesJob Bean so that this new Step is executed at the end.
We also add a multiItemReader Bean that reads multiple input files.
We finish by creating the fileDeletingTasklet Bean on which we specify the directory that needs to be cleaned.
4. Unit Test the Spring Batch Tasklet
Let’s update the existing unit test case so that we can check if our file deleting Tasklet works.
First, we copy the input files to the location from which our batch job will read. This is done in the copyFiles() method before the test case runs using the @Before annotation.
We then launch the batch job and check if it finishes successfully. We also check if all input files have been deleted.
Now run above test case. Open a command prompt and execute following Maven command:
Maven will download the needed dependencies, compile the code and run the unit test case. The result should be a successful build as shown below:
If you would like to run the above code sample you can get the full source code here.
In this tutorial, you learned what a Spring Batch Tasklet is. We also created a Tasklet example using Spring Batch, Spring Boot, and Maven.
Leave a comment if you have any questions. Or if you enjoyed this post.