Skip to content

Commit

Permalink
Remove support for multiple batch jobs
Browse files Browse the repository at this point in the history
Closes gh-25373
  • Loading branch information
mbhave committed Jun 15, 2022
1 parent fabe063 commit 55d6a87
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@
import org.springframework.util.StringUtils;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Batch. By default a
* Runner will be created and all jobs in the context will be executed on startup.
* {@link EnableAutoConfiguration Auto-configuration} for Spring Batch. If a single job is
* found in the context, it will be executed on startup.
* <p>
* Disable this behavior with {@literal spring.batch.job.enabled=false}).
* <p>
* Alternatively, discrete Job names to execute on startup can be supplied by the User
* with a comma-delimited list: {@literal spring.batch.job.names=job1,job2}. In this case
* the Runner will first find jobs registered as Beans, then those in the existing
* JobRegistry.
* If multiple jobs are found, a job name to execute on startup can be supplied by the
* User with : {@literal spring.batch.job.name=job1}. In this case the Runner will first
* find jobs registered as Beans, then those in the existing JobRegistry.
*
* @author Dave Syer
* @author Eddú Meléndez
Expand All @@ -74,9 +73,9 @@ public class BatchAutoConfiguration {
public JobLauncherApplicationRunner jobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
JobRepository jobRepository, BatchProperties properties) {
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(jobLauncher, jobExplorer, jobRepository);
String jobNames = properties.getJob().getNames();
String jobNames = properties.getJob().getName();
if (StringUtils.hasText(jobNames)) {
runner.setJobNames(jobNames);
runner.setJobName(jobNames);
}
return runner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ public Jdbc getJdbc() {
public static class Job {

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

public String getNames() {
return this.names;
public String getName() {
return this.name;
}

public void setNames(String names) {
this.names = names;
public void setName(String name) {
this.name = name;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
import java.util.Properties;

import jakarta.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -53,7 +54,6 @@
import org.springframework.core.Ordered;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered,

private JobRegistry jobRegistry;

private String jobNames;
private String jobName;

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

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

@PostConstruct
public void validate() {
if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) {
throw new IllegalArgumentException("Job name must be specified in case of multiple jobs");
}
}

public void setOrder(int order) {
this.order = order;
}
Expand All @@ -129,8 +136,8 @@ public void setJobRegistry(JobRegistry jobRegistry) {
this.jobRegistry = jobRegistry;
}

public void setJobNames(String jobNames) {
this.jobNames = jobNames;
public void setJobName(String jobName) {
this.jobName = jobName;
}

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

private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionException {
for (Job job : this.jobs) {
if (StringUtils.hasText(this.jobNames)) {
String[] jobsToRun = this.jobNames.split(",");
if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) {
if (StringUtils.hasText(this.jobName)) {
if (!this.jobName.equals(job.getName())) {
logger.debug(LogMessage.format("Skipped job: %s", job.getName()));
continue;
}
Expand All @@ -174,19 +180,15 @@ private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionEx
}

private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecutionException {
if (this.jobRegistry != null && StringUtils.hasText(this.jobNames)) {
String[] jobsToRun = this.jobNames.split(",");
for (String jobName : jobsToRun) {
try {
Job job = this.jobRegistry.getJob(jobName);
if (this.jobs.contains(job)) {
continue;
}
if (this.jobRegistry != null && StringUtils.hasText(this.jobName)) {
try {
Job job = this.jobRegistry.getJob(this.jobName);
if (!this.jobs.contains(job)) {
execute(job, jobParameters);
}
catch (NoSuchJobException ex) {
logger.debug(LogMessage.format("No job found in registry for job name: %s", jobName));
}
}
catch (NoSuchJobException ex) {
logger.debug(LogMessage.format("No job found in registry for job name: %s", this.jobName));
}
}
}
Expand Down

0 comments on commit 55d6a87

Please sign in to comment.