Spring Boot Components Hub

← Back to Home

Spring Boot Batch

Spring Boot Batch provides a comprehensive framework for processing large volumes of data in batch jobs. It offers features like chunk-oriented processing, job scheduling, transaction management, and restart capabilities, making it ideal for ETL operations, report generation, and bulk data processing.

Key Features

Chunk Processing

Process data in chunks for efficient memory usage and transaction management.

Job Scheduling

Schedule batch jobs using cron expressions or fixed intervals.

Job Restart

Resume failed jobs from the last successful checkpoint.

Transaction Management

Automatic transaction management for each chunk with rollback capabilities.

Use Cases

  • ETL Operations: Extract, transform, and load data between systems
  • Report Generation: Generating large reports from databases
  • Data Migration: Migrating data between databases or systems
  • Bulk Processing: Processing large volumes of records efficiently
  • Scheduled Jobs: Running periodic data processing tasks

Example Usage

Define a batch job configuration:

@Configuration
@EnableBatchProcessing
public class BatchConfig {
    
    @Bean
    public Job importUserJob(JobRepository jobRepository, 
                            Step step) {
        return new JobBuilder("importUserJob", jobRepository)
            .start(step)
            .build();
    }
    
    @Bean
    public Step step(JobRepository jobRepository,
                    PlatformTransactionManager transactionManager,
                    ItemReader<User> reader,
                    ItemProcessor<User, User> processor,
                    ItemWriter<User> writer) {
        return new StepBuilder("step1", jobRepository)
            .<User, User>chunk(10, transactionManager)
            .reader(reader)
            .processor(processor)
            .writer(writer)
            .build();
    }
}

Dependencies

Add the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

Configuration

Configure batch job database in application.properties:

spring.batch.job.enabled=false
spring.batch.initialize-schema=always
spring.datasource.url=jdbc:h2:mem:batchdb