When working with batch processing in Spring, it’s quite common to need to read multiple input files, process each line individually, and handle the output. Spring Batch offers everything you need to handle this efficiently with tools like MultiResourceItemReader
and FlatFileItemReader
.
This guide shows you how to set up a simple batch job that:
- Reads multiple files
- Processes each line
- Outputs the result (e.g., to the console)
📦 Step 1: Add Spring Batch Dependencies
Make sure your pom.xml
includes the following dependencies:
xml
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
⚙️ Step 2: Batch Configuration
Here’s a basic setup for the batch configuration class:
java
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
public Job job(JobBuilderFactory jobBuilderFactory, Step step) {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.start(step)
.build();
}
@Bean
public Step step(StepBuilderFactory stepBuilderFactory,
MultiResourceItemReader<String> reader,
ItemProcessor<String, String> processor,
ItemWriter<String> writer) {
return stepBuilderFactory.get("step")
.<String, String>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
public MultiResourceItemReader<String> multiResourceItemReader() {
Resource[] resources = new Resource[] {
new FileSystemResource("file1.txt"),
new FileSystemResource("file2.txt")
};
return new MultiResourceItemReaderBuilder<String>()
.name("multiResourceReader")
.resources(resources)
.delegate(fileItemReader())
.build();
}
@Bean
public FlatFileItemReader<String> fileItemReader() {
return new FlatFileItemReaderBuilder<String>()
.name("fileItemReader")
.lineMapper(new DefaultLineMapper<String>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames("line");
}});
setFieldSetMapper(fieldSet -> fieldSet.readString(0));
}})
.build();
}
@Bean
public ItemProcessor<String, String> itemProcessor() {
return item -> {
// Example transformation: convert to uppercase
return item.toUpperCase();
};
}
@Bean
public ItemWriter<String> itemWriter() {
return items -> items.forEach(System.out::println);
}
@Bean
public PlatformTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}
}
🚀 Step 3: Run the Batch Job
Here’s how to create a Spring Boot application that launches the job automatically:
java
@SpringBootApplication
public class BatchApplication {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
@PostConstruct
public void runJob() throws Exception {
jobLauncher.run(job, new JobParameters());
}
}
📌 What’s Happening Here?
- MultiResourceItemReader: Reads multiple files (e.g.,
file1.txt
, file2.txt
) - FlatFileItemReader: Parses each line as a string
- ItemProcessor: Transforms each line (e.g., converts to uppercase)
- ItemWriter: Outputs the result (prints to the console in this case)
✅ Summary
Spring Batch makes it easy to work with multiple files in a structured, maintainable way:
- 🔁 Use
MultiResourceItemReader
to handle multiple input files - 🧾 Use
FlatFileItemReader
to read individual lines - 🔧 Apply transformation logic with
ItemProcessor
- 🖨 Output processed lines using
ItemWriter
This setup is highly customizable—whether you're ingesting CSV files, processing logs, or transforming flat data.