How to create a Batch Job using Spring Batch

Batch processing means executing a series of jobs and Spring provides an open source framework for batch processing. Spring provides a list of classes /API to read/write resources , job processing ,job restart and partitioning techniques to high volumes of data.

Spring Batch Components

Spring Batch consists of the following component:-

Job :- It represents the Spring Batch Job .Each job can have one or more steps.

Step:- Each step consists of a task which needs to be done.For instance reading from a csv file.

ItemReader:- Each step typically has one ItemReader.It reads input data (that is a file ) and provides the data sequentially ,one by one.For instance, it reads the data from a csv file and provides the records in the file as a list.

ItemProcessor :- ItemProcessor modifies/transforms the data ,one item at a time.Each step can have one ItemProcessor.

ItemWriter:- ItemWriter writes the data ,one item at a item.For instance, it writes the data read by item reader from a csv file to the database.Each step typically has one ItemWriter.

JobRepository:- It stores details of the batch job.It basically stores metadata about configured and executed jobs.It provides CRUd operations for JobLauncher, Job and Step instantiations.

JobLauncher:- Job is executed by the JobLauncher

JobInstance:- Each job may be associated with multiple job instances , each of the instance is uniquely identified by Job Parameters.

JobExecution:- Each run of a JobInstance is referred to JobExecution.It basically keep track of the job status ,start and end times etc.

Now that we are familiar with the components in a Spring batch,I will show you how to create a spring batch job .

Our use case is to read from a csv file, process the data in the file and write the data to the database.

Prerequisites

The csv file consisting of first name,last name,department,email,age.

Database Table with the following fields:-

  • Full Name
  • Email
  • Department

Create Table Script:-

CREATE TABLE Student (
studentId Number(13,0),
department varchar(250),
email varchar(250),
fullName varchar(250)
);

Project Structure

Maven dependencies

Add the following dependencies in the pom file

Csv File to be read

Creating ItemReader

In the reader we define the columns that are to be read from the csv file. It basically defines the mapping of the csv file to a domain object.In this case the columns read are mapped to the Student Object.

Creating ItemProcessor

In the ItemProcessor I am concatenating the first name and last name and creating a full name .

So, the csv file is read by the ItemReader and mapped to the Student Object.The processor modifies or processes the data.It basically contains any business logic to modify the data.

Creating ItemWriter

Dao File to save the data in the database

The database details are provided in the property files.

Creating Batch Job Configuration

Executing the Spring batch job

To execute the Spring Batch Job ,we are using the JobLauncher .It takes the job to be executed and the job parameters associated with them.We can schedule the job to run at a specific time interval based on a cron job through @Scheduled annotation.

In this case the cron entry is read from the property file.

After the job is executed the csv file is read and entries are made in the database.

I hope you enjoyed reading this article.Happy Coding 🙂

Published by Anna

Just a simple girl who loves to code. .I am primarily a Java developer by profession and I am here to share my love for coding with you all I hope you enjoy reading my blog. Happy Coding Guys :-)

Leave a comment

Your email address will not be published. Required fields are marked *