How to use Spring Batch Restartable Function

SpringBatch provides a restartable mechanics with a Job, in the How to use Spring Batch Restartable Function tutorial, JavaSampleApproach will present to you the principle of this function with SpringBoot.

Related Post:
1. How to configure Spring Batch Step for restart
2. How to start with Spring Batch using Spring Boot – Java Config
3. Spring Batch XML Config by Spring Boot
4. Spring Batch Job with Parallel Steps
5. How to import CSV data to PostgreSQL Database using Spring Batch Job


I. Technologies

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

II. Overview

Create a Batch Job which has a simple step with a Reader & Writer.
Reader reads a list file-paths then pass them to a Writer. The Write uses file paths to process content of each file.
With created JOB, test a restartable function base on FileNotFound Error.
Note: a Batch Job is created with restartable default. Spring Batch also supports configuration to prevent the restartable function.

1. Project Structure

spring boot springbatch restartable project structure

2. Step to do

– Create SpringBoot project
– Create simple step
– Config a Batch Job
– Create a Job Launch controller
– Config Batch Job Database
– Enable Batch Job
– Scenario Test

III. Spring Batch Restartable Practices
1. Create a SpringBoot 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.

– Open pom.xml file, add Web MVC, PostgreSQL and Spring Batch dependencies:

2. Create classes Reader & Writer for simple step

Create a Reader class:

Create a Writer class:

3. Config a Batch Job

Config a Simple Batch Job:

4. Create a Job Launch controller

– Create a simple Controller for Job Launching.

5. Config Batch Job Database

Config DataBase’s info for Spring Batch:

6. Enable Batch Job

In main class of Spring Boot project, use @EnableBatchProcessing to enable Spring Batch.

7. Scenario Test

– Build & Run the Spring Boot Batch Job.
The Reader will forward a file name: C:\\readfile\\1.txt to Writer. Delete a file C:\\readfile\\1.txt, then makes a launch request: http://localhost:8080/launchjob
A Job Instance is created with Empty Parameters:

Investigate Server Logs:
Result FAIL:

Cause:

– Now create a C:\readfile\1.txt with content:

Then restart the Job Instance (with parameters: [{}]) again by http://localhost:8080/launchjob

Result: COMPLETED

Make the launching request again: http://localhost:8080/launchjob, result:

IV. Prevent Restartable

SpringBatch also supports a config to prevent restartable ability of a Job even if a Job Instance’s Execution with FAIL status.

How to configure? Just use a little code strong>.preventRestart(), details with BatchConfig class:

Build & Run Spring Boot project again, delete C:\\readfile\\1.txt file.
Make a request: http://localhost:8080/launchjob, -> Result: FAIL with Log details:

Create a file: C:\\readfile\\1.txt, then makes request http://localhost:8080/launchjob again, result FAIL:

V. Sourcecode

SpringBatchRestartableFunction


Related Posts



Got Something To Say:

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

*