BackendBench
Coffee

Unravel › Java concepts › Scheduling and batch

Spring

Spring Batch jobs, steps and chunks

Spring Batch runs big, restartable jobs: read many records, process each, write them in chunks inside transactions, and remember how far it got so a failed run can resume.

Learn these first

At a glance

Mental model

A night shift sorting parcels.

What happens, in order
  1. 1A Job consists of StepsA chunk-oriented step has an ItemReader (returns one item or null at the end), an optional ItemProcessor (transforms or filters) and an ItemWriter (receives a list of items).
  2. 2chunk(100, transactionManager) means: read and process 100 items, write them, commitA failure rolls back only the current chunk.
  3. 3A Tasklet step does one custom action (delete a folder, call a stored procedure) instead of read/process/write
  4. 4Each launch has JobParameters; the same parameters identify the same job instanceThe JobRepository stores execution status and counters in database tables.
Remember
Watch out
Chunk size so large it exhausts memory or holds locksNon-restartable custom readersStarting a job with the same parameters and being surprised it does not run againSwallowing errors inside the writer
Example (highlighted lines explain it)
@Beanpublic Step importStep(JobRepository repo, PlatformTransactionManager tx,                       ItemReader<CsvOrder> reader, ItemProcessor<CsvOrder, Order> processor,                       ItemWriter<Order> writer) {    return new StepBuilder("importOrders", repo)        .<CsvOrder, Order>chunk(100, tx)        .reader(reader)        .processor(processor)        .writer(writer)        .faultTolerant()        .skipLimit(10)        .skip(FlatFileParseException.class)        .build();}
  1. line 2 uses JobRepository, the core of Spring Batch jobs, steps and chunks
  2. line 3 uses ItemReader, the core of Spring Batch jobs, steps and chunks
  3. line 4 uses ItemWriter, the core of Spring Batch jobs, steps and chunks
  4. line 5 uses StepBuilder, the core of Spring Batch jobs, steps and chunks
Read the full explanation (about 2 min)

Spring Batch runs big, restartable jobs: read many records, process each, write them in chunks inside transactions, and remember how far it got so a failed run can resume.

A mental model

A night shift sorting parcels. The belt delivers items (reader), a worker stamps each one (processor), and boxes are sealed 100 at a time (chunk). The supervisor keeps a log, so after a power cut the shift resumes at box 37, not box 1.

How it works

A Job consists of Steps. A chunk-oriented step has an ItemReader (returns one item or null at the end), an optional ItemProcessor (transforms or filters) and an ItemWriter (receives a list of items).

chunk(100, transactionManager) means: read and process 100 items, write them, commit. A failure rolls back only the current chunk.

A Tasklet step does one custom action (delete a folder, call a stored procedure) instead of read/process/write.

Each launch has JobParameters; the same parameters identify the same job instance. The JobRepository stores execution status and counters in database tables.

Going deeper

Fault tolerance is configured per step: skip(ParseException.class).skipLimit(10) continues past bad rows, retry(DeadlockLoserDataAccessException.class) retries transient errors, and listeners record what was skipped.

Readers must be restartable. Cursor readers keep the position in the execution context; a reader that holds in-memory state without saving it cannot resume correctly. Multi-threaded steps need thread-safe readers.

Run order and idempotency matter: if the same job can be started twice, writers should use upserts or the job should refuse the duplicate by parameters.

Under the hood

The step loop is a TaskletStep driven by a repeat template. For each chunk it opens a transaction, calls read() until the chunk size or end of data, runs the processor, calls write(items), updates the step execution row in the same transaction and commits, which is how progress and data stay consistent.

Common mistakes

  • Chunk size so large it exhausts memory or holds locks.
  • Non-restartable custom readers.
  • Starting a job with the same parameters and being surprised it does not run again.
  • Swallowing errors inside the writer.

How Unravel recognises it

When you paste code, Unravel looks for these signs of spring batch jobs, steps and chunks. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Spring Batch jobs, steps and chunks?

Spring Batch runs big, restartable jobs: read many records, process each, write them in chunks inside transactions, and remember how far it got so a failed run can resume.

What are the most common mistakes with Spring Batch jobs, steps and chunks?

Chunk size so large it exhausts memory or holds locks. Non-restartable custom readers. Starting a job with the same parameters and being surprised it does not run again. Swallowing errors inside the writer.

How can I see Spring Batch jobs, steps and chunks in real code?

Paste your own Java into Unravel and it will point out where spring batch jobs, steps and chunks appears, explain how it works at the detail level you choose, and draw the flow of the code around it.