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 pageshowhide
explore
- Batch Domain Model25 questions
- Job & Step Abstractions5 questions
- JobInstance vs JobExecution5 questions
- StepExecution & Counts5 questions
- ExecutionContext5 questions
- BatchStatus & ExitStatus5 questions
- Step Processing & Item Flow34 questions
- Chunk-Oriented Processing4 questions
- ItemReader Implementations5 questions
- ItemProcessor5 questions
- ItemWriter5 questions
- Tasklet Steps5 questions
- Read-Process-Write Transaction Boundary5 questions
- Flat-File Mapping & Tokenizing5 questions
- Launching, Parameters & Metadata34 questions
- JobLauncher5 questions
- JobParameters & Incrementer5 questions
- JobRepository & Metadata Schema5 questions
- Restartability5 questions
- Idempotency & Duplicate Runs4 questions
- JobOperator5 questions
- @StepScope / @JobScope & Late Binding5 questions
- Fault Tolerance20 questions
- Skip Handling5 questions
- Retry5 questions
- Listeners5 questions
- Rollback Semantics5 questions
- Scaling & Parallel Processing20 questions
- Multithreaded Step5 questions
- Partitioning5 questions
- Remote Chunking5 questions
- Parallel Steps (Split)5 questions
- Flow Control15 questions
- Conditional Flow Transitions5 questions
- JobExecutionDecider5 questions
- Flows & Nested Jobs5 questions
questions
148 · 6 sectionsWhat is the ExecutionContext in Spring Batch, and what is it used for?
basics
~20 sExecutionContext 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.
In Spring Batch, what is the difference between a JobInstance and a JobExecution?
basics
~20 sA 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.
In Spring Batch, what are the Job and Step abstractions, and how do they relate?
basics
~20 sA 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.
In Spring Batch, what is the difference between BatchStatus and ExitStatus?
basics
~20 sBatchStatus 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.
What is a StepExecution in Spring Batch, and which metrics/counts does it record?
basics
~10 sA 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.
What is chunk-oriented processing in a Spring Batch step, and what does the commit-interval control?
basics
~20 sA 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.
In Spring Batch, how does a FlatFileItemReader turn a line of text into a domain object? Name the pieces involved.
basics
~20 sThe 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.
What is an ItemProcessor in Spring Batch, and where does it sit in a chunk-oriented step?
basics
~10 sItemProcessor 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.
What is the ItemReader interface in Spring Batch and what is the contract of its read() method?
basics
~10 sItemReader 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.
What is an ItemWriter in Spring Batch, and why does its write method take a whole Chunk instead of a single item?
basics
~20 sAn 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.
What happens when you launch a Spring Batch job a second time with the exact same identifying job parameters, and why?
basics
~10 sSpring 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.
What is Spring Batch's JobLauncher, and how do you use it to start a job?
basics
~10 sJobLauncher is the interface that starts a batch Job. You call jobLauncher.run(job, jobParameters). It returns a JobExecution describing that run (status, id, timestamps).
What is Spring Batch's JobOperator, and how does it differ from JobLauncher?
basics
~10 sJobOperator 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.
What are JobParameters in Spring Batch, and how do you build them?
basics
~10 sJobParameters 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).
What is the JobRepository in Spring Batch, and what does it store?
basics
~10 sThe 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.
What is a JobExecutionListener in Spring Batch, and when do its callbacks run?
basics
~10 sA 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.
In a Spring Batch chunk-oriented step, what does retry(Exception.class).retryLimit(n) do, and when would you use it?
basics
~20 sOn 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.
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?
basics
~20 sThe 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.
What is skip handling in Spring Batch, and how do you enable it on a chunk-oriented step?
basics
~10 sSkip 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.
How does StepExecutionListener work, and what is the @BeforeStep annotation commonly used for?
basics
~20 sStepExecutionListener 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.
What is a multithreaded step in Spring Batch, and how do you enable it?
basics
~20 sA 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.
What is partitioning in Spring Batch, and why would you use it?
basics
~20 sPartitioning 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.
What is Remote Chunking in Spring Batch, and which part of the chunk-oriented step runs where?
basics
~20 sA 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.
How do you configure a split with FlowBuilder and a TaskExecutor, and what does each call do?
basics
~10 sBuild 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.
How does the Partitioner interface work, and how does a worker step read its assigned slice?
basics
~10 sPartitioner 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']}.
In Spring Batch, how do you make a job run a different step depending on whether the previous step succeeded or failed?
basics
~10 sUse 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.
What is a JobExecutionDecider in Spring Batch and why would you use one instead of relying on a step's ExitStatus?
basics
~20 sA 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.
Explain the difference between BatchStatus and ExitStatus, and how you customize ExitStatus to drive a conditional transition.
basics
~20 sBatchStatus 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.
Show how you wire a JobExecutionDecider into a job's flow with .next(decider).on(...).to(...), including branching to multiple paths.
basics
~10 sAfter 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().
Compare FlowStep and JobStep: what is the key difference, and how do you choose between them?
basics
~20 sFlowStep 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.