Skip to content

Commit

Permalink
Polish "Throw exception when a named batch job does not exist"
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick committed Jun 29, 2023
1 parent 1bb1925 commit bfa8996
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.autoconfigure.batch;

import javax.annotation.PostConstruct;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -41,7 +43,6 @@
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobParametersNotFoundException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
Expand Down Expand Up @@ -112,6 +113,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 @@ -162,6 +175,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 @@ -179,19 +200,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));
if(this.jobExplorer != null && this.jobExplorer.getLastJobInstance(jobName) == null) {
throw ex;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -72,7 +71,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

/**
Expand All @@ -82,7 +80,7 @@
* @author Stephane Nicoll
* @author Vedran Pavic
* @author Kazuki Shimizu
* @author Akshay Dubey
* @author Scott Frederick
*/
@ExtendWith(OutputCaptureExtension.class)
class BatchAutoConfigurationTests {
Expand Down Expand Up @@ -378,18 +376,13 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
}

@Test
void testExecuteLocalAndNonConfiguredJob() {
this.contextRunner
.withUserConfiguration(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.batch.job.names:discreteLocalJob,nonConfiguredJob")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThrows(NoSuchJobException.class,()->{
context.getBean(JobLauncherApplicationRunner.class).run();
});
assertThat(context.getBean(JobRepository.class)
.getLastJobExecution("discreteLocalJob", new JobParameters())).isNotNull();
});
void nonConfiguredJobThrowsException() {
this.contextRunner.withUserConfiguration(JobConfiguration.class, EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.batch.job.names:nonConfiguredJob")
.run((context) -> assertThat(context).hasFailed()
.getFailure()
.getRootCause()
.hasMessageContaining("nonConfiguredJob"));
}

@Configuration(proxyBeanMethods = false)
Expand Down

0 comments on commit bfa8996

Please sign in to comment.