Java Reference
In-Depth Information
<job id="conditionalStepLogicJob">
<step id="step1">
<tasklet ref="passTasklet"/>
<next on="*" to="step2a"/>
<next on="FAILED" to="step2b"/>
</step>
<step id="step2a">
<tasklet ref="successTasklet"/>
</step>
<step id="step2b">
<tasklet ref="failTasklet"/>
</step>
</job>
</beans:beans>
The
next
tag uses the
on
attribute to evaluate the
ExitStatus
of the step and determine what to do.
It's important to note that you've seen both
org.springframework.batch.core.ExitStatus
and
org.springframework.batch.core.BatchStatus
over the course of this chapter.
BatchStatus
is an
attribute of the
JobExecution
or
StepExecution
that identifies the current state of the job or step.
ExitStatus
is the value returned to Spring Batch at the end of a job or step. The
on
attribute evaluates the
ExitStatus
for its decisions. So, the example in Listing 4-40 is the XML equivalent of saying, “If the exit
code of
step1
doesn't equal
FAILED
, go to
step2a
, else go to
step2b
.”
Because the values of the
ExitStatus
are really just
String
s, the ability to use wildcards can make
things interesting. Spring Batch allows for two wildcards in
on
criteria:
•
*
matches zero or more characters. For example,
C*
matches
C
,
COMPLETE
, and
CORRECT
.
•
?
matches a single character. In this case,
?AT
matches
CAT
or
KAT
but not
THAT
.
Although evaluating the
ExitStatus
gets you started in determining what to do next, it may not take
you all the way. For example, what if you didn't want to execute a step if you skipped any records in the
current step? You wouldn't know that from the
ExitStatus
alone.
Note
Spring Batch helps you when it comes to configuring transitions. It automatically orders the transitions
from most to least restrictive and applies them in that order.
Spring Batch has provided a programmatic way to determine what to do next. You do this by
creating an implementation of the
org.springframework.batch.core.job.flow.JobExecutionDecider
interface. This interface has a single method,
decide
, that takes both the
JobExecution
and the
StepExecution
and returns a
FlowExecutionStatus
(a wrapper for a
BatchStatus
/
ExitStatus
pair). With
both the
JobExecution
and
StepExecution
available for evaluation, all information should be available to
you to make the appropriate decision about what your job should do next. Listing 4-41 shows an
implementation of the
JobExecutionDecider
that randomly decides what the next step should be.
Listing 4-41.
RandomDecider