diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java index 09b9685512..658010ae7c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java @@ -90,7 +90,9 @@ public class QuartzAutoConfiguration { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setJobFactory(new AutowireCapableBeanJobFactory( this.applicationContext.getAutowireCapableBeanFactory())); - schedulerFactoryBean.setBeanName(this.properties.getSchedulerName()); + if (this.properties.getSchedulerName() != null) { + schedulerFactoryBean.setSchedulerName(this.properties.getSchedulerName()); + } schedulerFactoryBean.setAutoStartup(this.properties.isAutoStartup()); schedulerFactoryBean .setStartupDelay((int) this.properties.getStartupDelay().getSeconds()); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java index 1f4e4984a9..733b3ba2da 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java @@ -41,7 +41,7 @@ public class QuartzProperties { /** * Name of the scheduler. */ - private String schedulerName = "quartzScheduler"; + private String schedulerName; /** * Whether to automatically start the scheduler after initialization. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 85dc74583d..c370ba38c3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -504,6 +504,11 @@ "level" : "error" } }, + { + "name": "spring.quartz.scheduler-name", + "type": "java.lang.String", + "defaultValue": "quartzScheduler" + }, { "name": "spring.quartz.jdbc.initialize-schema", "defaultValue": "embedded" diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java index c1cd6f74e2..f7d8859e00 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java @@ -250,17 +250,14 @@ public class QuartzAutoConfigurationTests { @Test public void withCustomConfiguration() { - this.contextRunner.withPropertyValues( - "spring.quartz.scheduler-name=testScheduler", - "spring.quartz.auto-startup=false", "spring.quartz.startup-delay=1m", + this.contextRunner.withPropertyValues("spring.quartz.auto-startup=false", + "spring.quartz.startup-delay=1m", "spring.quartz.wait-for-jobs-to-complete-on-shutdown=true", "spring.quartz.overwrite-existing-jobs=true").run((context) -> { assertThat(context).hasSingleBean(SchedulerFactoryBean.class); SchedulerFactoryBean schedulerFactory = context .getBean(SchedulerFactoryBean.class); DirectFieldAccessor dfa = new DirectFieldAccessor(schedulerFactory); - assertThat(dfa.getPropertyValue("schedulerName")) - .isEqualTo("testScheduler"); assertThat(schedulerFactory.isAutoStartup()).isFalse(); assertThat(dfa.getPropertyValue("startupDelay")).isEqualTo(60); assertThat(dfa.getPropertyValue("waitForJobsToCompleteOnShutdown")) @@ -270,6 +267,45 @@ public class QuartzAutoConfigurationTests { }); } + @Test + public void schedulerNameWithDedicatedProperty() { + this.contextRunner + .withPropertyValues("spring.quartz.scheduler-name=testScheduler") + .run(assertSchedulerName("testScheduler")); + } + + @Test + public void schedulerNameWithQuartzProperty() { + this.contextRunner.withPropertyValues( + "spring.quartz.properties.org.quartz.scheduler.instanceName=testScheduler") + .run(assertSchedulerName("testScheduler")); + } + + @Test + public void schedulerNameWithDedicatedPropertyTakesPrecedence() { + this.contextRunner.withPropertyValues( + "spring.quartz.scheduler-name=specificTestScheduler", + "spring.quartz.properties.org.quartz.scheduler.instanceName=testScheduler") + .run(assertSchedulerName("specificTestScheduler")); + } + + @Test + public void schedulerNameUseBeanNameByDefault() { + this.contextRunner.withPropertyValues() + .run(assertSchedulerName("quartzScheduler")); + } + + private ContextConsumer assertSchedulerName( + String schedulerName) { + return (context) -> { + assertThat(context).hasSingleBean(SchedulerFactoryBean.class); + SchedulerFactoryBean schedulerFactory = context + .getBean(SchedulerFactoryBean.class); + DirectFieldAccessor dfa = new DirectFieldAccessor(schedulerFactory); + assertThat(dfa.getPropertyValue("schedulerName")).isEqualTo(schedulerName); + }; + } + @Import(ComponentThatUsesScheduler.class) @Configuration protected static class BaseQuartzConfiguration {