Skip to content

Commit

Permalink
Merge branch '2.7.x' into 3.0.x
Browse files Browse the repository at this point in the history
Closes gh-36531
  • Loading branch information
snicoll committed Jul 24, 2023
2 parents 73cc54a + 45f685f commit 259fac5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
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 All @@ -65,6 +64,7 @@
* @author Jean-Pierre Bergamin
* @author Mahmoud Ben Hassine
* @author Stephane Nicoll
* @author Akshay Dubey
* @since 2.3.0
*/
public class JobLauncherApplicationRunner
Expand Down Expand Up @@ -116,6 +116,11 @@ public void afterPropertiesSet() {
if (this.jobs.size() > 1 && !StringUtils.hasText(this.jobName)) {
throw new IllegalArgumentException("Job name must be specified in case of multiple jobs");
}
if (StringUtils.hasText(this.jobName)) {
if (!isLocalJob(this.jobName) && !isRegisteredJob(this.jobName)) {
throw new IllegalArgumentException("No job found with name '" + this.jobName + "'");
}
}
}

@Deprecated(since = "3.0.10", forRemoval = true)
Expand Down Expand Up @@ -173,6 +178,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.jobName)) {
Expand All @@ -187,14 +200,9 @@ private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionEx

private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecutionException {
if (this.jobRegistry != null && StringUtils.hasText(this.jobName)) {
try {
if (!isLocalJob(this.jobName)) {
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", this.jobName));
execute(job, jobParameters);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.autoconfigure.batch;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

Expand Down Expand Up @@ -80,6 +81,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -400,6 +403,53 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
.hasBean("customInitializer"));
}

@Test
void whenTheUserDefinesAJobNameAsJobInstanceValidates() {
JobLauncherApplicationRunner runner = createInstance("another");
runner.setJobs(Collections.singletonList(mockJob("test")));
runner.setJobName("test");
runner.afterPropertiesSet();
}

@Test
void whenTheUserDefinesAJobNameAsRegisteredJobValidates() {
JobLauncherApplicationRunner runner = createInstance("test");
runner.setJobName("test");
runner.afterPropertiesSet();
}

@Test
void whenTheUserDefinesAJobNameThatDoesNotExistWithJobInstancesFailsFast() {
JobLauncherApplicationRunner runner = createInstance();
runner.setJobs(Arrays.asList(mockJob("one"), mockJob("two")));
runner.setJobName("three");
assertThatIllegalArgumentException().isThrownBy(runner::afterPropertiesSet)
.withMessage("No job found with name 'three'");
}

@Test
void whenTheUserDefinesAJobNameThatDoesNotExistWithRegisteredJobFailsFast() {
JobLauncherApplicationRunner runner = createInstance("one", "two");
runner.setJobName("three");
assertThatIllegalArgumentException().isThrownBy(runner::afterPropertiesSet)
.withMessage("No job found with name 'three'");
}

private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
mock(JobExplorer.class), mock(JobRepository.class));
JobRegistry jobRegistry = mock(JobRegistry.class);
given(jobRegistry.getJobNames()).willReturn(Arrays.asList(registeredJobNames));
runner.setJobRegistry(jobRegistry);
return runner;
}

private Job mockJob(String name) {
Job job = mock(Job.class);
given(job.getName()).willReturn(name);
return job;
}

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

Expand Down

0 comments on commit 259fac5

Please sign in to comment.