skip to content

Spring Batch

Spring Batch for bulk processing: jobs and steps, chunk-oriented reading and writing, the job repository that makes runs restartable, fault tolerance, and scaling. Interviewers ask about it whenever the scenario involves a nightly file, a migration or a million-row job.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

explore

questions

148 · 6 sections

What is the ExecutionContext in Spring Batch, and what is it used for?

level: juniorimportance: must knowfreq 70%
basics
~20 s

ExecutionContext is a persisted key/value map Spring Batch attaches to a running job or step. It stores state — like how many items were read — so if the job stops and restarts, it can pick up where it left off.

open as a page

In Spring Batch, what is the difference between a JobInstance and a JobExecution?

level: juniorimportance: must knowfreq 78%
basics
~20 s

A JobInstance is a logical run of a job (job name + its identifying parameters). A JobExecution is a single attempt to run that instance. One JobInstance can have several JobExecutions if earlier attempts failed and were restarted.

open as a page

In Spring Batch, what are the Job and Step abstractions, and how do they relate?

level: juniorimportance: must knowfreq 80%
basics
~20 s

A Job is an entire batch process. A Step is one independent phase of it. A Job contains an ordered list of Steps and runs them in sequence; each Step does a unit of work.

open as a page

In Spring Batch, what is the difference between BatchStatus and ExitStatus?

level: juniorimportance: must knowfreq 55%
basics
~20 s

BatchStatus is an enum (COMPLETED, FAILED, STARTED, STOPPED...) describing a job's or step's lifecycle state. ExitStatus is a string-code object (exitCode plus description) used mainly to decide flow between steps. Both live on every JobExecution and StepExecution.

open as a page

What is a StepExecution in Spring Batch, and which metrics/counts does it record?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A StepExecution represents one attempt to run a Step. It records runtime metrics: readCount, writeCount, filterCount, commitCount, rollbackCount, and skipCount, plus status, timestamps, and its own ExecutionContext.

open as a page

What is chunk-oriented processing in a Spring Batch step, and what does the commit-interval control?

level: juniorimportance: must knowfreq 75%
basics
~20 s

A Spring Batch step reads items one by one, optionally processes each, and writes them in batches. The commit-interval (the chunk size) is how many items are grouped and written together in one transaction before committing.

open as a page

In Spring Batch, how does a FlatFileItemReader turn a line of text into a domain object? Name the pieces involved.

level: juniorimportance: must knowfreq 70%
basics
~20 s

The reader reads one raw line, then hands it to a LineMapper. The LineMapper uses a LineTokenizer to split the line into fields (a FieldSet), and a FieldSetMapper to turn those fields into a domain object.

open as a page

What is an ItemProcessor in Spring Batch, and where does it sit in a chunk-oriented step?

level: juniorimportance: must knowfreq 70%
basics
~10 s

ItemProcessor is the middle stage of a chunk step. For each item the ItemReader reads, its process() method transforms or filters it, then passes the result to the ItemWriter. It is optional.

open as a page

What is the ItemReader interface in Spring Batch and what is the contract of its read() method?

level: juniorimportance: must knowfreq 75%
basics
~10 s

ItemReader is the input side of a chunk-oriented step. Its single method read() returns one item at a time, or null when there is no more data, signalling the end of input.

open as a page

What is an ItemWriter in Spring Batch, and why does its write method take a whole Chunk instead of a single item?

level: juniorimportance: must knowfreq 75%
basics
~20 s

An ItemWriter is the step component that outputs processed data. Its write method receives a Chunk (a batch of items) so it can write many items in one operation, like a single batched SQL insert, which is faster than one call per item.

open as a page

What happens when you launch a Spring Batch job a second time with the exact same identifying job parameters, and why?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Spring Batch treats the same identifying parameters as the same JobInstance. If that instance already completed successfully, re-launching throws JobInstanceAlreadyCompleteException, so the job does not run again and duplicate work is avoided.

open as a page

What is Spring Batch's JobLauncher, and how do you use it to start a job?

level: juniorimportance: must knowfreq 70%
basics
~10 s

JobLauncher is the interface that starts a batch Job. You call jobLauncher.run(job, jobParameters). It returns a JobExecution describing that run (status, id, timestamps).

open as a page

What is Spring Batch's JobOperator, and how does it differ from JobLauncher?

level: juniorimportance: must knowfreq 50%
basics
~10 s

JobOperator is an operations-facing facade for controlling jobs — start, stop, restart, and abandon executions using simple types like job names and execution ids. JobLauncher just runs a Job you already have wired.

open as a page

What are JobParameters in Spring Batch, and how do you build them?

level: juniorimportance: must knowfreq 70%
basics
~10 s

JobParameters are typed key/value inputs passed to a job at launch (Strings, Longs, Doubles, Dates). You build them with JobParametersBuilder and pass them to JobLauncher.run(job, params).

open as a page

What is the JobRepository in Spring Batch, and what does it store?

level: juniorimportance: must knowfreq 70%
basics
~10 s

The JobRepository is the component that saves batch execution state to a database. It records which jobs and steps ran, their status, timestamps, and parameters, so a job can be tracked and restarted.

open as a page

What is a JobExecutionListener in Spring Batch, and when do its callbacks run?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A JobExecutionListener lets you run code around a job. beforeJob runs before the job starts; afterJob runs once the job finishes, whether it succeeded or failed. You register it on the job builder.

open as a page

In a Spring Batch chunk-oriented step, what does retry(Exception.class).retryLimit(n) do, and when would you use it?

level: juniorimportance: must knowfreq 70%
basics
~20 s

On a faultTolerant() step, retry(SomeException.class) marks that exception as retryable and retryLimit(n) caps the number of attempts. Spring Batch re-runs the failing work instead of failing the job. Use it for transient errors like a brief network or DB blip.

open as a page

In Spring Batch chunk-oriented processing, what happens to the current chunk's transaction when an exception is thrown while processing or writing an item?

level: juniorimportance: must knowfreq 55%
basics
~20 s

The whole chunk's transaction rolls back, so none of the items in that chunk get written. Spring Batch wraps each chunk (read-process-write of N items) in a single transaction, so one bad item undoes the entire chunk.

open as a page

What is skip handling in Spring Batch, and how do you enable it on a chunk-oriented step?

level: juniorimportance: must knowfreq 62%
basics
~10 s

Skip handling lets a step ignore individual bad items instead of failing the whole job. You enable it with faultTolerant() on the step builder, then declare which exceptions to skip and a skipLimit.

open as a page

How does StepExecutionListener work, and what is the @BeforeStep annotation commonly used for?

level: middleimportance: must knowfreq 50%
basics
~20 s

StepExecutionListener has beforeStep and afterStep callbacks that run around a single step. @BeforeStep is often put on a reader or writer method to grab the StepExecution — and through it the JobParameters — so the component can configure itself.

open as a page

What is a multithreaded step in Spring Batch, and how do you enable it?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A multithreaded step processes chunks on several threads inside one JVM instead of one. You enable it by giving the step a TaskExecutor (e.g. step.taskExecutor(...)), so each chunk runs on a separate thread from the pool.

open as a page

What is partitioning in Spring Batch, and why would you use it?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Partitioning splits a step's work into several named partitions that run in parallel — each processes a slice of the data (e.g. an ID range). It scales a long-running step by using multiple threads or machines.

open as a page

What is Remote Chunking in Spring Batch, and which part of the chunk-oriented step runs where?

level: juniorimportance: must knowfreq 45%
basics
~20 s

A scaling pattern where one master step reads items and sends chunks over messaging (like a queue) to remote worker processes. The workers do the processing and writing, then reply. Reading stays central; processing/writing scales out.

open as a page

How do you configure a split with FlowBuilder and a TaskExecutor, and what does each call do?

level: middleimportance: must knowfreq 50%
basics
~10 s

Build each branch as a Flow, then call new FlowBuilder(name).split(taskExecutor).add(flowA, flowB).build(). split() supplies the thread pool, add() registers the concurrent branches, and the resulting flow becomes a step in the job.

open as a page

How does the Partitioner interface work, and how does a worker step read its assigned slice?

level: middleimportance: must knowfreq 62%
basics
~10 s

Partitioner has one method: partition(int gridSize) returning Map<String, ExecutionContext>. Each entry is a named partition whose ExecutionContext holds that slice's parameters (e.g. minId/maxId). The worker's @StepScope reader reads them via SpEL like #{stepExecutionContext['minId']}.

open as a page

In Spring Batch, how do you make a job run a different step depending on whether the previous step succeeded or failed?

level: juniorimportance: must knowfreq 55%
basics
~10 s

Use conditional transitions on the job builder: .start(stepA).on("FAILED").to(cleanupStep).from(stepA).on("*").to(nextStep).end(). The on(...) pattern is matched against the previous step's ExitStatus to pick the next step.

open as a page

What is a JobExecutionDecider in Spring Batch and why would you use one instead of relying on a step's ExitStatus?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A JobExecutionDecider is a small class whose decide() method returns a status that tells the job which path to take next. You use it when the choice depends on custom logic, not just whether a step passed or failed.

open as a page

Explain the difference between BatchStatus and ExitStatus, and how you customize ExitStatus to drive a conditional transition.

level: middleimportance: must knowfreq 50%
basics
~20 s

BatchStatus is the framework's enum (COMPLETED/FAILED/STOPPED) deciding success and restart. ExitStatus is a customizable string that flow patterns (on(...)) match on. Override ExitStatus in a StepExecutionListener's afterStep to return custom codes like "NO_DATA" and branch on them.

open as a page

Show how you wire a JobExecutionDecider into a job's flow with .next(decider).on(...).to(...), including branching to multiple paths.

level: middleimportance: must knowfreq 50%
basics
~10 s

After a step, call .next(decider), then chain .on("NAME").to(step) for each possible status. Use .from(decider).on("OTHER").to(otherStep) to add more branches, and finish with .end().build().

open as a page

Compare FlowStep and JobStep: what is the key difference, and how do you choose between them?

level: middleimportance: must knowfreq 48%
basics
~20 s

FlowStep wraps a Flow and runs its steps inside the current job execution. JobStep wraps a whole Job and launches it as a separate execution with its own parameters and identity. Use FlowStep to reuse steps; use JobStep to nest an independent job.

open as a page