How to use Spring Batch Inheritance function

In the tutorial, JavaSampleApproach will introduce about inheritance feature of Spring Batch: Inheriting from a Parent Job & Inheriting from a Parent Step.

Related post:
How to use Intercepting Job Execution in Spring Batch

I. Technologies

– 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

II. Inheritance feature

Spring Batch provides Inheritance feature for Batch Job scope & Batch Job Step.
Inheriting from a Parent Job: It is helpful idea for jobs can share the similar configuration from a parent job.

Merging Lists: Some of Step configurable elements are lists; like . If parent & child Steps has a , then the child’s list will override the parent’s. For a child to have both listeners (an additional element from parent), every list element must use “merge” attribute.
In above example, the child Job with id=”job” has both listener: baseListeners & interceptingJobExecution

Inheriting from a Parent Step: When a group of Steps have similar configurations, parent Step is a good idea from which the concrete Steps can inherit properties.

“abstract” attribute is used to defined an Abstract Step which will not be instantiated. Abstract Step is just used only for extending.

III. Practices

Create a Spring Boot project with 2 batch jobs:
– 1 parent Job (abstract job)
– 1 abstract Step
– 1 child job has a Step which inherits from the abstract Step.

Spring batch inheritance

Step to do:
– Create Spring Boot project
– Create a Job Step
– Create JobExecutionListeners
– Configure Batch Job
– Create a Launch Controller
– Run & Check result

1. Create Spring Boot project

Create a Spring Boot project with needed dependencies:

2. Create a Job Step

Create a simple Job step with Reader, Writer and Processor:

3. Create JobExecutionListeners

– Create for a Parent Job:

– For a child Job, create listener:

4. Configure Batch Job

– Open file to configure batch job database:

– Enable Batch Job in main class:

5. Create a Launch Controller

6. Run & Check result

– Build & Run the project with Spring Boot App mode.
– Make a request for launch a Job: localhost:8080/launchjob

IV. Sourcecode


Related Posts

Got Something To Say:

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