Java Reference
In-Depth Information
In this scenario, you have a single exception that you want to be able to skip. However, sometimes
this can be a rather exhaustive list. The configuration in Listing 7-61 allows the skipping of a specific
exception, but it might be easier to configure the ones you don't want to skip instead of the ones you do.
To do this, you use a combination of the include tag like Listing 7-61 did and the exclude tag. Listing 7-62
shows how to configure the opposite of your previous example (skipping all exceptions except for the
Listing 7-62. Configuring to Skip All Exceptions Except the ParseException
<step id="copyFileStep">
<chunk reader="customerItemReader" writer="outputWriter"
commit-interval="10" skip-limit="10">
<include class="java.lang.Exception"/>
<exclude class="org.springframework.batch.item.ParseException"/>
The configuration in Listing 7-62 specifies that any Exception that extends java.lang.Exception
except for org.springframework.batch.item.ParseException will be skipped up to 10 times.
There is a third way to specify what Exceptions to skip and how many times to skip them. Spring
Batch provides an interface called org.springframework.batch.core.step.skip.SkipPolicy . This
interface, with its single method shouldSkip , takes the Exception that was thrown and the number of
times records have been skipped. From there, any implementation can determine what Exceptions they
should skip and how many times. Listing 7-63 shows a SkipPolicy implementation that will not allow a to be skipped but 10 ParseExceptions to be skipped.
Listing 7-63. FileVerificationSkipper
package com.apress.springbatch.chapter7;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.item.ParseException;
public class FileVerificationSkipper implements SkipPolicy {
public boolean shouldSkip(Throwable exception, int skipCount)
throws SkipLimitExceededException {
if(exception instanceof FileNotFoundException) {
return false;
} else if(exception instanceof ParseException && skipCount <= 10) {
return true;
} else {
return false;
Search WWH ::

Custom Search