Skip to content

Commit 55d6a87

Browse files
committed
Remove support for multiple batch jobs
Closes gh-25373
1 parent fabe063 commit 55d6a87

File tree

5 files changed

+200
-45
lines changed

5 files changed

+200
-45
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@
4545
import org.springframework.util.StringUtils;
4646

4747
/**
48-
* {@link EnableAutoConfiguration Auto-configuration} for Spring Batch. By default a
49-
* Runner will be created and all jobs in the context will be executed on startup.
48+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Batch. If a single job is
49+
* found in the context, it will be executed on startup.
5050
* <p>
5151
* Disable this behavior with {@literal spring.batch.job.enabled=false}).
5252
* <p>
53-
* Alternatively, discrete Job names to execute on startup can be supplied by the User
54-
* with a comma-delimited list: {@literal spring.batch.job.names=job1,job2}. In this case
55-
* the Runner will first find jobs registered as Beans, then those in the existing
56-
* JobRegistry.
53+
* If multiple jobs are found, a job name to execute on startup can be supplied by the
54+
* User with : {@literal spring.batch.job.name=job1}. In this case the Runner will first
55+
* find jobs registered as Beans, then those in the existing JobRegistry.
5756
*
5857
* @author Dave Syer
5958
* @author Eddú Meléndez
@@ -74,9 +73,9 @@ public class BatchAutoConfiguration {
7473
public JobLauncherApplicationRunner jobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
7574
JobRepository jobRepository, BatchProperties properties) {
7675
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(jobLauncher, jobExplorer, jobRepository);
77-
String jobNames = properties.getJob().getNames();
76+
String jobNames = properties.getJob().getName();
7877
if (StringUtils.hasText(jobNames)) {
79-
runner.setJobNames(jobNames);
78+
runner.setJobName(jobNames);
8079
}
8180
return runner;
8281
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ public Jdbc getJdbc() {
4646
public static class Job {
4747

4848
/**
49-
* Comma-separated list of job names to execute on startup (for instance,
50-
* 'job1,job2'). By default, all Jobs found in the context are executed.
49+
* Job name to execute on startup. Must be specified if multiple Jobs are found in
50+
* the context.
5151
*/
52-
private String names = "";
52+
private String name = "";
5353

54-
public String getNames() {
55-
return this.names;
54+
public String getName() {
55+
return this.name;
5656
}
5757

58-
public void setNames(String names) {
59-
this.names = names;
58+
public void setName(String name) {
59+
this.name = name;
6060
}
6161

6262
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherApplicationRunner.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.Properties;
2626

27+
import jakarta.annotation.PostConstruct;
2728
import org.apache.commons.logging.Log;
2829
import org.apache.commons.logging.LogFactory;
2930

@@ -53,7 +54,6 @@
5354
import org.springframework.core.Ordered;
5455
import org.springframework.core.log.LogMessage;
5556
import org.springframework.util.Assert;
56-
import org.springframework.util.PatternMatchUtils;
5757
import org.springframework.util.StringUtils;
5858

5959
/**
@@ -86,7 +86,7 @@ public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered,
8686

8787
private JobRegistry jobRegistry;
8888

89-
private String jobNames;
89+
private String jobName;
9090

9191
private Collection<Job> jobs = Collections.emptySet();
9292

@@ -110,6 +110,13 @@ public JobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExpl
110110
this.jobRepository = jobRepository;
111111
}
112112

113+
@PostConstruct
114+
public void validate() {
115+
if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) {
116+
throw new IllegalArgumentException("Job name must be specified in case of multiple jobs");
117+
}
118+
}
119+
113120
public void setOrder(int order) {
114121
this.order = order;
115122
}
@@ -129,8 +136,8 @@ public void setJobRegistry(JobRegistry jobRegistry) {
129136
this.jobRegistry = jobRegistry;
130137
}
131138

132-
public void setJobNames(String jobNames) {
133-
this.jobNames = jobNames;
139+
public void setJobName(String jobName) {
140+
this.jobName = jobName;
134141
}
135142

136143
@Autowired(required = false)
@@ -162,9 +169,8 @@ protected void launchJobFromProperties(Properties properties) throws JobExecutio
162169

163170
private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionException {
164171
for (Job job : this.jobs) {
165-
if (StringUtils.hasText(this.jobNames)) {
166-
String[] jobsToRun = this.jobNames.split(",");
167-
if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) {
172+
if (StringUtils.hasText(this.jobName)) {
173+
if (!this.jobName.equals(job.getName())) {
168174
logger.debug(LogMessage.format("Skipped job: %s", job.getName()));
169175
continue;
170176
}
@@ -174,19 +180,15 @@ private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionEx
174180
}
175181

176182
private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecutionException {
177-
if (this.jobRegistry != null && StringUtils.hasText(this.jobNames)) {
178-
String[] jobsToRun = this.jobNames.split(",");
179-
for (String jobName : jobsToRun) {
180-
try {
181-
Job job = this.jobRegistry.getJob(jobName);
182-
if (this.jobs.contains(job)) {
183-
continue;
184-
}
183+
if (this.jobRegistry != null && StringUtils.hasText(this.jobName)) {
184+
try {
185+
Job job = this.jobRegistry.getJob(this.jobName);
186+
if (!this.jobs.contains(job)) {
185187
execute(job, jobParameters);
186188
}
187-
catch (NoSuchJobException ex) {
188-
logger.debug(LogMessage.format("No job found in registry for job name: %s", jobName));
189-
}
189+
}
190+
catch (NoSuchJobException ex) {
191+
logger.debug(LogMessage.format("No job found in registry for job name: %s", this.jobName));
190192
}
191193
}
192194
}

0 commit comments

Comments
 (0)