Manuel Jordan
Manuel Jordan

Reputation: 16271

Spring Batch: How retrieve the skipped exceptions for Testing purposes

I am working with Spring Batch 3.0.7.RELEASE version

About Skip I have the following:

<batch:chunk reader="personErrorSkipFileItemReader"
             processor="personErrorSkipItemProcessor"
             writer="personErrorSkipFileItemWriter"
             commit-interval="100" 
             skip-limit="11">
        <batch:skippable-exception-classes>
            <batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>                                           
        <batch:include class="com.manuel.jordan.batch.exception.PersonBatchException"/>  
        </batch:skippable-exception-classes>         
</batch:chunk>

It works how is expected.

For logging and test purposes I need retrieve the following data:

Even better if can show in what step and area (reader, processor, writer) was thrown each skipped item.

Working through JobExecution, I have the following

List<Throwable> exceptions = jobExecution.getAllFailureExceptions();
logger.info("exceptions size: {}", exceptions.size());
for(Throwable throwable : exceptions){
    logger.error("Throwable Class: {}", throwable.getClass().getSimpleName());
    logger.error("{}", throwable.getMessage());
}
List<Throwable> exceptions_ = jobExecution.getFailureExceptions();
logger.info("exceptions_ size: {}", exceptions_.size());
for(Throwable throwable : exceptions_){
    logger.error("Throwable Class: {}", throwable.getClass().getSimpleName());
    logger.error("{}", throwable.getMessage());
}

But the size of both collections are higher than 1 and show the exceptions just when the Job completes how FAILED.

Because 8 < 11, the Job completes how COMPLETED and the lists sizes returns 0.

Upvotes: 0

Views: 1202

Answers (1)

Niraj Sonawane
Niraj Sonawane

Reputation: 11055

implement SkipListener , It has three callback methods

1 :onSkipInProcess 2 :onSkipInWrite 3: onSkipInRead

http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/SkipListener.html

Upvotes: 1

Related Questions