Spring Batch Concepts, Part I

In the v0.5.x release of NixMash Spring we're going to add several features based on Spring Batch. With Spring Batch we can retrieve data on a recurring basis to populate Administrative Reports, for instance. This post will introduce several Spring Batch concepts as you'll find them in NixMash Spring. Spring Batch Concepts, Part II (III, IV?) will follow.

We begin with the new batch module and its initial construction. The Batch module contains a single demoJob here but will eventually house all Batch jobs and will be run at the command line with a –job=[jobname] parameter.

We'll determine which job we want to run with Spring's ability to use the Command Line as a Property Source shown in BatchLauncher shown below.

Then we're going to create a Spring Conditional Class for each job (very cool and which we'll cover in another post) and use it to determine if the job should fire and do its thing on application startup.

Job Properties and Other Parameters

Our jobs may require certain parameters to perform their work. We want to easily set and modify those properties as well as the interval of any repeating jobs. We'll create an external job.properties file for that purpose and add it as a Spring Property Source.

For demoJob we have a couple of properties along with a repeating interval value, all easy to change since they're in a text file on the server.

The Heart of the Job, Defined

Here's the heart of the job, defined in the demoJob Bean and in the step1 Bean. We use Spring's jobBuilderFactory and stepBuilderFactory to design our batch job. We have a jobListener component which, for our purposes, will simply announce when the job completes. We have two job steps, step1 and optionalStep which we'll run using a Spring JobExecutionDecider and our demo.job.param.iterations property shown above.

The Steps

If you look at the step1 bean above you'll see the heart of the Spring Batch Step construction as well. We read data, process the data, then write the data out. Not all three functions are required, but they are the norm. We add a promotionListener() bean because we want to pass a value from the step to multiple processes. Our JobStepListener will capture that value for handling.

Run it!

The Demo Job itself isn't of any interest. It follows the normal course of a Spring Batch Job Step in reading all NixMash Spring posts, putting the Post Title in all capital letters (the processing), then will write those titles to a CSV file. Here is the final result.

In our next Spring Batch Concepts post we'll go a bit deeper into what we covered here, with Conditionals, Job and Step Listeners, capturing job data values and probably one or two other awesome bits.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted November 28, 2016 03:56 PM EST

More Like This Post