WebSphere Application Server & Liberty

JSR-352 (Java Batch) Post #8: Flow Control Using Step Exit Status

By David Follis posted Thu August 09, 2018 01:12 PM


This post is part of a series delving into the details of the JSR-352 (Java Batch) specification. Each post examines a very specific part of the specification and looks at how it works and how you might use it in a real batch application.

To start at the beginning, follow the link to the first post.

The next post in the series is here.
Time to pull together some things we’ve talked about earlier.  Last time we covered how control flows from one step to another through the ‘next’ keyword as part of a step.  Before that we talked about how an application can set the step Exit Status through the StepContext.  Now we’ll look at how the executing application can influence flow through a job using the Exit Status.

Before we start, there’s one more thing we need to know about- Batch Status.  While the Exit Status is a string value set by the application, the Batch Status (still a string) is one of a set of specification-defined values that indicate what’s going on with the job.  The values are:  STARTING, STARTED, STOPPING, STOPPED, FAILED, COMPLETED, and ABANDONED.  Obviously, a running job can only have a few of those – a running job’s Batch Status will never be “STARTING”.  Batch Status is interesting because…here’s the important part…if the application doesn’t set an Exit Status, the Batch Status value is used.

Now that we’ve got that in our heads, there are four transition elements you can include in a step.  All of them key off the Exit Status value for the step (which might be the Batch Status value, but you knew that).  The transition elements are processed in the order they are found in the JSL, and you can wild-card the status string (so a catch-all value of ‘*’ at the end might not be a bad idea). 

The first element is ‘next’.  You would code it like this:

<next on=”0” to=”Step2” />

Which says that if the exit status for the step is “0” proceed to the step whose id value is Step2.  You can transition to a step, a flow, or a split (using the appropriate id value for the one you want to transition to). 

The other three transition elements are:  fail, end, and stop.  All allow you to specify an Exit Status to match against.  When a match occurs for one of these, the Batch Status for the Job is set appropriately.  For fail, the job has failed and is non-restartable.  For end, the ends successfully (this is how you conditionally end a job, as opposed to just running out of steps to execute).  And finally, for stop, the job stops executing, but is restartable.  You can also set the id of a step to restart on, if you want.  (We’ll get to job restart processing eventually).  All three allow you to set an Exit Status for the job itself (different from the Batch Status of the job). 

<next on=”COMPLETED” to=”NextBlogEntry” />

 >>Next article in this series

<<Previous article in this series