Spring Batch

1. What is Spring Batch?

Spring Batch is a lightweight framework for batch processing, which is processing large volumes of data in chunks. Key features:

  • Chunk-based processing
  • Transaction management
  • Retry and skip logic
  • Job scheduling
  • Integration with databases, files, queues, etc.

2. Basic Concepts

TermDescription
JobA batch process; contains one or more steps.
StepA phase of a job; can read, process, write data.
ItemReaderReads data from a source (CSV, DB, etc.).
ItemProcessorProcesses data (e.g., transforms it).
ItemWriterWrites data to a destination (DB, file, etc.).
ChunkProcessed in small sets to improve efficiency (e.g., 10 items at a time).

3. Setup Spring Boot Batch

pom.xml dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

Using H2 database for simplicity; you can replace it with MySQL, Postgres, etc.


4. Example: CSV to Database

Step 1: Create a model

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String lastName;
    private String email;

    // getters and setters
}

Step 2: CSV File (input.csv)

John,Doe,john.doe@example.com
Jane,Smith,jane.smith@example.com

Step 3: Create a Batch Configuration

@Configuration
@EnableBatchProcessing
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    // Reader
    @Bean
    public FlatFileItemReader<Person> reader() {
        FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("input.csv"));
        reader.setLineMapper(new DefaultLineMapper<>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames("firstName", "lastName", "email");
            }});
            setFieldSetMapper(fieldSet -> {
                Person p = new Person();
                p.setFirstName(fieldSet.readString("firstName"));
                p.setLastName(fieldSet.readString("lastName"));
                p.setEmail(fieldSet.readString("email"));
                return p;
            });
        }});
        return reader;
    }

    // Processor
    @Bean
    public ItemProcessor<Person, Person> processor() {
        return person -> {
            person.setEmail(person.getEmail().toLowerCase());
            return person;
        };
    }

    // Writer
    @Bean
    public JpaItemWriter<Person> writer() {
        JpaItemWriter<Person> writer = new JpaItemWriter<>();
        writer.setEntityManagerFactory(entityManagerFactory);
        return writer;
    }

    // Step
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Person, Person>chunk(5)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }

    // Job
    @Bean
    public Job importUserJob() {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .flow(step1())
                .end()
                .build();
    }
}

Step 4: Application Properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.batch.initialize-schema=always

Step 5: Run the Application

  • Spring Boot Batch will automatically run the job on startup.
  • You can check the H2 database console to see the imported Person records.

✅ This example demonstrates the classic Spring Batch pattern: read → process → write.

Leave a Reply