Job Scheduling with Spring Batch

 Spring Batch

Spring Batch is a processing framework designed for robust and parallel execution of multiple job. It follows the standard batch architecture in which a job repository takes care of scheduling and interacting with the job.

 Spring Batch Job

Spring batch job reads input data, processes the input data, and writes the processed data to the configured output.

  Spring Batch Job processing


Spring Batch job consists of the following components as shown in above image:

  • The JobLauncher, which is a simple interface is used to manage the jobs.
  • The Job represents the Batch job. Each job is comprised of one or more step.
  • The Step represents a logical task (i.e. read data from DB). Each step belongs to a single job.
  • The JobRepository is responsible for storing each Java object into its correct meta-data table for spring batch.
  • The Item-Reader reads the input data from a file or database and gives us the items.
  • The Item-Processor : it is used if any processing required for the data. It process the data and send to the ItemWriter.
  • The Item-Writer writes out (store) data of an item to the database or in output files.

The following figure define a simple batch job


Above image describe a job only, not a batch job. Let us try to visualize a batch job with following image:


Now, the above diagram is a better description of a batch job which includes multiple jobs. When all jobs are done then the batch can be declared as done.

Example Of Spring Batch

What we’ll build

We will build a job that will read a string value from an array, convert it into upper case and print on console.

This job will perform its task in one step and this step will have a reader that will read a string value from an array, a processor that will transform a string value in uppercase and a writer that will print transformed string on console.

What we’ll need

  • Java 1.8
  • Maven 3.3.9
  • Spring Boot: 1.4.0.RELEASE

1. Required dependencies

Add these dependencies


2. Create Steps: Reader, Processor and Writer

  •  we will create a class which implements a ItemReader interface. We use it to get data by read() method. This will read string from array one by one and forward to processor. If all data are consumed, then a null value will be returned by this method.


  •  Now we will make a class which imlements an interface ItemProcessor. it  is used to process the input data. We can provide output and input data type for each ItemProcessor. This will change the string in upper case.

  •  Now create a class which implements an interface ItemWriter. We use it to generate output of batch processing. The write() method get a list of items from batch processor or batch reader to process it. This will print changed string.

3. Make the application executable

Although we can make WAR file of batch processing ,but here we are making  a standalone application, in this approach we package everything in a single, executable JAR file and run as a standalone application.

In SprinpBatchApplication class, @EnableBatchProcessing is used to include Spring Batch features and provide a configuration for setting up batch jobs in a @Configuration class.

4.Spring Batch Configuration

Create a configuration file and enable Spring batch via annotations: @Configuration

5. Spring Batch Job Configuration

6.Build an executable JAR file of Batch Job

We will build a single executable JAR  that contains all the required dependencies, classes, and resources.

 We can build the JAR file with mvn clean package, and run the JAR file:


Note: The above  procedure will create a runnable JAR file, but you can also build a classic                    WAR file from here.

Leave a Reply