Java Reference
In-Depth Information
Although a bit hard to read, you'll notice that Spring Batch has saved your commit count in the
jobRepository. Because of this and your logic to skip the 50 th customer the second time around, you can
re-execute your job knowing that Spring Batch will start back where it left off and your writer will skip
the item that caused the error.
Files, databases, services and even your own custom ItemReaders—Spring Batch provides you with
a wide array of input options of which you have truly only scratched the surface here. Unfortunately, not
all of the data you work with in the real world is as pristine as the data you have been working with here.
However, not all errors are ones that need to stop processing. In the next section you will look at some of
the ways that Spring Batch allows you to deal with input errors.
Error Handling
Things can go wrong in any part of a Spring Batch application—on startup, when reading input,
processing input, or writing output. In this section, you will look at ways to handle different errors that
can occur during batch processing.
Skipping Records
When there is an error reading a record from your input, you have a couple different options. First, an
Exception can be thrown that causes processing to stop. Depending on how many records need to be
processed and the impact of not processing this single record, this may be a drastic resolution. Instead,
Spring Batch provides the ability to skip a record when a specified Exception is thrown. This section will
look at how to use this technique to skip records based upon specific Exceptions.
There are two pieces involved in choosing when a record is skipped. The first is under what
conditions to skip the record, specifically what exceptions you will ignore. When any error occurs during
the reading process, Spring Batch throws an exception. In order to determine what to skip, you need to
identify what exceptions to skip.
The second part of skipping input records is how many records you will allow the step to skip before
considering the step execution failed. If you skip one or two records out of a million, not a big deal;
however, skipping half a million out of a million is probably wrong. It's your responsibility to determine
the threshold.
To actually skip records, all you need to do is tweak your configuration to specify the exceptions you
want to skip and how many times it's okay to do so. Say you want to skip the first 10 records that throw
any org.springframework.batch.item.ParseException . Listing 7-61 shows the configuration for this
Listing 7-61. Configuring to Skip 10 ParseExceptions
<step id="copyFileStep">
<chunk reader="customerItemReader" writer="outputWriter"
commit-interval="10" skip-limit="10">
<include class="org.springframework.batch.item.ParseException"/>
Search WWH ::

Custom Search