How to use Spring Batch Late Binding – Step Scope & Job Scope

Spring Batch Late Binding provides a solution for late binding Job’s Parameters with Beans’ properties. In the tutorial, JavaSampleApproach will introduce Spring Batch Late Binding cleary by a sample project.

Related Articles:
1. How to use Spring Batch Inheritance function
2. How to use Intercepting Job Execution in Spring Batch
3. How to start with Spring Batch using Spring Boot – Java Config
4. Spring Batch Partition for Scaling & Parallel Processing

I. Spring Batch Late Binding

See below configuration:

The configuration uses a traditional approach:${folder} & ${inputFileName}
folder & inputFileName must define in a configuration file, like application.properties:

Question: How to bind data between Job Parameters & Beans’ properties?
Answer: Spring Batch Late Binding with Job & Step attributes.

1. Step Scope

Spring Batch provides various form for late binding with Step attributes:

Step Scope is used to late binding because a bean can NOT be instantiated until the step starts.
Sample:

Launching the Job with .addString("input.file.name", fileName) on JobParameters, when step 1 starts then the bean tasklet1 will be instantiated with logs:

Because by default, Step Scope is not part of the Spring container, so we need declare the scope explicitly by a bean:

2. Job Scope

Job Scope provides some ways to pull value of beans’s properties from Job Execution Context & the Job Parameters.

Job scope is similar to Step scope But is a Scope for the Job context so there is only one instance per executing job.
Sample:

Launching the Job with .addString("input.file.name", fileName) on JobParameters, when step 2 starts then the bean tasklet2 will be instantiated. step 4 will reuse the instance tasklet2 from step 2
Logs:

II. Practice

Create a Spring Boot project has a Batch Job with 4 steps and:
tasklet1 bean has Step scope: write content to a File
tasklet2 bean has Job scope: read content form a File then displays on console.
– Launch the Job from a JobLauncherController that has a file name request paramenter for a JobParameter.

Spring Batch Late Binding - Overview job

Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE
– MySQL Database 1.4

Step to do

– Create a Spring Boot project
– Create StepTasklets
– Config Batch Job
– Create JobLauncherController
– Run & Check results

1. Create a Spring Boot project

Create a Spring Boot project with needed dependencies:
spring-boot-starter-batch
spring-boot-starter-web
mysql-connector-java

2. Create StepTasklets

– Create an AbstracTasklet.java:

– Create Tasklet1.java that extends AbstracTasklet:

– Create a Tasklet2.java that extends AbstracTasklet:

3. Config Batch Job

– Configure a Batch Job:

– Open application.properties file, configure Job’s Database & folder property:

– Enable Batch Proccessing in main class:
@EnableBatchProcessing
@ImportResource("classpath:batchjob.xml")

4. Create JobLauncherController

Create a JobLauncherController having @RequestParam("fileName") String fileName for late binding on beans:tasklet1 & tasklet2.

5. Run & Check result

– Run the project with Spring Boot App mode, then make a launch request:
http://localhost:8080/launchjob?fileName=1.txt
Logs:

III. Source Code

SpringBatchLateBinding


Related Posts


Got Something To Say:

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

*