How to use Spring Batch Tasklet

Spring Batch provides the Tasklet interface, to process a single task, such as clean or init resources before or after any step running. In the tutorial, JavaSampleApproach will guide you how to use Spring Batch Tasklet.

Related Posts:
How to start with Spring Batch using Spring Boot – Java Config
Spring Batch XML Config by Spring Boot
Spring Batch Job with Parallel Steps
Spring Batch – Programmatic Flow Decision
How to import CSV data to PostgreSQL Database using Spring Batch Job
Spring XD Option Module – Batch Job with PostgreSQL Datasource
How to use Spring Batch Restartable Function

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
1. Project Structure

In the tutorial, we create a Job with 2 step:step1 -> step 2.
step1 processes a file C:\\readfile\\1.txt.
step2 is used to delete the file after processing by step1. So step2 is implemented by Tasklet.

Spring Batch tasklet project structure

2. Step to do

– Create Spring Boot project
– Implement Reader & Writer for step1

– Implement Tasklet for step2

– Config a Batch Job
– Implement JobLauncherController
– Run & Enjoy Results

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:

2. Implement Reader & Write for step1

– Implement Reader:

– Implement Writer:

3. Implement Tasklet for step2

Create TaskletStep by implement: Tasklet interface. TaskletStep just deletes a file: C:\\readfile\\1.txt after step1‘s processing

4. Config a Batch Job

Configure a Job with 2 step: step1->step2.

5. Implement JobLauncherController

Implement JobLauncherController

6. Run & Enjoy Results

Build & Run the project with Spring Boot App mode.
Launch the Job by a request:

IV. Sourcecode


By grokonez | February 7, 2017.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

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