"spring.batch.job.names" Does Not Run Jobs In Order Specified
Bug description
The property spring.batch.job.names does not run the jobs in the order of the jobs listed.
I expected the comma-separated values would maintained order, but they do not. The documentation does not state whether the property should maintain order or not.
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.batch.running-jobs-on-startup
The property: --spring.batch.job.names=JOB_1,JOB_2 runs the jobs in any order:
JOB_1 --> JOB_2
or
JOB_2 --> JOB_1
Environment
- Spring: 4.3.4
- Java: 1.8.0_261
- Maven: 3.6.3
Steps to reproduce
- Create a Spring Batch application with at least 2 jobs.
- Run the application with
--spring.batch.job.names=JOB_1,JOB_2
Expected behavior The application ALWAYS runs JOB_1, followed by JOB_2.
Actual behavior The application runs the jobs in unpredictable order. Sometimes JOB_1 runs first, sometimes JOB_2 runs first.
The appears to be due to the fact that Spring locates the Job Beans from the HashMap of all Beans. The HashMap is unordered and can locate and return the Job Beans in any order.
Minimal Complete Reproducible example
- Copy
pom.xmlandBatchProcessingApplication.javashown below. - Run
mvn clean package - Run
java -jar target/springbatch-bug-1.0-SNAPSHOT.jar --spring.batch.job.names=jobB,jobA - The logs show
jobAis ran beforejobB.
2022-02-06 10:56:29.768 INFO 5924 --- [main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=jobA]] launched with the following parameters: [{}]
2022-02-06 10:56:29.810 INFO 5924 --- [main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step]
2022-02-06 10:56:29.826 INFO 5924 --- [main] o.s.batch.core.step.AbstractStep : Step: [step] executed in 16ms
2022-02-06 10:56:29.831 INFO 5924 --- [main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=jobA]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 34ms
2022-02-06 10:56:29.839 INFO 5924 --- [main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=jobB]] launched with the following parameters: [{}]
2022-02-06 10:56:29.845 INFO 5924 --- [main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step]
2022-02-06 10:56:29.849 INFO 5924 --- [main] o.s.batch.core.step.AbstractStep : Step: [step] executed in 4ms
2022-02-06 10:56:29.854 INFO 5924 --- [main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=jobB]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 12ms
Example Files
src/main/java/org/example/BatchProcessingApplication.java
package org.example;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
@SpringBootApplication
public class BatchProcessingApplication {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(BatchProcessingApplication.class, args)));
}
@Bean
public Job jobA(Step step) {
return jobBuilderFactory.get("jobA")
.flow(step)
.end()
.build();
}
@Bean
public Job jobB(Step step) {
return jobBuilderFactory.get("jobB")
.flow(step)
.end()
.build();
}
@Bean
public Step step() {
return stepBuilderFactory.get("step")
.<String, String> chunk(1)
.reader(() -> null)
.writer((list) -> {})
.build();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>springbatch-bug</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@ryanlevell Thank you for your detailed analysis, I experience the same problem.
There is a workaround, but it feels like we are hacking the framework: https://stackoverflow.com/a/52407429
I'd really appreciate it if the jobs would be executed in the order they are specified in the property spring.batch.job.names.
Thank you for opening this issue. However, this is a Spring Boot property and we have no control over it in Spring Batch. Please open this issue against Spring Boot for more details about the expected behaviour or if you want to request a feature. Thank you upfront.
-
i have spring batch app with multiple jobs,
-
add dependencies : implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-task', version: '3.0.1'//
-
start app
-
error : Caused by: java.lang.IllegalArgumentException: Job name must be specified in case of multiple jobs at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.validate(JobLauncherApplicationRunner.java:116) ~[spring-boot-autoconfigure-3.0.1.jar:3.0.1]
-
i want to set run order for job without error
-
Should I put only one job in Spring Batch, create as many Spring Batch apps as the number of jobs, and create and execute schedules in spring cloud data flow?
-
help me pls...
In Spring Boot 3.0 If you have more than one job in the context, you must specify the job name at startup that you want to launch using the spring.batch.job.name property. You can read more about it here: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.batch.running-jobs-on-startup.
thanks for your help . and i have a question.
-
batch app - build.gradle set
- implementation 'org.springframework.boot:spring-boot-starter-batch'
- implementation 'org.springframework.cloud:spring-cloud-starter-task'
-
batch run
-
error - No qualifying bean of type 'org.springframework.transaction.TransactionManager' available: expected single matching bean but found 2: transactionManager,springCloudTaskTransactionManager
-
help me pls...
@sangbinlee If you have a question, please use StackOverflow and tag your question with spring-batch. We would like to keep the issue tracker exclusively for bug reports and feature requests, see SUPPORT.md. Thank you.
thanks for your help .