Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JobLauncherApplicationRunner returns a success exit code even when no jobs have been run #36060

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -66,6 +68,7 @@
* @author Jean-Pierre Bergamin
* @author Mahmoud Ben Hassine
* @author Stephane Nicoll
* @author Akshay Dubey
* @since 2.3.0
*/
public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered, ApplicationEventPublisherAware {
Expand Down Expand Up @@ -111,6 +114,18 @@ public JobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExpl
this.jobRepository = jobRepository;
}

@PostConstruct
public void validate() {
if (StringUtils.hasText(this.jobNames)) {
String[] jobsToRun = this.jobNames.split(",");
for(String jobName: jobsToRun) {
if (!isLocalJob(jobName) && !isRegisteredJob(jobName)) {
throw new IllegalArgumentException("No job instances were found for job name [" + jobName + "]");
}
}
}
}

public void setOrder(int order) {
this.order = order;
}
Expand Down Expand Up @@ -161,6 +176,14 @@ protected void launchJobFromProperties(Properties properties) throws JobExecutio
executeRegisteredJobs(jobParameters);
}

private boolean isLocalJob(String jobName) {
return this.jobs.stream().anyMatch((job) -> job.getName().equals(jobName));
}

private boolean isRegisteredJob(String jobName) {
return this.jobRegistry != null && this.jobRegistry.getJobNames().contains(jobName);
}

private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionException {
for (Job job : this.jobs) {
if (StringUtils.hasText(this.jobNames)) {
Expand All @@ -178,16 +201,10 @@ private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecut
if (this.jobRegistry != null && StringUtils.hasText(this.jobNames)) {
String[] jobsToRun = this.jobNames.split(",");
for (String jobName : jobsToRun) {
try {
if(isRegisteredJob(jobName) && !isLocalJob(jobName)) {
Job job = this.jobRegistry.getJob(jobName);
if (this.jobs.contains(job)) {
continue;
}
execute(job, jobParameters);
}
catch (NoSuchJobException ex) {
logger.debug(LogMessage.format("No job found in registry for job name: %s", jobName));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* @author Stephane Nicoll
* @author Vedran Pavic
* @author Kazuki Shimizu
* @author Akshay Dubey
*/
@ExtendWith(OutputCaptureExtension.class)
class BatchAutoConfigurationTests {
Expand Down Expand Up @@ -374,6 +375,17 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
.hasBean("customInitializer"));
}

@Test
void testNonConfiguredJobThrowsException() {
this.contextRunner
.withUserConfiguration(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.batch.job.names:discreteLocalJob,nonConfiguredJob")
.run((context) -> {
assertThat(context).hasFailed().getFailure().getRootCause()
.hasMessageContaining("nonConfiguredJob");
});
}

@Configuration(proxyBeanMethods = false)
protected static class BatchDataSourceConfiguration {

Expand Down