Sunday, October 8, 2017

Spring Batch Decision with Spring Boot / Java Configuration Example (with equivalent XML)

I have a spring batch application using spring boot and java configuration.

Spring Boot / Spring Boot Starter Batch Version 1.5.6.RELEASE
Oracle Java 8

The spring batch application was working fine with a job with three steps - Each of the three steps had a reader/processor/writer where it was reading from the DB, processing db record specific metadata in the processor, and then writing the data records.  A job listener is doing a beforeJob and afterJob database record writing and updating for auditing purposes.  Each of the steps are doing something specific for each type of data being processed.

The Java config looked like the below for the Job.
Job cherryShoeJob(JobBuilderFactory jobBuilderFactory, CherryShoeListener jobListener,
  @Qualifier("noOpStep") Step noOpStep,
  @Qualifier("step1") Step step1, @Qualifier("step2") Step step2, @Qualifier("step3") Step step3) {
  return jobBuilderFactory.get("cherryShoeJob").incrementer(new RunIdIncrementer()).listener(jobListener)

Instead of using a scheduler to determine if a job was to be ran, a cron job is being used to call the executable jar at a scheduled time.  This was so the spring batch application didn't always have to be running on the server; instead it would start, run, and have the process completely stop.

We needed to add a second job.  Both jobs did not run at the same scheduled times.  The obvious answer would have been to switch the Jobs to use the scheduler; that would have been easy to solve; just schedule each Job with a specific time to run.  But that requires the spring batch application to constantly be running.  Because of the need to have the application start, run, then stop completely, this needed to be solved a different way.  I decided on this:

The solution below makes this work for one job.  A second job could easily be added via the same solution.

1.  add specific configuration for the job to be disabled.  If I wanted the job to run, then pass in the optional command line argument "--cherryshoe.job1.job.enabled=true" to enable the job.
# Overwrite the default cherryshoe.job1.job.enabled configuration property with a command line arguments "--cherryshoe.job1.job.enabled=true" to enable this job.
# Command line always have preference over the default configuration options.

java -jar cherryshoe-batch.jar --cherryshoe.job1.job.enabled=true

2.  Create a JobExecutionDecider to determine if the job should run or not.  This was determined by the cherryshoe.job1.job.enabled value, or the overridden value via command line argument.  If the job was disabled the FlowExecutionStatus would be STOPPED, if enabled the FlowExecutionStatus would be UNKNOWN.

public class CherryShoeExecutionDecider implements JobExecutionDecider {

 private static final Logger LOGGER = LoggerFactory.getLogger(CherryShoeExecutionDecider.class);

 protected Boolean jobEnabled;

 public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
  FlowExecutionStatus status = FlowExecutionStatus.STOPPED;
  if (jobEnabled)
   status = FlowExecutionStatus.UNKNOWN;
    "Job enabled[" + jobEnabled + "],[ flowExecutionStatus[" + status.getName() + "]");

  return status;


3.  Create a "No Op" Step and a "No Op" Tasklet.  My new first step would be a no-op step just to be able to use a Decider to determine to continue running the Job, or stop.  If the job should continue, then it goes through the original three steps. If the job should stop, then jump to the no-op step again, just so we can end.
public class NoOpStepConfig {

 private static final Logger LOGGER = LoggerFactory.getLogger(NoOpStepConfig.class);

  * Step noOpStep
  * @param stepBuilderFactory
  * @return
 Step noOpStep(StepBuilderFactory stepBuilderFactory, NoOpTasklet noOpTasklet) {
  LOGGER.debug("NoOp Step processing...");
  return stepBuilderFactory.get("noOpStep").tasklet(noOpTasklet).build();

public class NoOpTasklet implements Tasklet {

 private Logger logger = LoggerFactory.getLogger(NoOpTasklet.class);

 public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
  logger.debug("NoOp Tasklet processing...");
  return RepeatStatus.FINISHED;


4.  The new Java config looked like the below for the Job.
 Job cherryShoeJob(JobBuilderFactory jobBuilderFactory, CherryShoeListener jobListener,
   CherryShoeExecutionDecider jobExecutionDecider, @Qualifier("noOpStep") Step noOpStep,
   @Qualifier("step1") Step step1, @Qualifier("step2") Step step2, @Qualifier("step3") Step step3) {

  // Since we are using command line argument values to determine if a Job
  // should run or not (vs using a scheduler to determine that),
  // we must solve this by: 1. Step 1 is a no-op step just so we can use a
  // Decider to determine if we should continue running the Job, or stop.
  // If we continue
  // running the job, then it goes through each notification type step. If
  // we stop, then we jump to the no-op step again, just so we can end.
   return jobBuilderFactory.get("cherryShoeJob").incrementer(new

XML Configuration
Step 2 - 4 above can be roughly converted into the XML configuration below.  NOTE: I did not test this with an XML config.
<!-- 2. CherryShoeExecutionDecider determines the FlowExecutionStatus of STOPPED or UNKNOWN depending on if if cherryshoe.job1.job.enabled is true or false -->
<beans:bean id="cherryShoeExecutionDecider" class="com.cherryshoe.batch.decider.CherryShoeExecutionDecider"/>

<!-- 3. NoOpTasklet does nothing -->
<beans:bean id="noOpTasklet" class="com.cherryshoe.batch.tasklet.NoOpTasklet" />

<!-- 4.  Job Configuration -->
<job id="cherryShoeJob">
    <!-- 3. NoOpStep does nothing but use NoOpTasklet -->
    <step id="noOpStep" next="jobExecutionDecider">
        <tasklet ref="noOpTasklet" />
    <decision id="jobExecutionDecider" decider="cherryShoeExecutionDecider">
        <next on="STOPPED" to="noOpStep" />
        <next on="UNKNOWN" to="step1" />
    <step id="step1" next="step2" >
    <step id="step2" next="step3">

    <step id="step3">

These were very helpful articles:

No comments:

Post a Comment

I appreciate your time in leaving a comment!