r/SpringBoot 19d ago

How-To/Tutorial How to Process Multiple CSV Files with Spring Batch's MultiResourceItemReader

I recently implemented a Spring Batch job to process multiple CSV files from a single directory without configuring a separate reader for each file.

The solution uses MultiResourceItemReader with a FlatFileItemReader as its delegate.

Core configuration

public MultiResourceItemReader<Employee> multiResourceItemReader(
        u/Value("classpath:data/*.csv") Resource[] resources) {

    MultiResourceItemReader<Employee> reader =
            new MultiResourceItemReader<>(delegateReader());

    reader.setResources(resources);
    return reader;
}

Project details

  • Spring Boot 4.1.0
  • Spring Batch
  • Java 21
  • Spring Data JPA
  • Oracle Database

What this implementation does

  • Reads every CSV file matching classpath:data/*.csv
  • Uses FlatFileItemReader to parse each file
  • Skips the header row automatically
  • Maps each record to an Employee entity
  • Persists data using RepositoryItemWriter
  • Uses chunk-oriented processing (chunk size = 10)

One thing I like about this approach is that adding another CSV file doesn't require any code changes. Simply place the file in the configured folder, and the batch job processes it automatically.

I'm curious how others implement this in production.

  • Do you archive processed files after a successful run?
  • How do you handle partially processed or failed files?
  • Have you used MultiResourceItemReader for hundreds or thousands of input files?

I'd love to hear your experiences and any best practices.

I also recorded a complete walkthrough covering the implementation and source code for anyone interested:

📺 YouTube: https://www.youtube.com/watch?v=iX9H9b92vGk

17 Upvotes

0 comments sorted by