Spring Batch Tutorial - Architecture
Overview
In this tutorial we will be looking at Spring Batch Architecture and it's components.
Spring Batch Tutorial :
Q: What is Spring Batch?
Ans:
Spring Batch is an open source,lightweight framework for batch processing by performing a series of jobs. It allows processing of bulk data in a transactional manner and execute day to day activity with precision and speed.
Spring batch reads an input (ItemReader), processes (ItemProcessor) based on business logic, then aggregates the chunk of data output to a destination (file or database) and eventually writes (ItemWriter) it out. It's the most common way to perform a step.
The following figure shows the different components of Spring Batch and how they are connected with each other.Job
A Job is the batch process to be executed in a Spring Batch application without interruption from start to finish. This Job is further broken down into steps.
A Job is made up of many steps and each step is a READ-PROCESS-WRITE task or a single operation task (tasklet).
Step in job
Spring Batch Step is an independent part of a job. As per above Spring Batch structure diagram, each Step consist of an ItemReader, ItemProcessor (optional) and an ItemWriter. Note: A Job can have one or more steps.
ItemReader
An ItemReader reads data into a Spring Batch application from a particular source.
ItemWriter
ItemWriter writes data from the Spring Batch application to a particular destination.
ItemProcessor
After reading the input data using itemReader, ItemProcessor applies business logic on that input data and then writes to the file / database by using itemWriter.
Job Repository
Job Repository is used to persist all meta-data related to the execution of the Job.
Take a look at our suggested posts:
As shown in below diagram, black line represents Main processing flow and blue line represents the flow which persists job information.
Main processing flow
- JobLauncher is initiated from the job scheduler.
- The JobLauncher executes the job.
- Job performs the step.
- Step uses ItemReader to get input data.
- ItemProcessor is used to process input data in Step.
- ItemWriter is used by Step to output processed data.
A flow for persisting job information
- JobLauncher registers JobInstance in Database via JobRepository.
- JobLauncher registers that Job execution has begun in Database via JobRepository.
- JobStep updates miscellaneous details such as counts of I/O records and status in Database via JobRepository.
- JobLauncher registers that Job execution has successfully completed in Database via JobRepository.