Chunk-Oriented Reader-Processor-Writer Flows
Wire ItemReader, ItemProcessor, and ItemWriter components for scalable chunk processing.
Why Chunk-Oriented Processing?
Spring Batch reads, processes, and writes data in chunks instead of one record at a time. A chunk is a configurable number of items (the commit-interval) handled inside a single transaction.
- Read N items one by one with an
ItemReader. - Process each item with an
ItemProcessor. - Write the whole batch of N at once with an
ItemWriter.
Writing in bulk and committing per chunk is what makes batch jobs scale to millions of rows without exhausting memory.
The Three Core Interfaces
Every chunk step is built from three single-method interfaces. Knowing their contracts is the foundation of the whole pattern.
ItemReader<I>→I read()returns the next item, ornullwhen the input is exhausted.ItemProcessor<I, O>→O process(I item)transforms an input into an output; returningnullfilters the item out.ItemWriter<O>→void write(Chunk<? extends O> chunk)persists the accumulated chunk.
public interface ItemReader<I> {
I read() throws Exception; // null = end of input
}
public interface ItemProcessor<I, O> {
O process(I item) throws Exception; // null = filter
}
public interface ItemWriter<O> {
void write(Chunk<? extends O> chunk) throws Exception;
}All lessons in this course
- Jobs, Steps, and the JobRepository Model
- Chunk-Oriented Reader-Processor-Writer Flows
- Fault Tolerance, Skip, and Retry Policies
- Partitioning and Parallel Step Execution