WebSphere Application Server

JSR-352 (Java Batch) Post #3: Stopping a Running Batchlet

By David Follis posted Tue July 03, 2018 04:17 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.
Last time we talked about how great batchlets are for doing all sorts of things.  But how do you stop one that gets out of control?  Or that just happens to be in control when operations wants to stop the job? 

This requires a little bit of thought from the developer.  But before we get to that, let’s take a moment to recall how stop processing works when a batchlet step is executing as the stop operation is issued.  Stop processing will mark the job as stopping to try to prevent it from doing anything new.  In a chunk step that means after each read/process cycle.  But for a batchlet there isn’t any point in processing where the batch container gets control to stop execution.  The container needs help from the batchlet itself.

What will happen is the stop method of the batchlet will be driven by the container on a separate thread from the one executing the batchlet.  This means the batchlet instance has code executing on two threads at once.  It is up to the application code to find a way to communicate between those two and get the batchlet to stop. 

Here’s a high-level view:


boolean stopping=false;


process( ) {


while (!stopping) {

   … do batchlet stuff



stop( ) {




And that sort of structure works great if your batchlet is doing something in a loop.  But if your batchlet has made a call to some other thing, maybe a database or some utility, then there isn’t anything the stop method can probably do to stop it.  The stop method has to just kind of shrug, look at the batch container apologetically, and return.  Hopefully whatever the batchlet called will return and the process method will complete and then the job will stop.

 >>Next article in this series

<<Previous article in this series