Prefer explicit "org.quartz.scheduler.instanceName" over bean name

Issue: SPR-16884
This commit is contained in:
Juergen Hoeller
2018-09-19 22:19:28 +02:00
parent a6f9c4c599
commit 50c9542796
3 changed files with 68 additions and 17 deletions

View File

@@ -192,9 +192,6 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
@Nullable
private Map<String, ?> 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.
* <p>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.
* <p>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);

View File

@@ -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());

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="scheduler1" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">quartz1</prop>
</props>
</property>
</bean>
<bean id="scheduler2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">quartz2</prop>
</props>
</property>
</bean>
</beans>