Java Reference
In-Depth Information
totalProcessed = 0;
System.out.println("The chunk size has been set to " + chunkSize);
return context;
}
public void update(RepeatContext context) {
totalProcessed++;
}
}
Listing 4-37.
Configuring
RandomChunkSizePolicy
<beans:bean id="randomChunkSizer"
class="com.apress.springbatch.chapter4.RandomChunkSizePolicy" />
<step id="copyStep">
<tasklet>
<chunk reader="inputReader" writer="outputWriter"
chunk-completion-policy="randomChunkSizer" />
</tasklet>
</step>
<job id="chunkConfigurationJob">
<step id="step1" parent="copyStep" />
</job>
You explore the rest of chunk configuration when you get to error handling. That section covers
retry and skip logic, which the majority of the remaining options center around. The next step elements
that this chapter looks at also carry over from a job: listeners.
Step Listeners
When you looked at job listeners, earlier this chapter, you saw the two events they can fire on: the start
and end of a job. Step listeners cover the same types of events (start and end), but for individual steps
instead of an entire job. This section covers the
org.springframework.batch.core.StepExecutionListener
and
org.springframework.batch.core.ChunkListener
interfaces, both of which allow the processing of logic
at the beginning and end of a step and chunk respectively. Notice that the Step's listener is named the
StepExecutionListener
and not just
StepListener
. There actually is a
StepListener
interface, however
it's just a marker interface that all step related listeners extend.
Both the
StepExecutionListener
and
ChunkListener
provide methods that are similar to the ones in
the
JobExecutionListener
interface.
StepExecutionListener
has a
beforeStep
and an
afterStep
, and
ChunkListener
has a
beforeChunk
and an
afterChunk
, as you would expect. All of these methods are void
except
afterStep
.
afterStep
returns an
ExitStatus
because the listener is allowed to modify the
ExitStatus
that was returned by the step itself prior to it being returned to the job. This feature can be
useful when a job requires more than just knowing whether an operation was successful to determine if
the processing was successful. An example would be doing some basic integrity checks after importing a
file (whether the correct number of records were written to the database, and so on). The ability to
configure listeners via annotations also continues to be consistent, with Spring Batch providing