Search Tutorials

Spring Batch Tasklet - Hello World Example | JavaInUse

Spring Batch Tasklet - Hello World example

A Step is a domain object that encapsulates an independent, sequential phase of a batch job and contains all of the information necessary to define and control the actual batch processing.

Steps can be processed in either of the following two ways.
  • Chunk
  • Tasklet
The Tasklet which is a simple interface with just one method execute. Using this we can perform single tasks like executing queries, deleting files. In a previous post we had seen the differences between Chunk and Tasklet processing. In this post we execute a simple tasklet for deleting a file.

Spring Batch - Table Of Contents

Spring Batch Hello World example-Write data from csv to xml file Spring Boot Batch Simple example Spring Batch - Difference between Step, Chunk and Tasklet Spring Batch Tasklet - Hello World example Spring Boot + Batch + Task Scheduler Example

Lets Begin-

The maven project we will be creating is as follows-

The pom.xml with spring batch dependencies is as follows-

<project xmlns="" xmlns:xsi=""



		<!-- Spring Batch dependencies -->



We create a class that implements the Tasklet interface and write logic to delete all the files from a folder.
package com.javainuse.tasklet;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

public class FileDeletingTasklet implements Tasklet, InitializingBean {

	private Resource directory;

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(directory, "directory must be set");

	public RepeatStatus execute(StepContribution contribution,
			ChunkContext chunkContext) throws Exception {

		File dir = directory.getFile();

		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			boolean deleted = files[i].delete();
			if (!deleted) {
				throw new UnexpectedJobExecutionException(
						"Could not delete file " + files[i].getPath());
			} else {
				System.out.println(files[i].getPath() + " got deleted");
		return RepeatStatus.FINISHED;

	public Resource getDirectory() {
		return directory;

	public void setDirectory(Resource directory) { = directory;

Next define the batch configuration batch-task.xml for creating the tasklet as follows-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:batch="" xmlns:xsi=""

	<!-- Batch Config -->
	<bean id="jobRepository"
		<property name="transactionManager" ref="transactionManager" />

	<bean id="transactionManager"
		class="" />

	<bean id="jobLauncher"
		<property name="jobRepository" ref="jobRepository" />

	<bean id="fileDeletingTasklet" class="com.javainuse.tasklet.FileDeletingTasklet">
		<property name="directory" value="file:testdelete/deletefolder/" />

	<!-- Define Tasklet -->
	<job id="deleteTask" xmlns="">
		<step id="deleteDir">
			<tasklet ref="fileDeletingTasklet" />

Finally we load the Batch configuration and execute the tasklet.
package com.javainuse;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;

public class MainApp {

	public static void main(String[] args) {
		MainApp obj = new MainApp();

	private void runBatchTask() {

		//load the batch config file
		String[] batchConfig = { "batch-task.xml" };
		ApplicationContext context = new ClassPathXmlApplicationContext(

		JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

		//load the job bean
		Job job = (Job) context.getBean("deleteTask");

		try {

			JobExecution execution =, new JobParameters());
			System.out.println("Job Exit Status : " + execution.getStatus());
		} catch (Exception e) {


On Running any file existing in the deletefolder gets deleted.


Download Source Code

Download it -
Spring Batch Tasklet Hello World Example

See Also