Spring Batch (Reading Data from DB Table)

Spring Batch is basically used to process or automate a job which can be scheduled to run at a date and time as per the given cron schedule.In this tutorial I will explain how to create a spring batch which can read data from a database table and process it.

In my previous tutorial ,I explained about the Spring Batch and how to read data from a csv and write it to a database table.

Prerequisite

  • Maven3
  • IntelliJ

The use case is to read from the following database table:-

Partner_Program_Fee

partnerId number(13,0)

programName varchar(35)

startDate Date

endDate Date

Create a new project in IntelliJ. Project Structure:-

Create a POJO for the table called PartnerProgramFee.

Now ,we will create a mapper for reading from the database table.The mapper is used to map the data read from the sql to our Pojo class.Our custom mapper implements RowMapper which will map the rows returned from sql statement to our Pojo class.

Spring batch mapper

There are three parts to any Spring Batch:-

  • Reader
  • Processor
  • Writer

And we will declare all these three components as beans in our Batch Job configuration.

Reader:- We need a JdbcCursorItemReader to read data from the database.

We provide a native sql query in our Reader(partnerProgramFeeReader) and in the rowMapper we provide the PartnerProgramFeeMapper which we created for mapping the data returned from sql to our Pojo class.We also need to assign dataSource to the JdbcCursorItemReaderBuilder. You can use this tutorial to configure datasource in your spring batch.

Processor:- In our custom processor we implement the ItemProcessor.You can process/update the data we got from the mapper in the processor.This is where usually we have the business logic.

Writer:- Writer is used to either write data to a csv or a database table or call a service.In out tutorial we are primarily focusing on reading data from database so I am not doing anything in the writer class.But I have added the writer code to show how it looks .You create your own custom writer by implementing ItemWriter.

Now, we will create the Spring Batch Configuration to declare the reader, processor and writer as beans.There are five parts to Spring Batch Configuration:-

  • Job Builder Factory
  • Step Builder Factory
  • Data Source
  • Declaring Beans for reader, processor and writer
  • Declaring the Spring Batch Job by using the above specified components.

Here’s the code:-

Please note the annotations you need to include in your Spring Batch Configuration.

Declaring the reader, processor and writer.

Declaring the Step and the Job:-

The last step is to create a Batch Job executor to run the job.

Please note the annotation you need to add in the Batch Job Executor

Launching the job in the batch job executor:-

You can specify the cron schedular in the application property file and read it in the batch job executor.Example:-

partner.program.fee.batch.cron=0 */5 * * * ?
The above cron will execute the job every 5 minutes.

When you start the batch job the job should run every 5 minutes .

That’s all about reading from data in a Spring Batch job.

Happy Coding 🙂

Leave a comment

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