Skip to content
AstralSurge Learnings
Follow LinkedIn X RSS

Spring Batch 5 Architecture & Processing Guide (Spring Boot 3)

Spring Batch 5 (shipped with Spring Boot 3) is a robust framework engineered for enterprise batch processing. It processes data in chunks, maintains execution checkpoints in metadata repositories, and supports automated retries and skips.

Key Modernization in Spring Batch 5

Spring Batch 5 completely removed legacy factory classes like JobBuilderFactory and StepBuilderFactory. Builders must now be instantiated directly with an explicit JobRepository and PlatformTransactionManager.

Complete Production Example: CSV Ingestion Pipeline

package com.astralsurge.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.transaction.PlatformTransactionManager;

public record Employee(int id, String name, String email, double salary) {}

@Configuration
public class EmployeeBatchConfig {

    @Bean
    public FlatFileItemReader<Employee> reader() {
        return new FlatFileItemReaderBuilder<Employee>()
            .name("employeeItemReader")
            .resource(new ClassPathResource("employees.csv"))
            .delimited()
            .names("id", "name", "email", "salary")
            .targetType(Employee.class)
            .build();
    }

    @Bean
    public ItemProcessor<Employee, Employee> processor() {
        return employee -> new Employee(
            employee.id(),
            employee.name().toUpperCase(),
            employee.email().toLowerCase(),
            Math.round(employee.salary() * 1.10 * 100.0) / 100.0 // 10% raise
        );
    }

    @Bean
    public FlatFileItemWriter<Employee> writer() {
        return new FlatFileItemWriterBuilder<Employee>()
            .name("employeeItemWriter")
            .resource(new FileSystemResource("output/processed_employees.csv"))
            .delimited()
            .delimiter(",")
            .names("id", "name", "email", "salary")
            .build();
    }

    @Bean
    public Step processEmployeesStep(JobRepository jobRepository,
                                     PlatformTransactionManager transactionManager) {
        return new StepBuilder("processEmployeesStep", jobRepository)
            .<Employee, Employee>chunk(50, transactionManager)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .build();
    }

    @Bean
    public Job employeeJob(JobRepository jobRepository, Step processEmployeesStep) {
        return new JobBuilder("employeeProcessingJob", jobRepository)
            .start(processEmployeesStep)
            .build();
    }
}

0 Comments