VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/spring-boot-with-spring-batch/

⇱ Spring Boot with Spring Batch - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot with Spring Batch

Last Updated : 27 Oct, 2025

Spring Batch is a lightweight yet robust framework designed for batch processing, the automated execution of large data tasks without human intervention. It provides reusable components for logging, transaction management, job scheduling, retries and error handling.

When integrated with Spring Boot, it simplifies batch job configuration and execution, allowing developers to focus on the business logic instead of boilerplate setup.

What is Batch Processing

Batch processing refers to executing repetitive, data-intensive tasks in bulk. Typical examples include:

  • Processing large datasets
  • Database migration
  • Generating reports
  • ETL (Extract, Transform, Load) operations

Spring Batch is purpose-built for such use cases by splitting jobs into smaller, manageable steps that can run sequentially or in parallel.

Jobs, Steps and Flow

A Job in Spring Batch represents the complete batch process, while Steps define the logical phases within that job.

  • Job: Encapsulates the full batch process, consisting of multiple steps.
  • Step: Represents one stage of a job β€” typically involves reading, processing and writing data.
  • Flow: Defines the execution order of steps. You can create conditional or parallel flows (e.g., Step 2 runs only if Step 1 succeeds).

Each step operates in three distinct phases: ItemReader, ItemProcessor and ItemWriter.

Core Components of Spring Batch

1. ItemReader

Reads input data from a source such as a database, file, or message queue. It reads one record at a time and passes it to the processor.

2. ItemProcessor

Applies business logic or transformation on each item read by the reader.

3. ItemWriter

Writes the processed data to the desired output, such as a database or console.

Chunk-Oriented Processing

Spring Batch processes data in chunks, not all at once.

Each step reads and processes individual items, but commits them in groups defined by a chunk size, improving both performance and transaction management.

In this example:

  • 10 items are read and processed.
  • Once the chunk limit is reached, all 10 items are written in a single transaction.

Job Repository and Metadata

The Job Repository maintains execution metadata for jobs and steps, including:

  • JobInstance: Represents a unique execution configuration.
  • JobExecution: Tracks job runs, including status and timestamps.
  • StepExecution: Records details of each step execution.

This allows restartability (resume from failure point) and monitoring of batch executions. A relational database (e.g., MySQL, HSQLDB) typically stores this metadata.

Transaction Management and Error Handling

Spring Batch ensures transactional integrity β€” if a step fails, its changes can be rolled back.

Error Handling Strategies:

  • Retry: Automatically retry failed steps.
  • Skip: Ignore certain failed records.
  • Listeners: Run custom logic before or after steps.

Scheduling Batch Jobs

You can schedule jobs using Spring’s @Scheduled annotation or tools like Quartz.:

Example Project: Spring Boot with Spring Batch

This project reads data from a CSV file, processes it and writes it to a MySQL database.

Step 1: Create a New Spring Boot Project

Project Name: spring-batch-example
Type: Maven
Packaging: Jar
Dependencies:

  • Spring Batch
  • Spring Data JPA
  • Spring Boot DevTools
  • Lombok
  • MySQL Driver

Project Structure:

After project creation done, the folder structure will be like below:

πŸ‘ Project Folder Structure
Project Structure

Step 2: Configure application.properties

spring.application.name=spring-batch-example

# Database Configuration

spring.datasource.url=jdbc:mysql://localhost:3306/spring_batch_db?useSSL=false&serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=mypassword

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


# Hibernate Settings

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect


# Batch Settings

spring.batch.job.enabled=true

spring.batch.initialize-schema=always


server.port=8080

Step 3: Create CSV Data File

Create data.csv in src/main/resources/:

firstName,lastName
Mahesh ,Kadambala
Ravi,Teja
Lakshmi,Narayana
Praveen,Chowdary
Kiran ,Kumar
Saneep,Kumar
Akhil,Hero
Gautam,P
Madhavo ,Reddy
Suresh,Kumar
Ravi,Teja
Lakshmi,Narayana
Anusha,Reddy
Venkat,Rao
Praveen,Chowdary
Sowmya,Krishna
Kiran,Kumar
Manjula,Rao
Naveen,Prasad
Madhavi,Reddy
Srinivas,Rao
Ramya,Lakshmi
Venkatesh,Babu
Sujatha,Rani

Step 4: Create SQL Schema

Create schema.sql in src/main/resources/:

CREATE TABLE people (

id BIGINT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(255),

last_name VARCHAR(255)

);

Step 5: Spring Batch Configuration

Create BatchConfig.java to configure Spring Batch with a reader, processor and writer.

BatchConfig.java

Step 6: Model Class

Person.java

Step 7: Repository Interface

Create PersonRepository.java to access the database.

Step 8: Add Job Completion Listener

Create JobCompletionNotificationListener.java to log results after batch completion.

JobCompletionNotificationListener.java

Step 9: Processor Class

PersonItemProcessor.java

Step 11: Main Application

In this main class, add the @EnableBatchProcessing annotation to enable the functionalities of the Spring Boot project.

SpringBatchExampleApplication.java

Step 11: Run the application

Run the application and it will start on port 8080.

Spring Batch will read data from the CSV file, convert names to uppercase and insert them into the people table.

Console Logs Example:

πŸ‘ Application Runs
Output
πŸ‘ Console Logs
Output

Person Table Data in Database

πŸ‘ Person Table Data in Database
Table
Comment
Article Tags:

Explore