Readers, Processors, and Writers
Build the chunk-oriented batch flow.
Readers, Processors, and Writers is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The read-process-write triad
A chunk step has three collaborators:
- ItemReader - supplies items one at a time
- ItemProcessor - transforms or filters an item
- ItemWriter - persists a chunk of items
ItemReader interface
ItemReader.read() returns the next item, or null when the input is exhausted. Spring Batch keeps calling it until it returns null.
public interface ItemReader<T> {
T read() throws Exception;
}FlatFileItemReader
For CSV files, FlatFileItemReader reads lines and maps them to objects via a line mapper.
@Bean
public FlatFileItemReader<Person> reader() {
return new FlatFileItemReaderBuilder<Person>()
.name("personReader")
.resource(new ClassPathResource("people.csv"))
.delimited()
.names("firstName", "lastName")
.targetType(Person.class)
.build();
}JdbcCursorItemReader
To read from a database, use JdbcCursorItemReader or JdbcPagingItemReader. Paging is preferred for very large result sets to avoid holding a long cursor.
@Bean
public JdbcCursorItemReader<Person> dbReader(DataSource ds) {
return new JdbcCursorItemReaderBuilder<Person>()
.name("dbReader")
.dataSource(ds)
.sql("SELECT first_name, last_name FROM people")
.rowMapper(new PersonRowMapper())
.build();
}ItemProcessor interface
ItemProcessor transforms an input item into an output item. The input and output types can differ.
public interface ItemProcessor<I, O> {
O process(I item) throws Exception;
}Writing a processor
Here a processor uppercases names. Returning a transformed object is the common case.
public class UpperCaseProcessor implements ItemProcessor<Person, Person> {
@Override
public Person process(Person p) {
return new Person(p.getFirstName().toUpperCase(),
p.getLastName().toUpperCase());
}
}Filtering with the processor
If a processor returns null, the item is filtered out and never reaches the writer. This is how you skip records based on business rules.
public Person process(Person p) {
if (p.getLastName().isBlank()) {
return null; // drop this record
}
return p;
}ItemWriter interface
ItemWriter receives a chunk of items at once, not single items. This lets it batch the write into one efficient operation.
public interface ItemWriter<T> {
void write(Chunk<? extends T> chunk) throws Exception;
}JdbcBatchItemWriter
JdbcBatchItemWriter performs a batched SQL insert/update for the whole chunk, which is far faster than row-by-row writes.
@Bean
public JdbcBatchItemWriter<Person> writer(DataSource ds) {
return new JdbcBatchItemWriterBuilder<Person>()
.dataSource(ds)
.sql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)")
.beanMapped()
.build();
}Wiring them into a step
The step ties the triad together with a chunk size. Items flow reader -> processor -> writer in chunks.
return new StepBuilder("step1", repo)
.<Person, Person>chunk(50, tx)
.reader(reader())
.processor(new UpperCaseProcessor())
.writer(writer(ds))
.build();Custom readers and writers
You can implement the interfaces directly for custom sources (a REST API, a queue). Spring also offers ItemStream so your component can save and restore its position for restarts.
Quick Check
Verify your understanding of the triad.
Recap
You assembled a chunk pipeline:
ItemReader.read()returns items until nullItemProcessortransforms; returning null filtersItemWriterwrites a wholeChunkat once- The step binds all three with a chunk size
Frequently asked questions
Is the “Readers, Processors, and Writers” lesson free?
Yes — the full text of “Readers, Processors, and Writers” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.
What will I learn in “Readers, Processors, and Writers”?
Build the chunk-oriented batch flow. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Microservices & REST APIs?
No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Readers, Processors, and Writers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?
Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Scheduling with @Scheduled
- Spring Batch Jobs and Steps
- Readers, Processors, and Writers
- Restart and Error Handling