Skip to content

Commit cd57335

Browse files
committed
SchedulerFactoryBean ignores local factory settings in case of external SchedulerFactory instance (unless it extends from StdSchedulerFactory)
Issue: SPR-16439
1 parent d02e4fb commit cd57335

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

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

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
216216

217217
/**
218218
* Set the Quartz {@link SchedulerFactory} implementation to use.
219-
* <p>Default is {@link StdSchedulerFactory}, reading in the standard
220-
* {@code quartz.properties} from {@code quartz.jar}.
221-
* To use custom Quartz properties, specify the "configLocation"
222-
* or "quartzProperties" bean property on this FactoryBean.
219+
* <p>Default is the {@link StdSchedulerFactory} class, reading in the standard
220+
* {@code quartz.properties} from {@code quartz.jar}. For applying custom Quartz
221+
* properties, specify {@link #setConfigLocation "configLocation"} or
222+
* {@link #setQuartzProperties "quartzProperties"} on {@code SchedulerFactoryBean}.
223223
* @see org.quartz.impl.StdSchedulerFactory
224224
* @see #setConfigLocation
225225
* @see #setQuartzProperties
@@ -230,10 +230,15 @@ public void setSchedulerFactoryClass(Class<? extends SchedulerFactory> scheduler
230230

231231
/**
232232
* Set an external Quartz {@link SchedulerFactory} instance to use.
233-
* <p>Default is an internal {@link StdSchedulerFactory} instance.
234-
* If this method is being called, it overrides any class specified
235-
* through {@link #setSchedulerFactoryClass}.
236-
* @since 5.0.4
233+
* <p>Default is an internal {@link StdSchedulerFactory} instance. If this method is
234+
* called, it overrides any class specified through {@link #setSchedulerFactoryClass}.
235+
* <p>An externally provided {@code SchedulerFactory} instance may get initialized
236+
* from local {@code SchedulerFactoryBean} settings (such as {@link #setConfigLocation}
237+
* or {@link #setQuartzProperties}) but only if it extends from {@link StdSchedulerFactory},
238+
* inheriting the common {@link StdSchedulerFactory#initialize(Properties)} method.
239+
* Otherwise, all such local settings will be ignored here in {@code SchedulerFactoryBean},
240+
* expecting the external {@code SchedulerFactory} instance to get initialized on its own.
241+
* @since 4.3.15
237242
* @see #setSchedulerFactoryClass
238243
*/
239244
public void setSchedulerFactory(SchedulerFactory schedulerFactory) {
@@ -480,9 +485,7 @@ public void afterPropertiesSet() throws Exception {
480485
}
481486

482487
// Initialize the SchedulerFactory instance...
483-
SchedulerFactory schedulerFactory = (this.schedulerFactory != null ? this.schedulerFactory :
484-
BeanUtils.instantiateClass(this.schedulerFactoryClass));
485-
initSchedulerFactory(schedulerFactory);
488+
SchedulerFactory schedulerFactory = prepareSchedulerFactory();
486489

487490
if (this.resourceLoader != null) {
488491
// Make given ResourceLoader available for SchedulerFactory configuration.
@@ -540,22 +543,39 @@ public void afterPropertiesSet() throws Exception {
540543

541544

542545
/**
543-
* Load and/or apply Quartz properties to the given SchedulerFactory.
544-
* @param schedulerFactory the SchedulerFactory to initialize
546+
* Create a SchedulerFactory if necessary and apply locally defined Quartz properties to it.
547+
* @return the initialized SchedulerFactory
545548
*/
546-
private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws SchedulerException, IOException {
547-
if (!(schedulerFactory instanceof StdSchedulerFactory)) {
548-
if (this.configLocation != null || this.quartzProperties != null ||
549+
private SchedulerFactory prepareSchedulerFactory() throws SchedulerException, IOException {
550+
SchedulerFactory schedulerFactory = this.schedulerFactory;
551+
if (schedulerFactory != null) {
552+
if (schedulerFactory instanceof StdSchedulerFactory) {
553+
initSchedulerFactory((StdSchedulerFactory) schedulerFactory);
554+
}
555+
// Otherwise, assume that externally provided factory has been initialized with appropriate settings...
556+
}
557+
else {
558+
// Create local SchedulerFactory instance (typically a StdSchedulerFactory)
559+
schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);
560+
if (schedulerFactory instanceof StdSchedulerFactory) {
561+
initSchedulerFactory((StdSchedulerFactory) schedulerFactory);
562+
}
563+
else if (this.configLocation != null || this.quartzProperties != null ||
549564
this.taskExecutor != null || this.dataSource != null) {
550565
throw new IllegalArgumentException(
551566
"StdSchedulerFactory required for applying Quartz properties: " + schedulerFactory);
552567
}
553-
// Otherwise assume that no initialization is necessary...
554-
return;
568+
// Otherwise, no local settings to be applied via StdSchedulerFactory.initialize(Properties)
555569
}
570+
return schedulerFactory;
571+
}
556572

573+
/**
574+
* Initialize the given SchedulerFactory, applying locally defined Quartz properties to it.
575+
* @param schedulerFactory the SchedulerFactory to initialize
576+
*/
577+
private void initSchedulerFactory(StdSchedulerFactory schedulerFactory) throws SchedulerException, IOException {
557578
Properties mergedProps = new Properties();
558-
559579
if (this.resourceLoader != null) {
560580
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,
561581
ResourceLoaderClassLoadHelper.class.getName());
@@ -580,17 +600,14 @@ private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws Sche
580600
}
581601

582602
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
583-
584603
if (this.dataSource != null) {
585604
mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
586605
}
587-
588-
// Make sure to set the scheduler name as configured in the Spring configuration.
589606
if (this.schedulerName != null) {
590607
mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
591608
}
592609

593-
((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);
610+
schedulerFactory.initialize(mergedProps);
594611
}
595612

596613
/**

0 commit comments

Comments
 (0)