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.
The csv file consisting of first name,last name,department,email,age.
Database Table with the following fields:-
- Full Name
Create Table Script:-
CREATE TABLE Student (
Add the following dependencies in the pom file
Csv File to be read
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.
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.
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.
References:- Spring Batch Docs
I hope you enjoyed reading this article.Happy Coding 🙂