WebSphere Application Server & Liberty

JSR-352 (Java Batch) Post #36: A review of Chunk Step Listeners

By David Follis posted Wed April 03, 2019 07:36 AM

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.

This series is also available as a podcast on iTunesGoogle PlayStitcher, or use the link to the RSS feed

There are a lot of songs about listening.  Chunk processing has a rhythm to it, so I’m going with “Listen to the Music” by the Doobie Brothers. 

A batchlet step just has the basic Step Listener, but a chunk step has a bunch of different listeners you could use.  I thought we might wrap up our coverage of chunk steps by taking a quick look through them all. 

First there is the chunk listener itself which gets control before and after each chunk, plus when any unhandled exceptions are thrown.  This is your opportunity to get control around the transaction that wraps each chunk of processing.

As we dive inward into the chunk loop we also have listeners around each piece of the processing in the chunk.  The ItemReadListener gets control around the ItemReader.  The ItemProcessListener gets control around the ItemProcessor.  The ItemWriteListener gets control around the ItemWriter processing.  These listeners give you the opportunity to get involved before, after, and in between the parts of chunk processing.  They also get control if unhandled exceptions are thrown within the reader, processor, and writer.

Then come the skip listeners.  There are three separate skip listeners, one each for the reader, processor, and writer.  They get control if a skippable exception is thrown.  Which one gets control depends on which artifact threw the exception.  All of these listeners get passed the exception that was thrown. 

This might be a good place to remind you that a single Java class can implement more than one listener because the method names are different.  For instance, a single class could implement the onSkipReadItem method, the onSkipProcessItem method, and the onSkipWriteIem method.  You would specify this Java class in the JSL as a listener and the batch runtime would realize it implements all three listeners and call the right method whenever appropriate. 

Also recall that more than one class can implement the same listener interface.  There is no rule about what order they get called in, but they will all get called when appropriate for the interface they implement.

We’ll wrap this up with the retry listeners.  There are three of these also.  One listener is defined for reader, processor, and writer.  The right listener will be called based on which batch artifact threw a retryable exception (whether it was a retry with or without rollback, there’s just the one listener). 

That’s it for our discussion of chunk processing.  What’s next I wonder….