Spring Batch – Programmatic Flow Decision

In the article, JavaSampleApproach will introduce about Programmatic Flow Decisions in Spring Batch.

Related Post: Spring Batch

A. Concept

Spring Batch provides mechanics for controlling the flow steps of batch job: JobExecutionDecider interface & decision tag

1. JobExecutionDecider

The interface allowing for programmatic access to the decision on what the status of a flow should be.
In here we need overwrite the function: FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution): Strategy for branching an execution based on the state of an ongoing. The return value will be used as a status to determine the next step in the job.

2. “decision” tag

“decision” tag specify the decider to use as well as all of the transitions

B. Practice

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview

The tutorial, JavaSampleApproach create a Batch job with 3 steps, step 1 executing then go to decision for next step.
In the decision, it just checks a value of random integer number, if the integer is odd, it will return a FAILED status then go to step 2 for processing. Else if the integer number is even, decision will return COMPLETED status, and next step is step 3.

Always, the next step after step 2 is step 3.

programatics flow decision

1. Project Structure

javasampleapproach spring batch programmatics flow decision

2. Step to do

– Create Spring Boot project
– Add needed dependencies
– Create Batch Steps
– Create Flow Decision
– Create Launch Controller
– Config batch job
– Config Data Source for batch job
– Run & Check result

III. Practice
1. Create Spring Boot project

It is very simple step, open Spring Tool Suite, File->New->Spring Starter Project, input project info:
spring batch programmatics flow decision

Press Next then Finish, Spring Boot project will be created successfully.

2. Add needed dependencies

Need add Spring Boot Batch Job dependencies and Spring Web dependency.
Open pom.xml, add dependencies:

3. Create Batch Steps

Create 3 steps:
– Step 1

– Step 2:

– Step 3:

4. Create Flow Decision

– Create a simple Programmatics Flow Decision

5. Create Launch Controller

– Create a Controller for launch a job:

6. Config batch job

Create a batchjob.xml under: src/main/resources, config a batch job:
programatics flow decision

– Enable BatchProcessing by :

7. Config Data Source for Batch Job

Open application.properties, add info:

8. Run & Check result

Build Spring Boot project: mvn clean install
Run Project: mvn spring-boot:run

– Make some requests with url: http://localhost:8080/launchjob

Result in case decision randon integer has even value:

Result in case decision randon integer has odd value:

IV. Source code


By grokonez | December 11, 2016.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *