[JAVA] Create a simple on-demand batch with Spring Batch


Create a simple Spring Batch and batch launch it from a web request.


Detailed procedure

Project creation

Add spring-boot-starter-batch to the dependency to use SpringBatch. Also, since DB is used for Job management, an appropriate JDBC is also added.



Batch creation

** Basically created by referring to here **

Batch actual processing

Create an appropriate processing class as @Service

public class MyBatchService {

	public void execute(Long id, String name, Date reqDate) throws InterruptedException {
		//Appropriate batch processing

Batch settings

The liver of batch processing. This time, easily create one one-step (tasklet) job. Set in Java configuration class.

@EnableBatchProcessing //(1)
public class BatchConfig {

	private JobBuilderFactory jobBuilderFactory; //(2)
	private StepBuilderFactory stepBuilderFactory; //(2)

	private MyBatchService service;

	public JobLauncher jobLauncher1(JobRepository jobRepository) { //(2),(3)
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		//The number of concurrent executions is 3.Wait for more queues.
		taskExecutor.setCorePoolSize(3); //(4)
		// java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized
		taskExecutor.initialize(); //(5)

		SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

		return jobLauncher;

	public Job job(Step step1) { //(6)
		return jobBuilderFactory.get("batchjob")
					.incrementer(new RunIdIncrementer())

	public Step step1(Tasklet tasklet1) { //(6)
		return stepBuilderFactory.get("step1")

	@StepScope //(7)
	public Tasklet tasklet1( //(6)
			@Value("#{jobParameters['id']}") Long id, //(8)
			@Value("#{jobParameters['name']}") String name,
			@Value("#{jobParameters['reqDate']}") Date reqDate
			) {
		MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
		tasklet.setArguments(new Object[] {id, name, reqDate});

		return tasklet;
(1) Indicates that it is a Spring Batch setting@EnableBatchProcessingTo@ConfigurationGrant with.
(2) Some beans are included in the context by default and can be autowired.
(3) Job execution environment settings.
(4) Number of concurrent executions with CorePoolSize(Number of threads)To set. If it exceeds this, it will wait in the queue and will be executed sequentially as soon as threads become available.
(5) I have to initialize"java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized"Exception occurs.
(6) Job, Step,Tasklet settings.
(7) If you set run-time parameters, you cannot create beans at startup.@StepScopeTo change the timing of bean creation.
(8) Settings to retrieve objects from jobParameters.
(9) Run with Invoke.

Batch call

public class MyServletService {

	private JobLauncher jobLauncher1;
	private Job job;

	public void request(Long id, String name) throws JobExecutionException {
		// (10)
		JobParameters params = new JobParametersBuilder()
								.addLong("id", id)
								.addString("name", name)
								.addDate("reqDate", Calendar.getInstance().getTime())

		jobLauncher1.run(job, params);
(10) The parameters to be passed to Job are collectively passed to JobParameters.

Startup settings

Add settings to application.yml.


  datasource: # (11)
    url: jdbc:postgresql://localhost:5432/kurukuruz1
    username: u-s-e-r
    password: p-a-s-s
    driver-class-name: org.postgresql.Driver
    initialize-schema: always # (12)
      enabled: false # (13)
(11) Set the connection information of the DB to be used.
(12) Setting to execute DDL of the table used by Spring Batch. By defaultembeddedSo, when using PostgreSQL, DDL is not executed, so change it explicitly.
(13) By defaulttrueJob is executed when Spring is started, but it cannot be executed at startup because there are no Job Parameters at startup. Therefore, change it explicitly.

Reference site

Spring Batch re-introduction --I tried to organize how to use it myself --Qiita BLOG.IK.AM --Mistakenly misunderstood ThreadPoolTaskExecutor settings Spring Batch sample code (Java / Gradle) --Qoosky [Spring Batch] Do not execute Job automatically Control method when jobs other than the specified Job are automatically executed --Qiita Kojioniruku-Hello World in Spring Boot Batch

Recommended Posts

Create a simple on-demand batch with Spring Batch
Create a simple search app with Spring Boot
Create a simple demo site with Spring Security with Spring Boot 2.1
Create a simple web application with Dropwizard
[Rails withdrawal] Create a simple withdrawal function with rails
Create a simple bar chart with MPAndroidChart
Create a simple bulletin board with Java + MySQL
Create a simple batch processing framework in Eclipse.
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Create a playground with Xcode 12
Create microservices with Spring Boot
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ③ ~ Add Validation ~
Create a Hello World web app with Spring framework + Jetty
Let's make a simple API with EC2 + RDS + Spring boot ①
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Create a Vue3 environment with Docker!
Create an app with Spring Boot 2
Create an app with Spring Boot
Create exceptions with a fluid interface
Create a Maven project with a command
Implement a simple Web REST API server with Spring Boot + MySQL
Create a simple web server with the Java standard library com.sun.net.httpserver
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ④ ~ Customize error messages ~
I made a simple search form with Spring Boot + GitHub Search API.
Create a jar file with the command
[Rails6] Create a new app with Rails [Beginner]
Create a GUI JSON Viewer with Ruby/GTK3
Create a MySQL environment with Docker from 0-> 1
Create Spring Boot-gradle-mysql development environment with Docker
Create a temporary class with new Object () {}
[docker] [nginx] Make a simple ALB with nginx
[Rails] Let's create a super simple Rails API
[Rails 5] Create a new app with Rails [Beginner]
Create a Spring Boot app development project with the cURL + tar command
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ② ~ Screen and function creation ~
How to create a server executable JAR and WAR with Spring gradle
Create a simple gateway server by setting masquerade with firewall-cmd of CentOS8
Steps to create a simple camel app using Apache Camel Spring Boot starters
[Memo] Create a CentOS 8 environment easily with Docker
Create a CSR with extended information in Java
[Rails] rails new to create a database with PostgreSQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
[Tutorial] Spring Batch
Create a Spring Boot application using IntelliJ IDEA
[Java] Create a collection with only one element
Create a team chat with Rails Action Cable
Periodically update DB with Spring Batch and MyBatis
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
Create a SandBox account with fastlane spaces ip
Create a multi-key map with the standard library
Create your own Utility with Thymeleaf with Spring Boot
How to create an Excel form using a template file with Spring MVC
Show a simple Hello World with SpringBoot + IntelliJ
Create Spring Boot environment with Windows + VS Code
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Creating a common repository with Spring Data JPA
Build a WEB system with Spring + Doma + H2DB