Skip to content

Commit 1bb1925

Browse files
itsAkshayDubeyscottfrederick
authored andcommitted
Throw exception when a named batch job does not exist
See spring-projectsgh-36060
1 parent 9e1f2c4 commit 1bb1925

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @author Jean-Pierre Bergamin
6767
* @author Mahmoud Ben Hassine
6868
* @author Stephane Nicoll
69+
* @author Akshay Dubey
6970
* @since 2.3.0
7071
*/
7172
public class JobLauncherApplicationRunner implements ApplicationRunner, Ordered, ApplicationEventPublisherAware {
@@ -187,6 +188,9 @@ private void executeRegisteredJobs(JobParameters jobParameters) throws JobExecut
187188
}
188189
catch (NoSuchJobException ex) {
189190
logger.debug(LogMessage.format("No job found in registry for job name: %s", jobName));
191+
if(this.jobExplorer != null && this.jobExplorer.getLastJobInstance(jobName) == null) {
192+
throw ex;
193+
}
190194
}
191195
}
192196
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.batch.core.explore.JobExplorer;
3939
import org.springframework.batch.core.job.AbstractJob;
4040
import org.springframework.batch.core.launch.JobLauncher;
41+
import org.springframework.batch.core.launch.NoSuchJobException;
4142
import org.springframework.batch.core.repository.JobRepository;
4243
import org.springframework.beans.factory.annotation.Autowired;
4344
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -71,6 +72,7 @@
7172

7273
import static org.assertj.core.api.Assertions.assertThat;
7374
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
75+
import static org.junit.jupiter.api.Assertions.assertThrows;
7476
import static org.mockito.Mockito.mock;
7577

7678
/**
@@ -80,6 +82,7 @@
8082
* @author Stephane Nicoll
8183
* @author Vedran Pavic
8284
* @author Kazuki Shimizu
85+
* @author Akshay Dubey
8386
*/
8487
@ExtendWith(OutputCaptureExtension.class)
8588
class BatchAutoConfigurationTests {
@@ -374,6 +377,21 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
374377
.hasBean("customInitializer"));
375378
}
376379

380+
@Test
381+
void testExecuteLocalAndNonConfiguredJob() {
382+
this.contextRunner
383+
.withUserConfiguration(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class)
384+
.withPropertyValues("spring.batch.job.names:discreteLocalJob,nonConfiguredJob")
385+
.run((context) -> {
386+
assertThat(context).hasSingleBean(JobLauncher.class);
387+
assertThrows(NoSuchJobException.class,()->{
388+
context.getBean(JobLauncherApplicationRunner.class).run();
389+
});
390+
assertThat(context.getBean(JobRepository.class)
391+
.getLastJobExecution("discreteLocalJob", new JobParameters())).isNotNull();
392+
});
393+
}
394+
377395
@Configuration(proxyBeanMethods = false)
378396
protected static class BatchDataSourceConfiguration {
379397

0 commit comments

Comments
 (0)