Quick revision
The short answer
Spring Batch offers listeners at every level of a run: JobExecutionListener around the whole job, StepExecutionListener around a step, ChunkListener around each chunk, ItemReadListener, ItemProcessListener and ItemWriteListener around single items, and SkipListener for items that were skipped. We register a listener on a step with .listener(...) on the StepBuilder, or write a plain class with annotations such as @BeforeStep and register that.
What to remember
In this answer, we saw that Spring Batch listeners mirror the shape of a run: JobExecutionListener around the job for notifications, cleanup and summaries, StepExecutionListener around each step for parameters and exit statuses, ChunkListener and the item listeners for progress and failures of single items, and SkipListener for records a fault-tolerant step skipped. We registered them with .listener(...) on the step and job builders, saw the annotation style and the automatic registration of readers and writers, and noted that listeners must stay cheap because they run inside the job.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Batch offers listeners at every level of a run: JobExecutionListener around the whole job, StepExecutionListener around a step, ChunkListener around each chunk, ItemReadListener, ItemProcessListener and ItemWriteListener around single items, and SkipListener for items that were skipped. We register a listener on a step with .listener(...) on the StepBuilder, or write a plain class with annotations such as @BeforeStep and register that.
Why this question is asked
A batch job that only reads, processes and writes is rarely enough in real work. Somebody wants an email when the nightly import finishes, a summary of how many rows were loaded, a file of the records that were rejected, or a step that fails when too much data is bad. Listeners are the place for all of that, because they let us add behaviour around the job without mixing it into the reader or writer. Interviewers ask this question to see whether we know which hook fires when, and whether we pick the right one instead of putting everything in afterJob. In this answer, we will recap the structure of a Spring Batch run, go through each listener type with what it is good for, register a listener on a step in code, look at the annotation style, and finish with a few traps.
1A Quick Recap of How a Batch Run Is Built
A Spring Batch job is made of steps that run one after another. Each run of a job is recorded as a JobExecution, and each run of a step inside it as a StepExecution. These objects carry the status, the start and end times, and counters such as how many items were read, written and skipped.
Most steps are chunk-oriented. A chunk step reads items one at a time, passes each one through a processor, collects them until it has a chunk of, say, 100 items, and then writes the whole chunk in one transaction. After the commit it starts the next chunk. So a single step contains many chunks, and each chunk contains many items.
Listeners follow exactly this nesting. There is a listener for the job, one for the step, one for the chunk, and one for each phase of an item. Knowing the nesting is the easiest way to remember them.
2The Listener Types and What Each One Is For
JobExecutionListener has beforeJob(JobExecution) and afterJob(JobExecution). It wraps the whole run, so it is the natural place for work that happens once: sending a notification when the job ends, cleaning up temporary tables or files, and writing a summary report from the counters of all steps. afterJob runs whether the job succeeded or failed, so we check jobExecution.getStatus() inside it.
StepExecutionListener has beforeStep(StepExecution) and afterStep(StepExecution). beforeStep is a good place to read job parameters or values from the execution context before the first item is read. afterStep returns an ExitStatus, which lets us change how the step is reported based on its results, for example marking it with a custom code when too many records were rejected.
ChunkListener has beforeChunk, afterChunk and afterChunkError, each receiving a ChunkContext in Spring Batch 5 (Spring Batch 6 adds variants that receive the chunk itself, so check your version). We use it to log progress every chunk or to measure how long each commit takes. ItemReadListener, ItemProcessListener and ItemWriteListener go one level deeper, with before, after and on-error methods for each read, process and write. They are useful for logging the exact item that failed, but they run for every item, so they must be cheap.
SkipListener has onSkipInRead, onSkipInProcess and onSkipInWrite. It is only called when the step is fault tolerant and an item is skipped because of an exception we declared skippable. We use it to log skipped records or store them in an error table so someone can fix and reload them.
| Listener | Methods | Typical use |
|---|---|---|
JobExecutionListener | beforeJob, afterJob | notify, clean up, write a run summary |
StepExecutionListener | beforeStep, afterStep | read parameters, change the ExitStatus |
ChunkListener | beforeChunk, afterChunk, afterChunkError | progress logs, commit timing |
ItemReadListener | beforeRead, afterRead, onReadError | log the input that failed to parse |
ItemProcessListener | beforeProcess, afterProcess, onProcessError | trace one item through the processor |
ItemWriteListener | beforeWrite, afterWrite, onWriteError | log the chunk that failed to write |
SkipListener | onSkipInRead, onSkipInProcess, onSkipInWrite | store skipped items in an error table |
3Registering a Listener on a Step
Here is a step listener that fails the step's report when more than a tenth of the records were filtered out as invalid, and a skip listener that saves skipped lines. Both are registered with .listener(...) on the step builder.
Notice that the skip listener is only useful because we called faultTolerant() and declared which exceptions are skippable. Without that, the first bad line fails the step and no skip ever happens. In Spring Batch 6 the chunk(100, txManager) form is deprecated in favour of passing the transaction manager separately, so check your version for the exact builder calls. A job listener is registered the same way on the JobBuilder: new JobBuilder("importJob", jobRepository).listener(jobListener).start(load).build().
4The Annotation Style
Instead of implementing an interface, we can write a plain class and mark methods with annotations: @BeforeJob, @AfterJob, @BeforeStep, @AfterStep, @BeforeChunk, @AfterChunk, @OnSkipInRead, @OnSkipInProcess, @OnSkipInWrite and the item-level ones such as @OnReadError. Spring Batch wraps such an object in a listener adapter when we pass it to .listener(Object).
This style is handy when one class needs a few hooks from different interfaces. There is also a convenient shortcut: if the reader, processor or writer of a chunk step itself implements a listener interface, the step builder registers it automatically. A reader that implements StepExecutionListener can read job parameters in beforeStep without any extra registration.
5Traps to Avoid
Listeners run inside the batch run, so a slow listener slows the job and a listener that throws can change the outcome. Item-level listeners run thousands or millions of times, so a database call in afterRead can double the run time. The order matters too: when several step listeners are registered, the before methods run in registration order and the after methods in reverse order, like nested try blocks.
The other trap is putting logic in the wrong level. A report written in afterStep only knows about that step; a report of the whole run belongs in afterJob. A rule that should stop the job belongs in afterStep with an exit status that the job flow routes on, not in afterJob, which runs after every step has already finished.
6Follow-up questions
Is afterJob called when the job fails?
Yes. afterJob is called after the job finishes whatever the outcome, so it must check jobExecution.getStatus() to tell COMPLETED from FAILED or STOPPED. That makes it the right place for a notification that says either 'import finished' or 'import failed', with the failure exceptions taken from jobExecution.getAllFailureExceptions().
When is onSkipInWrite called, and why not immediately?
Skip callbacks are deferred until the chunk is about to commit. When a write fails, Spring Batch rolls back the chunk and retries the items one by one to find the bad one, so it only knows for certain which item was skipped later. Calling the listener at commit time means it is never told about an item that was later written successfully.
Can one class implement several listener interfaces?
Yes. A class can implement StepExecutionListener and ChunkListener together, or use several annotations, and a single .listener(...) call registers it for all the interfaces it implements. This is common for a monitoring listener that tracks both the step and each chunk.
7Conclusion
In this answer, we saw that Spring Batch listeners mirror the shape of a run: JobExecutionListener around the job for notifications, cleanup and summaries, StepExecutionListener around each step for parameters and exit statuses, ChunkListener and the item listeners for progress and failures of single items, and SkipListener for records a fault-tolerant step skipped. We registered them with .listener(...) on the step and job builders, saw the annotation style and the automatic registration of readers and writers, and noted that listeners must stay cheap because they run inside the job.
Open with
Spring Batch has listeners for each level of a run. JobExecutionListener with beforeJob and afterJob wraps the whole job, so I use it for notifications, cleanup and a summary report; afterJob runs on success and failure, so I check the status.
Then, if they want more
StepExecutionListener with beforeStep and afterStep wraps each step; beforeStep is where I read parameters, and afterStep returns an ExitStatus, so I can mark a step with a custom code when, for example, too many records were invalid, and route the job flow on that code.
Inside a chunk step there is ChunkListener, called before and after each chunk and on a chunk error, and ItemReadListener, ItemProcessListener and ItemWriteListener for each item, which I keep very light because they run per item. SkipListener tells me about items skipped in read, process or write in a fault-tolerant step, and I usually store those in an error table.
To register one, I call listener on the step builder: new StepBuilder with the name and job repository, chunk of 100 with the transaction manager, reader, processor, writer, then listener of my listener, then build.
I can also write a plain class with annotations like BeforeStep and AfterStep, and a reader or writer that implements a listener interface is registered automatically.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Batch reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which listener types does Spring Batch offer, and what would you use each one for? Show...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/1e010c22c4/