Steps to develop Spring Batch jobs using Spring Boot with example

Spring Batch is a lightweight batch framework which can be used by enterprise systems to develop robust batch applications for the daily operations. In this article, we will see a simple Spring Batch example.

Software used

  • Spring Boot 2.0.4.RELEASE
  • Spring Batch
  • Java 8
  • Maven
  • Eclipse

Spring Batch

Source – https://docs.spring.io/spring-batch/trunk/reference/html/domain.html

Any Spring Batch job has 2 main components, Job, and Steps. A job may have multiple steps. Each step has a compulsory reader and writer routines and an optional processor unit.

Step

A Step is a fundamental unit of any Job. It is a domain object that encapsulates an independent, sequential phase of a batch job. A Step defines necessary information to define and control the actual batch processing.

Item Reader

ItemReader is an abstract representation of how data is provided as input to a Step. When the inputs are exhausted, the ItemReader returns null.

Item Processor

ItemProcessor represents the business processing of an item. The data read by ItemReader can be passed on to ItemProcessor. In this unit, the data is transformed and sent for writing. If, while processing the item, it becomes invalid for further processing, you can return null. The nulls are not written by ItemWriter.

Item Writer

ItemWriter is the output of a Step. The writer writes one batch or chunk of items at a time to the target system. ItemWriter has no knowledge of the input it will receive next, only the item that was passed in its current invocation.

POM – Main Dependencies

For our example, we are going to use the H2 Database with an in-memory mode. Below are few properties we need to mention so that our H2 database is accessible over the web.

Data

In our code, we will read a csv file with below data

Corresponding entity is

We are using Project Lombok for our domain object.

Reader

Our Reader is a FlatFileItemReader, which expects a csv file Resource as an input. To read data from file, we have to define a tokenizer. After that, we have to map the read tokens to our entity or domain object using BeanWrapperFieldMapper.

Writer

The writer routine is straightforward. It saves all the user data to our in-memory database.

Processor

Processor checks if data is present in the table if it is present then it adds the amount else new row is inserted.

The Job

When we annotate our main application with @EnableBatchProcessing Spring Boot makes it sure that all the required beans are available for you. To create a Spring Batch Job you need JobBuilderFactory and StepBuilderFactory

You can see how we have provided the Reader, Writer, and Processo to our Step. Then this Step is added to our Job instance.

Invoking the Job

By default, Spring runs all the job as soon as it has started its context. If you want to disable that you can define below the property

As we want to run the job manually, we will define a controller and we will call the job when that end point is loaded.

Results

When the application is first started you can check the H2 console using link http://localhost:8080/h2-console

But once you invoke the job using the rest endpoint, the user table is updated and you will see data in it.

Thus our job has completed successfully.

Conclusion

In this article, we have seen briefly how we can develop Spring Batch Jobs to take care of daily operations that involve some data processing or number crunching.

I will update this article as we go to integrate the jobs with some scheduling techniques.

You can download the complete code from git repository.

Download Code

 

8 Comments
  1. daniel
    May 12, 2020 | Reply
  2. Moraes
    March 3, 2020 | Reply
    • Shilpa
      March 4, 2020 | Reply
  3. Ranjay
    September 17, 2019 | Reply
  4. Abhijit Das
    November 23, 2018 | Reply
  5. Tuan Le
    November 19, 2018 | Reply
    • Pavan
      November 22, 2018 | Reply
  6. Omkar Kelkar
    November 9, 2018 | Reply

Add a Comment

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