WebSphere Application Server & Liberty

 View Only

JSR-352 (Java Batch) Post #159: How-To: Use a Partition Mapper - Part 2

By David Follis posted Thu October 28, 2021 09:38 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

You’ve read last week’s post and given some serious thought to how you want to partition your step.  Let’s get to some coding!

The partitionMapper only has to implement one method, mapPartitions.  That method needs to return an instance of an object that implements the PartitionPlan interface.  Conveniently, JSR-352 implementations provide you with one called, not surprisingly, PartitionPlanImpl.  Just new up an instance of that and you’re on the way.

We’ll presume you’ve figured out how much data you have and how many partitions you want and how many you want to let run at once.  If not, go back and re-read last week’s post and ponder some more. 

Once you’re set, you can call the setPartitions and setThreads methods to set those values into the plan you instantiated. 

In the JSL for the partition you’ll need to have set up the properties that will get injected into each copy of the step.  If you have a chunk step, the reader might need to know what record key values to start and stop at.  For our sample, the batchlet needs to know the full path of the file it should process.  Decide what the property names are and put them in the JSL.

Now you need to set up the different property values to give to each instance of the step.  Start by creating an array of Properties objects.  You’ll need a slot in the array for each partition.  Remember the array size is the number of partitions, but partition numbers start at zero (like the array index will).

At this point, iterate through the array, creating a new Properties object to go in each array slot.  Then use the setProperty method to set whatever properties you need for each partition.  You might be tempted to use the put method, but don’t do it.  Properties have to be Strings.  Using put will allow you to set integers or other things in there that won’t work properly, and you’ll get a null value injected into your partitions.  The setProperty method won’t let you set anything but Strings.

We have a couple more things to talk about, but we know enough to go have a look at the code.  The JSL is in UsingPartitionMapper.xml and the mapper itself is SamplePartitionMapper.java.  Our step is a batchlet that pretends to do some processing with the file injected into it and is called FakeFileProcessor.java for that reason.

The sample parts are here:  https://github.com/follisd/batch-samples