From 50c95427960c620d86068ac2deae8a6220eac636 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 19 Sep 2018 22:19:28 +0200 Subject: [PATCH] Prefer explicit "org.quartz.scheduler.instanceName" over bean name Issue: SPR-16884 --- .../quartz/SchedulerFactoryBean.java | 37 ++++++++++++++----- .../scheduling/quartz/QuartzSupportTests.java | 26 +++++++++---- ...multipleSchedulersWithQuartzProperties.xml | 22 +++++++++++ 3 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java index b43513de53..3124dc726a 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java @@ -192,9 +192,6 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe @Nullable private Map schedulerContextMap; - @Nullable - private ApplicationContext applicationContext; - @Nullable private String applicationContextSchedulerContextKey; @@ -213,6 +210,12 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe private boolean waitForJobsToCompleteOnShutdown = false; + @Nullable + private String beanName; + + @Nullable + private ApplicationContext applicationContext; + @Nullable private Scheduler scheduler; @@ -252,9 +255,13 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe } /** - * Set the name of the Scheduler to create via the SchedulerFactory. - *

If not specified, the bean name will be used as default scheduler name. + * Set the name of the Scheduler to create via the SchedulerFactory, as an + * alternative to the {@code org.quartz.scheduler.instanceName} property. + *

If not specified, the name will be taken from Quartz properties + * ({@code org.quartz.scheduler.instanceName}), or from the declared + * {@code SchedulerFactoryBean} bean name as a fallback. * @see #setBeanName + * @see StdSchedulerFactory#PROP_SCHED_INSTANCE_NAME * @see org.quartz.SchedulerFactory#getScheduler() * @see org.quartz.SchedulerFactory#getScheduler(String) */ @@ -467,9 +474,7 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe @Override public void setBeanName(String name) { - if (this.schedulerName == null) { - this.schedulerName = name; - } + this.beanName = name; } @Override @@ -564,10 +569,22 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps); if (this.dataSource != null) { - mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName()); + mergedProps.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName()); } + + // Determine scheduler name across local settings and Quartz properties... if (this.schedulerName != null) { - mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName); + mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName); + } + else { + String nameProp = mergedProps.getProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME); + if (nameProp != null) { + this.schedulerName = nameProp; + } + else if (this.beanName != null) { + mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.beanName); + this.schedulerName = this.beanName; + } } schedulerFactory.initialize(mergedProps); diff --git a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java index 29f278ec8c..68a619c0d9 100644 --- a/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java +++ b/spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -292,10 +292,7 @@ public class QuartzSupportTests { bean.destroy(); } - /** - * Tests the creation of multiple schedulers (SPR-772) - */ - @Test + @Test // SPR-772 public void multipleSchedulers() throws Exception { ClassPathXmlApplicationContext ctx = context("multipleSchedulers.xml"); try { @@ -310,6 +307,21 @@ public class QuartzSupportTests { } } + @Test // SPR-16884 + public void multipleSchedulersWithQuartzProperties() throws Exception { + ClassPathXmlApplicationContext ctx = context("multipleSchedulersWithQuartzProperties.xml"); + try { + Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1"); + Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2"); + assertNotSame(scheduler1, scheduler2); + assertEquals("quartz1", scheduler1.getSchedulerName()); + assertEquals("quartz2", scheduler2.getSchedulerName()); + } + finally { + ctx.close(); + } + } + @Test public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception { Assume.group(TestGroup.PERFORMANCE); @@ -363,8 +375,8 @@ public class QuartzSupportTests { @SuppressWarnings("resource") public void schedulerAutoStartupFalse() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); - BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition( - SchedulerFactoryBean.class).addPropertyValue("autoStartup", false).getBeanDefinition(); + BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(SchedulerFactoryBean.class) + .addPropertyValue("autoStartup", false).getBeanDefinition(); context.registerBeanDefinition("scheduler", beanDefinition); Scheduler bean = context.getBean("scheduler", Scheduler.class); assertFalse(bean.isStarted()); diff --git a/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml new file mode 100644 index 0000000000..d1b83b17ed --- /dev/null +++ b/spring-context-support/src/test/resources/org/springframework/scheduling/quartz/multipleSchedulersWithQuartzProperties.xml @@ -0,0 +1,22 @@ + + + + + + + + + quartz1 + + + + + + + + quartz2 + + + + +