Log skipped items in a DataBase.

A batch is executed without any human interaction. Due to the nature of a batch program, we must be able to know what are the processed items and also what are items in errors. In case of errors, we should also know what was the exception. Like that, we can fix the problem and relaunch a batch process for the skipped items.

The objective of this article is to log all the errors in a table: Source code

Spring batch provides some listeners:

StepExecutionListener: Listener interface for the lifecycle of a Step.

ChunkListener: Listener interface for the lifecycle of a chunk.

JobExecutionListener: Provide callbacks at specific points in the lifecycle of a Job.

SkipListener: Interface for listener to skipped items.

If we want to keep a track of all skipped items. The SkipListener is a perfect listener to do that

The SampleSkipListener will be called each time an items is skipped. But unfortunetly, the listener does not know the context of the job’s execution. We have to get this informations by using another Listener: StepExecutionListener.

This Listener is called when the step starts. At this moment, we can store some informations inside the StepExecutionContext. This information will be injected into SampleSkipListener via the Spring configuration.

Create a TABLE

Each row contains the skipped item id, the jobExecutionId, the stepExecutionId and a jobName.