Java Reference
In-Depth Information
<step id="retryStep">
<tasklet>
<chunk reader="itemReader" writer="retryItemWriter"
processor="itemProcessor"
commit-interval="20"/>
</tasklet>
</step>
</job>
The last aspect of retry logic is that, like most available events in Spring Batch, retry has the ability to
register listeners to when an item is being retried. There are two differences, however, between all the
other listeners and
org.springframework.batch.retry.RetryListener
. First,
RetryListener
has no
annotation equivalent of the interface, so if you want to register a listener on retry logic, you have to
implement the
RetryListener
interface. The other difference is that instead of two methods in the
interface for the start and end, there are three in this interface. In
RetryListener
, the
open
method is
called when the retry block is about to be called,
onError
is called once for each retry, and
close
is called
when the full retry block is complete.
That covers it for retry logic. The other way to handle item-specific error handling is to skip the item
altogether.
Item Skip
One of the greatest things in Spring Batch is the ability to skip an item that is causing problems. This
feature can easily prevent a phone call in the middle of the night to deal with a production problem if the
item can be addressed the next day. Configuring the ability to skip an item is similar to configuring retry
logic. All you need to do is use the
skip-limit
attribute on the
chunk
tag and specify the exceptions that
should cause an item to be skipped. Listing 4-55 demonstrates how to configure a step to allow a
maximum of 10 items to be skipped via
skip-limit
. It then states that any item that causes any subclass
of
java.lang.Exception
except for
java.lang.NullPointerException
is
allowed to be skipped. Any item
that throws a
NullPointerException
causes the step to end in error.
Listing 4-55.
Skip Logic Configuration
<job id="flowJob">
<step id="retryStep">
<tasklet>
<chunk reader="itemReader" writer="itemWriter"
processor="itemProcessor" commit-interval="20"
skip-limit="10">
<skippable-exception-classes>
<include class="java.lang.Exception"/>
<exclude class="java.lang.NullPointerException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
</job>
When using item-based error handling, whether it's retrying to process an item or skipping it, there
can be transactional implications. You learn what those are and how to address them when the topic
gets into reading and writing items in Chapters 7 & 9.