RESOLVED - BATCH-1065: Updated content about controlling flow

This commit is contained in:
dhgarrette
2009-02-07 06:24:00 +00:00
parent eb4991efe7
commit ddc5a997cc

View File

@@ -965,11 +965,11 @@
<section id="controllingStepFlow">
<title>Controlling Step Flow</title>
<para>With the ability to group steps together within an owning job, comes
<para>With the ability to group steps together within an owning job comes
the need to be able to control how the job 'flows' from one step to
another. The failure of a <classname>Step</classname> doesn't necessarily
mean that the <classname>Job</classname> should fail. Furthermore, there
may be more than one type of 'success', which determines which
may be more than one type of 'success' which determines which
<classname>Step</classname> should be executed next. Depending upon how a
group of Steps is configured, certain steps may not even be processed at
all.</para>
@@ -993,8 +993,8 @@
</imageobject>
</mediaobject>
<para>This can be achieved using the 'next' attribute of
<classname>Step</classname>:</para>
<para>This can be achieved using the 'next' attribute of the step
element:</para>
<para><programlisting>
&lt;job id="job"&gt;
@@ -1003,31 +1003,42 @@
&lt;step name="stepC" /&gt;
&lt;/job&gt;
</programlisting>In the scenario above, 'step A' will execute first. If 'step
A' completes normally, then 'step B' will execute and so on. However, if
'step A' fails, then the entire <classname>Job</classname> will fail and
'step B' will not execute.</para>
</programlisting>In the scenario above, 'step A' will execute first because it
is the first <classname>Step</classname> listed. If 'step A' completes
normally, then 'step B' will execute, and so on. However, if 'step A'
fails, then the entire <classname>Job</classname> will fail and 'step B'
will not execute.</para>
<note>
<para>With the Spring Batch namespace, the first step listed in the
configuration will <emphasis>always</emphasis> be the first step
executed by the <classname>Job</classname>. The order of the other
step elements does not matter, but the first step must always appear
first in the xml. </para>
</note>
</section>
<section>
<title>Conditional Flow</title>
<para>In the example above, there's only two possibilities:</para>
<para>In the example above, there are only two possibilities:</para>
<orderedlist>
<listitem>
<para>The Step is successful and the next Step should be
executed</para>
<para>The <classname>Step</classname> is successful and the next
<classname>Step</classname> should be executed.</para>
</listitem>
<listitem>
<para>The Step failed and thus the Job should fail.</para>
<para>The <classname>Step</classname> failed and thus the
<classname>Job</classname> should fail.</para>
</listitem>
</orderedlist>
<para>In many cases this may be sufficient. However, what about a
scenario in which the failure of a Step should trigger a different Step,
rather than causing failure? <mediaobject>
<para>In many cases, this may be sufficient. However, what about a
scenario in which the failure of a <classname>Step</classname> should
trigger a different <classname>Step</classname>, rather than causing
failure? <mediaobject>
<imageobject role="html">
<imagedata align="center" fileref="images/conditional-flow.png"
scale="" width="40%" />
@@ -1040,11 +1051,37 @@
</imageobject>
</mediaobject></para>
<para>In order to handle this scenario, the next step can be determined
based on the result of the step by adding a next element to the Step.
The "on" attribute uses a simple pattern-matching scheme to match the
exit code of the Step to the various next elements declared. Only two
special characters are allowed:</para>
<para id="nextElement">In order to handle more complex scenarios, the
Spring Batch namespace allows transition elements to be defined within
the step element. One such transition is the "next" element. Like the
"next" attribute, the "next" element will tell the
<classname>Job</classname> which <classname>Step</classname> to execute
next. However, unlike the attribute, any number of "next" elements are
allowed on a given <classname>Step</classname>, and there is no default
behavior the the case of failure. This means that if transition elements
are used, then all of the behavior for the <classname>Step</classname>'s
transitions must be defined explicitly. Note also that a single step
cannot have both a "next" attribute and a transtion element.</para>
<para>The next element specifies a pattern to match and the step to
execute next: </para>
<para><programlisting>
&lt;job id="job"&gt;
&lt;step name="stepA"&gt;
&lt;next on="FAILED" to="stepB" /&gt;
&lt;next on="*" to="stepC" /&gt;
&lt;/step&gt;
&lt;step name="stepB" next="stepC" /&gt;
&lt;step name="stepC" /&gt;
&lt;/job&gt;
</programlisting></para>
<para>The "on" attribute of a transition element uses a simple
pattern-matching scheme to match the <classname>ExitStatus</classname>
that results from the exeution of the <classname>Step</classname>. Only
two special characters are allowed in the pattern:</para>
<itemizedlist>
<listitem>
@@ -1059,27 +1096,17 @@
<para>For example, "c*t" will match "cat" and "count", while "c?t" will
match "cat" but not "count".</para>
<para>Any number of "next" elements is allowed, but if the step has an
exit code that is not covered by a "next" element, then the framework
will throw an exception and the job will fail. It is important to note
that the framework will automatically order transitions from most
specific to least specific. So even if the "next" elements were swapped
for "stepA" below, an exit status of "FAILED" would still go to
"stepB".</para>
<para>While there is no limit to the number of transition elements on a
<classname>Step</classname>, if the <classname>Step</classname>'s
execution results in an <classname>ExitStatus</classname> that is not
covered by an element, then the framework will throw an exception and
the <classname>Job</classname> will fail. It is important to note that
the framework will automatically order transitions from most specific to
least specific. This means that even if the elements were swapped for
"stepA" in the example above, an <classname>ExitStatus</classname> of
"FAILED" would still go to "stepB".</para>
<para><programlisting>
&lt;job id="job"&gt;
&lt;step name="stepA"&gt;
&lt;next on="FAILED" to="stepB" /&gt;
&lt;next on="*" to="stepC" /&gt;
&lt;/step&gt;
&lt;step name="stepB" next="stepC" /&gt;
&lt;step name="stepC" /&gt;
&lt;/job&gt;
</programlisting></para>
<section>
<section id="batchStatusVsExitStatus" label="">
<title>Batch Status vs. Exit Status</title>
<para>When configuring a <classname>Job</classname> for conditional
@@ -1092,7 +1119,7 @@
record the status of a <classname>Job</classname> or
<classname>Step</classname>. It can be one of the following values:
COMPLETED, STARTING, STARTED, FAILED, STOPPING, STOPPED, or UNKNOWN.
Most of them are self explanatory, COMPLETED is the status set when a
Most of them are self explanatory: COMPLETED is the status set when a
step or job has completed successfully, FAILED is set when it fails,
and so on. The example above contains the following 'next'
element:</para>
@@ -1104,13 +1131,12 @@
<para>At first glance, it would appear that the 'on' attribute
references the <classname>BatchStatus</classname> of the
<classname>Step</classname> to which it belongs. However, it
<classname>Step</classname> to which it belongs. However, it actually
references the <classname>ExitStatus</classname> of the
<classname>Step</classname>. As the name implies,
<classname>ExitStatus</classname> represents the status of a
<classname>Step</classname> after it finishes execution. More
specifically, the 'next' element above references the
<classname>ExitCode</classname> of the
specifically, the 'next' element above references the exit code of the
<classname>ExitStatus</classname>. To write it in English, it says:
"go to stepB if the exit code is FAILED". By default, the exit code is
always the same as the <classname>BatchStatus</classname> for the
@@ -1131,17 +1157,19 @@
<orderedlist>
<listitem>
<para>The step failed, in which case the job should fail.</para>
<para>The <classname>Step</classname> failed, in which case the
job should fail.</para>
</listitem>
<listitem>
<para>The Step completed successfully.</para>
<para>The <classname>Step</classname> completed
successfully.</para>
</listitem>
<listitem>
<para>The Step completed successfully, but with an exit code of
'COMPLETED WITH SKIPS'. In this case, a different step should be
run to handle the errors.</para>
<para>The <classname>Step</classname> completed successfully, but
with an exit code of 'COMPLETED WITH SKIPS'. In this case, a
different step should be run to handle the errors.</para>
</listitem>
</orderedlist>
@@ -1149,19 +1177,18 @@
change the exit code based on the condition of the execution having
skipped records:</para>
<programlisting>public class SkipCheckingListener implements StepExecutionListener {
<programlisting> public class SkipCheckingListener extends StepExecutionListenerSupport {
public ExitStatus afterStep(StepExecution stepExecution) {
if (!stepExecution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())
&amp;&amp; stepExecution.getSkipCount() &gt; 0) {
return new ExitStatus("COMPLETED WITH SKIPS");
} else {
return null;
public ExitStatus afterStep(StepExecution stepExecution) {
if (!stepExecution.getExitStatus().getExitCode().equals(ExitStatus.FAILED.getExitCode())
&amp;&amp; stepExecution.getSkipCount() &gt; 0) {
return new ExitStatus("COMPLETED WITH SKIPS");
} else {
return null;
}
}
}
...
}</programlisting>
}</programlisting>
<para>The above code is a <classname>StepExecutionListener</classname>
that first checks to make sure the <classname>Step</classname> was
@@ -1175,32 +1202,140 @@
<section>
<title>Configuring for Stop</title>
<para>If it is desired that the batch job stop under certain conditions,
then either the "stop" tag or the "end" tag may be used.</para>
<para>After the discussion of <link
linkend="batchStatusVsExitStatus"><classname>BatchStatus</classname> and
<classname>ExitStatus</classname></link>, one might wonder how the
<classname>BatchStatus</classname> and <classname>ExitStatus</classname>
are determined for the <classname>Job</classname>. While these statuses
are determined for the <classname>Step</classname> by the code that is
executed, the statuses for the <classname>Job</classname> will be
determined based on the configuration.</para>
<para>The "stop" tag indicates the job should stop processing with an
exit status of "STOPPED". The "to" attribute tells the framework which
step should be first when the job is subsequently restarted. This
mechanism allows the job to pause temporarily.</para>
<para>So far, all of the job configurations discussed have had at least
one final <classname>Step</classname> with no transitions. For example,
after the following step executes, the <classname>Job</classname> will
end:</para>
<para>On the other hand, the "end" tag will stop the job but does not
allow for a "to" attribute. The "status" attribute is optional. It will
determine the exit status of the step if the flow ends at that location.
The only legal values for the "status" are "COMPLETED", "FAILED", and
"STOPPED". If no status is specified, then the default is
"COMPLETED".</para>
<para><programlisting> &lt;step name="stepC" /&gt;</programlisting></para>
<para><programlisting>
&lt;step name="step1"&gt;
<para>If no transitions are defined for a <classname>Step</classname>,
then the <classname>Job</classname>'s statuses will be defined as
follows:</para>
<itemizedlist>
<listitem>
<para>If the <classname>Step</classname> ends with
<classname>ExitStatus</classname> FAILED, then the
<classname>Job</classname>'s <classname>BatchStatus</classname> and
<classname>ExitStatus</classname> will both be FAILED.</para>
</listitem>
<listitem>
<para>Otherwise, the the <classname>Job</classname>'s
<classname>BatchStatus</classname> and
<classname>ExitStatus</classname> will both be COMPLETED.</para>
</listitem>
</itemizedlist>
<para>While this method of terminating a batch job is sufficient for
some batch jobs, such as a simple sequential step job, custom defined
job-stopping scenarios may be required. For this purpose, Spring Batch
provides three transition elements to stop a <classname>Job</classname>
(in addition to the <link linkend="nextElement">"next" element</link>
that we discussed previously). Each of these stopping elements will stop
a <classname>Job</classname> with a particular
<classname>BatchStatus</classname>. It is important to note that the
stop transition elements will have no effect on either the
<classname>BatchStatus</classname> or <classname>ExitStatus</classname>
of any <classname>Step</classname>s in the <classname>Job</classname>:
these elements will only affect the final statuses of the
<classname>Job</classname>. For example, it is possible for every step
in a job to have a status of FAILED but the job to have a status of
COMPLETED, or vise versa.</para>
<section>
<title>The 'End' Element</title>
<para>The 'end' element instructs a <classname>Job</classname> to stop
with a <classname>BatchStatus</classname> of COMPLETED. A
<classname>Job</classname> that has finished with status COMPLETED
cannot be restarted (the framework will throw a
<classname>JobInstanceAlreadyCompleteException</classname>). The 'end'
element also allows for an optional 'status' attribute that can be
used to customize the <classname>ExitStatus</classname> of the
<classname>Job</classname>. If no 'status' attribute is given, then
the <classname>ExitStatus</classname> will be "COMPLETED" by default,
to match the <classname>BatchStatus</classname>.</para>
<para>In the following scenario, if step2 fails, then the
<classname>Job</classname> will stop with a
<classname>BatchStatus</classname> of COMPLETE and an
<classname>ExitStatus</classname> of "COMPLETED" and step3 will not
execute; otherwise, execution will move to step3. Additionally, if
step2 fails, the <classname>Job</classname> will not be
restartable.</para>
<programlisting> &lt;step name="step1" next="step2"&gt;
&lt;step name="step2"&gt;
&lt;end on="FAILED"/&gt;
&lt;next on="*" to="step3"/&gt;
&lt;/step&gt;
&lt;step name="step3"&gt;</programlisting>
</section>
<section>
<title>The 'Fail' Element</title>
<para>The 'fail' element instructs a <classname>Job</classname> to
stop with a <classname>BatchStatus</classname> of FAILED. Unlike the
'end' element, the 'fail' element will not prevent the
<classname>Job</classname> from being restarted. The 'fail' element
also allows for an optional 'status' attribute that can be used to
customize the <classname>ExitStatus</classname> of the
<classname>Job</classname>. If no 'status' attribute is given, then
the <classname>ExitStatus</classname> will be "FAILED" by default, to
match the <classname>BatchStatus</classname>.</para>
<para>In the following scenario, if step2 fails, then the
<classname>Job</classname> will stop with a
<classname>BatchStatus</classname> of FAILED and an
<classname>ExitStatus</classname> of "EARLY TERMINATION" and step3
will not execute; otherwise, execution will move to step3.
Additionally, if step2 fails, and the <classname>Job</classname> is
restarted, the execution will begin again on step2.</para>
<programlisting> &lt;step name="step1" next="step2"&gt;
&lt;step name="step2"&gt;
&lt;fail on="FAILED" status="EARLY TERMINATION"/&gt;
&lt;next on="*" to="step3"/&gt;
&lt;/step&gt;
&lt;step name="step3"&gt;</programlisting>
</section>
<section>
<title>The 'Pause' Element</title>
<para>The 'pause' element instructs a <classname>Job</classname> to
stop with a <classname>BatchStatus</classname> of STOPPED. Pausing a
<classname>Job</classname> is a meant to be a temporary so that the
operator can take some action before restarting the
<classname>Job</classname>. The 'pause' element requires a 'to'
attribute that specifies the step where execution should pick up once
the <classname>Job</classname> is restarted after being paused.</para>
<para>In the following scenario, step1 will execute and COMPLETE. The
job will then stop. Once it is restarted, execution will begin on
step2.</para>
<para><programlisting> &lt;step name="step1"&gt;
&lt;stop on="COMPLETED" to="step2"/&gt;
&lt;/step&gt;
&lt;step name="step2"&gt;
&lt;next on="FOO" to="step3"/&gt;
&lt;end on="*" status="FAILED"/&gt;
&lt;/step&gt;
&lt;step name="step3" /&gt;
</programlisting></para>
&lt;step name="step3" /&gt;</programlisting></para>
</section>
</section>
<section>
@@ -1248,18 +1383,19 @@
</programlisting></para>
</section>
</section>
<section>
<title>Late binding of Job and Step Attributes</title>
<section>
<title>Late binding of Job and Step Attributes</title>
<para>Both the XML and Flat File examples above use the Spring
<classname>Resource</classname> abstraction to obtain a file . This
works because <classname>Resource</classname> has a
<markup>getFile</markup> method, which returns a
<classname>java.io.File</classname>. Both XML and Flat File resources
can be configured using standard Spring constructs:</para>
<para>Both the XML and Flat File examples above use the Spring
<classname>Resource</classname> abstraction to obtain a file . This works
because <classname>Resource</classname> has a <markup>getFile</markup>
method, which returns a <classname>java.io.File</classname>. Both XML and
Flat File resources can be configured using standard Spring
constructs:</para>
<programlisting>
<programlisting>
&lt;bean id="flatFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
&lt;property name="resource"
@@ -1268,15 +1404,15 @@
</programlisting>
<para>The above <classname>Resource</classname> will load the file from
the file system location specified. Note that absolute locations have to
start with a double slash ("//"). In most spring applications, this
solution is good enough because the names of these are known at compile
time. However, in batch scenarios, the file name may need to be
determined at runtime as a parameter to the job. This could be solved
using '-D' parameters, i.e. a system property:</para>
<para>The above <classname>Resource</classname> will load the file from
the file system location specified. Note that absolute locations have to
start with a double slash ("//"). In most spring applications, this
solution is good enough because the names of these are known at compile
time. However, in batch scenarios, the file name may need to be determined
at runtime as a parameter to the job. This could be solved using '-D'
parameters, i.e. a system property:</para>
<programlisting>
<programlisting>
&lt;bean id="flatFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
&lt;property name="resource" value="${input.file.name}" /&gt;
@@ -1284,21 +1420,21 @@
</programlisting>
<para>All that would be required for this solution to work would be a
system argument (-Dinput.file.name="file://file.txt"). (Note that
although a <classname>PropertyPlaceholderConfigurer</classname> can be
used here, it is not necessary if the system property is always set
because the <classname>ResourceEditor</classname> in Spring already
filters and does placeholder replacement on system properties.)</para>
<para>All that would be required for this solution to work would be a
system argument (-Dinput.file.name="file://file.txt"). (Note that although
a <classname>PropertyPlaceholderConfigurer</classname> can be used here,
it is not necessary if the system property is always set because the
<classname>ResourceEditor</classname> in Spring already filters and does
placeholder replacement on system properties.)</para>
<para>Often in a batch setting it is preferable to parameterize the file
name in the <link
linkend="jobParameters"><classname>JobParameters</classname></link> of
the job, instead of through system properties, and access them that way.
To accomplish this, Spring Batch allows for the late binding of various
Job and Step attributes:</para>
<para>Often in a batch setting it is preferable to parameterize the file
name in the <link
linkend="jobParameters"><classname>JobParameters</classname></link> of the
job, instead of through system properties, and access them that way. To
accomplish this, Spring Batch allows for the late binding of various Job
and Step attributes:</para>
<programlisting>
<programlisting>
&lt;bean id="flatFileItemReader" scope="step"
class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
&lt;property name="resource" value="<emphasis role="bold">#{jobParameters[input.file.name]}</emphasis>" /&gt;
@@ -1306,12 +1442,12 @@
</programlisting>
<para>Both the <classname>JobExecution</classname> and
<classname>StepExecution</classname> level
<classname>ExecutionContext</classname> can be accessed in the same
way:</para>
<para>Both the <classname>JobExecution</classname> and
<classname>StepExecution</classname> level
<classname>ExecutionContext</classname> can be accessed in the same
way:</para>
<programlisting>
<programlisting>
&lt;bean id="flatFileItemReader" scope="step"
class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
&lt;property name="resource" value="<emphasis role="bold">#{jobExecutionContext[input.file.name]}</emphasis>" /&gt;
@@ -1319,7 +1455,7 @@
</programlisting>
<programlisting>
<programlisting>
&lt;bean id="flatFileItemReader" scope="step"
class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
&lt;property name="resource" value="<emphasis role="bold">#{stepExecutionContext[input.file.name]}</emphasis>" /&gt;
@@ -1327,13 +1463,13 @@
</programlisting>
<section>
<title>Step Scope</title>
<section>
<title>Step Scope</title>
<para>All of the late binding examples from above have a scope of
"step" declared on the bean definition:</para>
<para>All of the late binding examples from above have a scope of "step"
declared on the bean definition:</para>
<programlisting>
<programlisting>
&lt;bean id="flatFileItemReader" <emphasis role="bold">scope="step"</emphasis>
class="org.springframework.batch.item.file.FlatFileItemReader"&gt;
&lt;property name="resource" value="#{jobParameters[input.file.name]}" /&gt;
@@ -1341,17 +1477,16 @@
</programlisting>
<para>Using a scope of <classname>Step</classname> is required in
order to use late binding since the bean cannot actually be
instantiated until the <classname>Step</classname> starts, which
allows the attributes to be found. Because it is not part of the
Spring container by default, it must be added explicitly:</para>
<para>Using a scope of <classname>Step</classname> is required in order
to use late binding since the bean cannot actually be instantiated until
the <classname>Step</classname> starts, which allows the attributes to
be found. Because it is not part of the Spring container by default, it
must be added explicitly:</para>
<programlisting>
<programlisting>
&lt;bean class="org.springframework.batch.core.scope.StepScope" /&gt;
</programlisting>
</section>
</section>
</section>
</chapter>