How to configure Spring Batch Step for restart

In How to use Spring Batch Restartable Function, we had discussed about how to restart a Job. Today, JavaSampleApproach will guide you how to use additional configuration for step in case restart a Batch Job.

Related articles:
1. How to use Spring Batch Restartable Function
2. Spring Batch XML Config by Spring Boot


I. Technologies

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

II. Overview
1. Introduction

For default, when running a restartable Batch Job, a step is executed if it was FAILED in a previous execution and do NOT be invoked if it was COMPLETED.
Spring Batch also supports some specific configuration for Steps when restart a Job.
Setting a StartLimit: help control the number of times that a Step may be started.

allow-start-if-complete: in case restartable Job, steps with the configuration allow-start-if-complete=”true” are always be invoked to run.

2. Project Structure

In the tutorial, a Job will be created with 3 step: step1->step2->step3
– Step1 with no additional configuration
– Step2 is configured with allow-start-if-complete=”true”
– Step3 is configured with start-limit=”${limitstart}”

spring batch step restart - project structure

3. Step to do

– Create Spring Boot project
– Create Tasket for steps
– Configure Batch Job
– Implement Controller for Job Launcher
– Run & Enjoy Results
– How to manually restart a step when maximum start limit exceeded for step

III. Practice
1. Create Spring Boot project

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Press Next then Finish, a Spring Boot project will be created successfully.

Add needed dependencies:
spring-boot-starter-web
spring-boot-starter-batch
mysql-connector-java
Details:

2. Create Tasket for steps

Create 3 Tasklet for 3 steps
TaskletStep1: clean resources. It will empty folder: folderpath=C:\\readfile.

TaskletStep2: prepare data. It will create content for a file: filepath=C:\\readfile\\1.txt in case the file does Not exist before.

TaskletStep3: processing data. It reads the file: filepath=C:\\readfile\\1.txt then shows content on console.

3. Configure Batch Job

Create a batchjob.xml file:

Enable BatchJob configuration in main class:

Configure appliation.properties file:

4. Implement Controller for Job Launcher

Implement a Controller for launching a Job:

5. Run & Enjoy Results

Build & Run the project with Spring Boot App mode.

Step 3: Config: limitstart = 2 & sleeptime=10000

– Make request: http://localhost:8080/launchjob?jobId=1
Right after Step2 is completed, delete a file: C:\\readfile\\1.txt
Result: STEP1 & STEP2 are completed, STEP3 is failed because a file: C:\readfile\1.txt has been deleted.
Logs:

– Restart the Job by above request again: http://localhost:8080/launchjob?jobId=1
Right after Step2 is completed, delete a file: C:\\readfile\\1.txt
Result:
– STEP1 is not executed because it had been completed.
– STEP2 also run with completed status because it was configured with allow-start-if-complete=”true”
– STEP3 is Failed again because a file: C:\readfile\1.txt has been deleted.
Logs:

– Restart the Job by above request again: http://localhost:8080/launchjob?jobId=1
Result:
– STEP1 is not executed because it had been completed.
– STEP2 also run with completed status because it was configured with allow-start-if-complete=”true”
– STEP3 is Failed again because limitstart=2
Logs:

6. How to manually restart a step when maximum start limit exceeded for step

Solution 1: Reset start limit. In the case set: start limit = 3
– Restart the Job by above request again: http://localhost:8080/launchjob?jobId=1
Job is Completed with Logs:

Solution 2: Launch a new Job.
Make a request with jobId=2: http://localhost:8080/launchjob?jobId=2
Job is Completed with Logs:

IV. Sourcecode

SpringBatchRestartStepConfig


Related Posts


Got Something To Say:

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

*