The Ultimate Beginner’s Guide For Spring Batch Error Handling
Let’s get familiar with a couple of key concept for Spring Batch error handling including restart, retry and skip. You can also see coding examples in Spring Batch Github project. I have also put the link for how to configure in each section.
Restart
By default , if there’s an uncaught exception when processing the job, spring batch will stop the job. If the job is restarted with the same job parameters, it will pick up where it left off. The way it knows where the job status is by checking the job repository where it saves all the spring batch job status.
Let’s say for example if your job failed at step 2 for the 100th record, spring batch will stop the job processing. When the job is restarted, it will start at step 2 for the 100th record, since step 1 for 100th record is already successfully processed.
You can see more details for how to configure restart here.
Retry
Spring batch can integrate with spring retry to retry multiple times until it maxed out the retry count that it’s configured. You can also specify specific exception that allows for retry. ItemProcessor and ItemWriter is retryable, but ItemReader is not retryable. The reason is because ItemReader is forward only logic and rolling back read transaction will be very difficult and thus retry does not apply to ItemReader.
You can see more how to configure retry here.
You can set up custom RetryListener to log or do appropriate actions.
Skip
Sometimes we will need to skip failures and continue to process the rest of the records. Spring Batch allows you to configure to skip based on specific exception and also how many items are valid to be skipped. You should probably not skipped all of the records, so the number of skipped item is a required attribute.
You can see more details for how to configure skip here.
You can also set up custom SkipListeners to log the error or do appropriate actions.
You might also be interested in Four ways to scale Spring Batch.
Reference: Learning Spring Batch by Michael Minella