Skip to content

Commit 50c9542

Browse files
committed
Prefer explicit "org.quartz.scheduler.instanceName" over bean name
Issue: SPR-16884
1 parent a6f9c4c commit 50c9542

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
192192
@Nullable
193193
private Map<String, ?> schedulerContextMap;
194194

195-
@Nullable
196-
private ApplicationContext applicationContext;
197-
198195
@Nullable
199196
private String applicationContextSchedulerContextKey;
200197

@@ -213,6 +210,12 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
213210

214211
private boolean waitForJobsToCompleteOnShutdown = false;
215212

213+
@Nullable
214+
private String beanName;
215+
216+
@Nullable
217+
private ApplicationContext applicationContext;
218+
216219
@Nullable
217220
private Scheduler scheduler;
218221

@@ -252,9 +255,13 @@ public void setSchedulerFactoryClass(Class<? extends SchedulerFactory> scheduler
252255
}
253256

254257
/**
255-
* Set the name of the Scheduler to create via the SchedulerFactory.
256-
* <p>If not specified, the bean name will be used as default scheduler name.
258+
* Set the name of the Scheduler to create via the SchedulerFactory, as an
259+
* alternative to the {@code org.quartz.scheduler.instanceName} property.
260+
* <p>If not specified, the name will be taken from Quartz properties
261+
* ({@code org.quartz.scheduler.instanceName}), or from the declared
262+
* {@code SchedulerFactoryBean} bean name as a fallback.
257263
* @see #setBeanName
264+
* @see StdSchedulerFactory#PROP_SCHED_INSTANCE_NAME
258265
* @see org.quartz.SchedulerFactory#getScheduler()
259266
* @see org.quartz.SchedulerFactory#getScheduler(String)
260267
*/
@@ -467,9 +474,7 @@ public void setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnSh
467474

468475
@Override
469476
public void setBeanName(String name) {
470-
if (this.schedulerName == null) {
471-
this.schedulerName = name;
472-
}
477+
this.beanName = name;
473478
}
474479

475480
@Override
@@ -564,10 +569,22 @@ private void initSchedulerFactory(StdSchedulerFactory schedulerFactory) throws S
564569

565570
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
566571
if (this.dataSource != null) {
567-
mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
572+
mergedProps.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
568573
}
574+
575+
// Determine scheduler name across local settings and Quartz properties...
569576
if (this.schedulerName != null) {
570-
mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
577+
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
578+
}
579+
else {
580+
String nameProp = mergedProps.getProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME);
581+
if (nameProp != null) {
582+
this.schedulerName = nameProp;
583+
}
584+
else if (this.beanName != null) {
585+
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.beanName);
586+
this.schedulerName = this.beanName;
587+
}
571588
}
572589

573590
schedulerFactory.initialize(mergedProps);

spring-context-support/src/test/java/org/springframework/scheduling/quartz/QuartzSupportTests.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -292,10 +292,7 @@ public void schedulerWithSpringBeanJobFactoryAndJobSchedulingData() throws Excep
292292
bean.destroy();
293293
}
294294

295-
/**
296-
* Tests the creation of multiple schedulers (SPR-772)
297-
*/
298-
@Test
295+
@Test // SPR-772
299296
public void multipleSchedulers() throws Exception {
300297
ClassPathXmlApplicationContext ctx = context("multipleSchedulers.xml");
301298
try {
@@ -310,6 +307,21 @@ public void multipleSchedulers() throws Exception {
310307
}
311308
}
312309

310+
@Test // SPR-16884
311+
public void multipleSchedulersWithQuartzProperties() throws Exception {
312+
ClassPathXmlApplicationContext ctx = context("multipleSchedulersWithQuartzProperties.xml");
313+
try {
314+
Scheduler scheduler1 = (Scheduler) ctx.getBean("scheduler1");
315+
Scheduler scheduler2 = (Scheduler) ctx.getBean("scheduler2");
316+
assertNotSame(scheduler1, scheduler2);
317+
assertEquals("quartz1", scheduler1.getSchedulerName());
318+
assertEquals("quartz2", scheduler2.getSchedulerName());
319+
}
320+
finally {
321+
ctx.close();
322+
}
323+
}
324+
313325
@Test
314326
public void twoAnonymousMethodInvokingJobDetailFactoryBeans() throws Exception {
315327
Assume.group(TestGroup.PERFORMANCE);
@@ -363,8 +375,8 @@ public void schedulerAutoStartsOnContextRefreshedEventByDefault() throws Excepti
363375
@SuppressWarnings("resource")
364376
public void schedulerAutoStartupFalse() throws Exception {
365377
StaticApplicationContext context = new StaticApplicationContext();
366-
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(
367-
SchedulerFactoryBean.class).addPropertyValue("autoStartup", false).getBeanDefinition();
378+
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(SchedulerFactoryBean.class)
379+
.addPropertyValue("autoStartup", false).getBeanDefinition();
368380
context.registerBeanDefinition("scheduler", beanDefinition);
369381
Scheduler bean = context.getBean("scheduler", Scheduler.class);
370382
assertFalse(bean.isStarted());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
3+
4+
<beans>
5+
6+
<bean id="scheduler1" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
7+
<property name="quartzProperties">
8+
<props>
9+
<prop key="org.quartz.scheduler.instanceName">quartz1</prop>
10+
</props>
11+
</property>
12+
</bean>
13+
14+
<bean id="scheduler2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
15+
<property name="quartzProperties">
16+
<props>
17+
<prop key="org.quartz.scheduler.instanceName">quartz2</prop>
18+
</props>
19+
</property>
20+
</bean>
21+
22+
</beans>

0 commit comments

Comments
 (0)