Spring Batch + Spring Boot Java Config Example

Learn to create Spring batch job (with multiple steps) with Java configuration. It uses Spring Boot 2, Spring batch 4 and H2 database to execute batch job.

Table of Contents

Project Structure
Maven Dependencies
Add Tasklets
Spring Batch Configuration
Demo

Project Structure

In this project, we will create a simple job with 2 step tasks and execute the job to observe the logs. Job execution flow will be –

  1. Start job
  2. Execute task one
  3. Execute task two
  4. Finish job
Spring Batch Java Config Example
Spring Batch Java Config Example

Maven Dependencies

We need to include spring-boot-starter-batch dependency. Spring batch relies on job repository which is persistent data store. So we need one DB as well. I am using H2 (in-memory database) which integrates well with spring batch.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.howtodoinjava</groupId>
	<artifactId>App</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>App</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-batch</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>repository.spring.release</id>
			<name>Spring GA Repository</name>
			<url>http://repo.spring.io/release</url>
		</repository>
	</repositories>
</project>

Add Tasklets

The first step is to create few tasks which will be run in some sequence to form a job. In Spring batch, they are implemented as Tasklet.

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class MyTaskOne implements Tasklet {

	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception 
	{
		System.out.println("MyTaskOne start..");

	    // ... your code
	    
	    System.out.println("MyTaskOne done..");
	    return RepeatStatus.FINISHED;
	}    
}
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class MyTaskTwo implements Tasklet {

	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception 
	{
		System.out.println("MyTaskTwo start..");

	    // ... your code
	    
	    System.out.println("MyTaskTwo done..");
	    return RepeatStatus.FINISHED;
	}    
}

Spring Batch Configuration

This is major step where you define all the job related configurations and it’s execution logic.

package com.howtodoinjava.demo.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.howtodoinjava.demo.tasks.MyTaskOne;
import com.howtodoinjava.demo.tasks.MyTaskTwo;

@Configuration
@EnableBatchProcessing
public class BatchConfig {
	
	@Autowired
	private JobBuilderFactory jobs;

	@Autowired
	private StepBuilderFactory steps;
	
	@Bean
	public Step stepOne(){
	    return steps.get("stepOne")
	            .tasklet(new MyTaskOne())
	            .build();
	}
	
	@Bean
	public Step stepTwo(){
	    return steps.get("stepTwo")
	            .tasklet(new MyTaskTwo())
	            .build();
	}   
	
	@Bean
	public Job demoJob(){
	    return jobs.get("demoJob")
	    		.incrementer(new RunIdIncrementer())
	            .start(stepOne())
	            .next(stepTwo())
	            .build();
	}
}

Demo

Now our simple job 'demoJob' is configured and ready to be executed. I am using CommandLineRunner interface to execute the job automatically, with JobLauncher, when the application is fully started.

package com.howtodoinjava.demo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App implements CommandLineRunner
{
	@Autowired
	JobLauncher jobLauncher;
	
	@Autowired
	Job job;
	
	public static void main(String[] args) 
	{
		SpringApplication.run(App.class, args);
	}

	@Override
	public void run(String... args) throws Exception 
	{
		JobParameters params = new JobParametersBuilder()
					.addString("JobID", String.valueOf(System.currentTimeMillis()))
					.toJobParameters();
		jobLauncher.run(job, params);
	}
}

Notice the console logs.

o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=demoJob]] launched with 
the following parameters: [{JobID=1530697766768}]

o.s.batch.core.job.SimpleStepHandler     : Executing step: [stepOne]
MyTaskOne start..
MyTaskOne done..

o.s.batch.core.job.SimpleStepHandler     : Executing step: [stepTwo]
MyTaskTwo start..
MyTaskTwo done..

o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=demoJob]] completed with 
the following parameters: [{JobID=1530697766768}] and the following status: [COMPLETED]

Spring also automatically run batch jobs configured. To disable auto-run of jobs, you need to use spring.batch.job.enabled property in application.properties file.

spring.batch.job.enabled=false

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

19 thoughts on “Spring Batch + Spring Boot Java Config Example”

  1. Hi Lokesh,

    Thanking you for great support.

    I am using multiple remote data sources using spring jdbc, and i want store job information tables in my local db.

    Can you please suggest with example application.

    Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name ‘BATCH_JOB_INSTANCE’.

    Reply
  2. Hi Lokesh ,
    I am really new to spring boot ,
    When i add jdbc and mysql-connector to the dependency on “pom.xml” , and config database,user,password for mysql connection on “application.properties” ,when i run the app, i got an error “java.lang.IllegalStateException: Failed to execute CommandLineRunner” ,
    How to fix this , or can you suggest what to do .

    Best Regards
    Ardian

    Reply
  3. How to use Spring Cloud config server using spring batch. I would like my spring batch application to behave as a client and consume the properties defined in server config.

    Reply
  4. Description:

    Field jobLauncher in com.wipro.batch.SpringBatch1.App required a bean of type ‘org.springframework.batch.core.launch.JobLauncher’ that could not be found.

    Action:

    Consider defining a bean of type ‘org.springframework.batch.core.launch.JobLauncher’ in your configuration.

    Reply
    • Add spring.batch.job.enabled=false in application.properties file. The first time jobs are executed while initializing the beans. This property will prevent that first execution.

      Reply
  5. Hi Lokesh Gupta

    I am new to spring boot , would you please let me know how to run this via commpand prompt.
    may be if you can give me a command detail or script , would really help.

    Best Regards
    Jai

    Reply
  6. Hi,

    Getting below error for above code, can you please help me to sort it out.

    Description:

    Field jobLauncher in com.example.SpringBootMainClass required a bean of type ‘org.springframework.batch.core.launch.support.SimpleJobLauncher’ that could not be found.

    Action:

    Consider defining a bean of type ‘org.springframework.batch.core.launch.support.SimpleJobLauncher’ in your configuration.

    Reply
  7. In App class if you leave a blank run method still the steps run with run id of 1.
    If you do not add “spring.batch.job.enabled=false” to the application.properties file then the steps run twice.
    If you add “spring.batch.job.enabled=false” to the application.properties file then the steps run once.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.