Spring Batch Tasklet with Spring Boot, easy in 10 minutes

Today, I show you how to create a Spring Batch Tasklet with Spring Boot Example. This post introduces very clear to help you understand then you can create your own example. You know what, Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. Let’s dive into it.

Spring Batch Tasklet with Spring Boot

Project Setup

Tools and framework that we use:
Spring Boot 2.2.2 RELEASE
Spring Batch 4.2.1
Maven 3.6
Java 8

Project Directory

Our project will have a structure like below

Project Dependencies

We’re using the dependencies like below

Creating the Model

Here, we assume that we need to read employee information from CSV files named employees1.csv and employees2.csv, its format looks like below:

Note: This file is located in the project folder/resources/csv/

Then, we create a POJO class that its object map with data in the above CSV file

Configuring Spring Batch Jobs

We assume that we have the data CSV files that contain a list of employees, its format looks like below.
Create BatchConfig class and add the @EnableBatchProcessing annotation for this class to get the support of Spring Batch features.

Create Spring Batch job that named filterJob for our own Spring Batch Tasklet application.

Let’s dive into the above code:

The method filterJob returns an instance of Jobbean. That Jobbean may be likely understood is a container of steps and other configuration options.

The methodfilterStep returns an instance of Stepbean. This bean encapsulates phases of a batch job. Here, we have a step named filterStep that takes care of reading and writing the CSV file.

We use the FlatFileItemReader to read the CSV file, its instance is created by the FlatFileItemReaderBuilder implementation. In order to FlatFileItemReader processes your CSV file, you must provide the information of file format, e.g: java mapping class, matched fields.

The method multiItemReaderreads multiple input files.

The EmployeeItemProcessor handles the processing of the data.

To write file we use FlatFileItemWriter that its instance created by a FlatFileItemWriterBuilder builder implementation.

We use the FileDeletingTasklet bean to clean the input folder test-inputs.

Processing the Data

In most cases, we need to make some data processing from our original data. So we can create EmployeeItemProcessor that implements ItemProcessor interface to take care of that task. Let’s have a look at below implementation:

Creating a Spring Batch Tasklet

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. Here, we create a tasklet that will delete all files a directory.

Configuring SpringBootApplication class

To configure Spring Boot application we create simple class below. If you haven’t had knowledge about Spring Boot yet. Let’s checkout the post Spring Boot Tutorial for Beginners

So far, we have finished for implement our Spring Batch Tasklet with Spring Boot Example.

Running Application

To demo our Spring Batch Tasklet with Spring Boot Example, we run the below command in the console.

Note: You must make sure your CSV files already stored in the input folder test-inputs.
While running that command, you will see the output like below

Spring Batch Tasklet with Spring Boot

That’s all about Spring Batch Tasklet with Spring Boot.

If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.


Spring Boot Tutorial for Beginners

Spring Batch Frameworks

Download the complete source code

spring-batch-tasklet-example.zip (169 downloads)
Please share it if you found this useful
Hide Buttons