Spring Batch with Example

Spring Batch is a lightweight framework for development of batch applications.

A batch job consists of multiple Steps. Each Step has ItemReader, ItemProcessor, and ItemWriter.

By default, Spring Batch executes all configured jobs at startup. We can disable job execution at startup by adding the following property to application.properties:

spring.batch.job.enabled=false

Project Structure

ps

pom.xml

Screen Shot 2020-02-15 at 10.45.45 AM

Student – The Entity class

Screen Shot 2020-02-15 at 9.37.22 AM

StudentRepository – Spring Data JPA Repository

Screen Shot 2020-02-15 at 10.21.10 AM

StudentItemReaderItemReader provides the data

Screen Shot 2020-02-15 at 10.22.20 AM

StudentItemProcessorItemProcessor transforms input items

Screen Shot 2020-02-15 at 10.22.35 AM

StudentItemWriter – ItemWriter

Screen Shot 2020-02-15 at 10.22.54 AM

database.properties

Screen Shot 2020-02-13 at 6.39.49 PM

application.properties

Screen Shot 2020-02-15 at 10.23.25 AM

AppConfig

The @EnableBatchProcessing annotation enables Spring Batch features and provides a base configuration for setting up batch jobs.

@StepScope – Spring container instantiates a new instance for each step execution.

Screen Shot 2020-02-15 at 10.14.51 AMScreen Shot 2020-02-15 at 10.15.11 AM

Application

Screen Shot 2020-02-15 at 10.20.30 AM

Console Output

Reading Students from DB:[Student [id=1, name=gyan, age=18, weight=70], Student [id=2, name=rochit, age=16, weight=55], Student [id=8, name=ravi, age=17, weight=65], Student [id=9, name=TEJA, age=14, weight=58], Student [id=11, name=VIKU, age=16, weight=44], Student [id=12, name=Ajay, age=16, weight=44], Student [id=15, name=Ramu, age=18, weight=47], Student [id=20, name=Sachin, age=14, weight=50], Student [id=21, name=Ramesh, age=14, weight=50], Student [id=23, name=Prabhakar, age=14, weight=50], Student [id=30, name=Mantu, age=15, weight=50]]
Processing Student:Student [id=1, name=gyan, age=18, weight=70]
Processing Student:Student [id=2, name=rochit, age=16, weight=55]
Processing Student:Student [id=8, name=ravi, age=17, weight=65]
Writing students to console.
GYAN:1:18:70
ROCHIT:2:16:55
RAVI:8:17:65
Processing Student:Student [id=9, name=TEJA, age=14, weight=58]
Processing Student:Student [id=11, name=VIKU, age=16, weight=44]
Processing Student:Student [id=12, name=Ajay, age=16, weight=44]
Writing students to console.
TEJA:9:14:58
VIKU:11:16:44
AJAY:12:16:44
Processing Student:Student [id=15, name=Ramu, age=18, weight=47]
Processing Student:Student [id=20, name=Sachin, age=14, weight=50]
Processing Student:Student [id=21, name=Ramesh, age=14, weight=50]
Writing students to console.
RAMU:15:18:47
SACHIN:20:14:50
RAMESH:21:14:50
Processing Student:Student [id=23, name=Prabhakar, age=14, weight=50]
Processing Student:Student [id=30, name=Mantu, age=15, weight=50]
Writing students to console.
PRABHAKAR:23:14:50
MANTU:30:15:50

Leave a Reply

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