Skip to content

Commit 857a5b0

Browse files
committed
SchedulerFactoryBean accepts external Quartz SchedulerFactory instance
Issue: SPR-16439
1 parent f4de861 commit 857a5b0

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

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

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 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.
@@ -94,17 +94,13 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe
9494
public static final int DEFAULT_THREAD_COUNT = 10;
9595

9696

97-
private static final ThreadLocal<ResourceLoader> configTimeResourceLoaderHolder =
98-
new ThreadLocal<>();
97+
private static final ThreadLocal<ResourceLoader> configTimeResourceLoaderHolder = new ThreadLocal<>();
9998

100-
private static final ThreadLocal<Executor> configTimeTaskExecutorHolder =
101-
new ThreadLocal<>();
99+
private static final ThreadLocal<Executor> configTimeTaskExecutorHolder = new ThreadLocal<>();
102100

103-
private static final ThreadLocal<DataSource> configTimeDataSourceHolder =
104-
new ThreadLocal<>();
101+
private static final ThreadLocal<DataSource> configTimeDataSourceHolder = new ThreadLocal<>();
105102

106-
private static final ThreadLocal<DataSource> configTimeNonTransactionalDataSourceHolder =
107-
new ThreadLocal<>();
103+
private static final ThreadLocal<DataSource> configTimeNonTransactionalDataSourceHolder = new ThreadLocal<>();
108104

109105

110106
/**
@@ -166,6 +162,9 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
166162

167163
private Class<? extends SchedulerFactory> schedulerFactoryClass = StdSchedulerFactory.class;
168164

165+
@Nullable
166+
private SchedulerFactory schedulerFactory;
167+
169168
@Nullable
170169
private String schedulerName;
171170

@@ -185,7 +184,6 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
185184
@Nullable
186185
private DataSource nonTransactionalDataSource;
187186

188-
189187
@Nullable
190188
private Map<String, ?> schedulerContextMap;
191189

@@ -217,7 +215,7 @@ public static DataSource getConfigTimeNonTransactionalDataSource() {
217215

218216

219217
/**
220-
* Set the Quartz SchedulerFactory implementation to use.
218+
* Set the Quartz {@link SchedulerFactory} implementation to use.
221219
* <p>Default is {@link StdSchedulerFactory}, reading in the standard
222220
* {@code quartz.properties} from {@code quartz.jar}.
223221
* To use custom Quartz properties, specify the "configLocation"
@@ -230,6 +228,18 @@ public void setSchedulerFactoryClass(Class<? extends SchedulerFactory> scheduler
230228
this.schedulerFactoryClass = schedulerFactoryClass;
231229
}
232230

231+
/**
232+
* 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
237+
* @see #setSchedulerFactoryClass
238+
*/
239+
public void setSchedulerFactory(SchedulerFactory schedulerFactory) {
240+
this.schedulerFactory = schedulerFactory;
241+
}
242+
233243
/**
234244
* Set the name of the Scheduler to create via the SchedulerFactory.
235245
* <p>If not specified, the bean name will be used as default scheduler name.
@@ -317,7 +327,6 @@ public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
317327
this.nonTransactionalDataSource = nonTransactionalDataSource;
318328
}
319329

320-
321330
/**
322331
* Register objects in the Scheduler context via a given Map.
323332
* These objects will be available to any Job that runs in this Scheduler.
@@ -470,8 +479,9 @@ public void afterPropertiesSet() throws Exception {
470479
this.resourceLoader = this.applicationContext;
471480
}
472481

473-
// Create SchedulerFactory instance...
474-
SchedulerFactory schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass);
482+
// Initialize the SchedulerFactory instance...
483+
SchedulerFactory schedulerFactory = (this.schedulerFactory != null ? this.schedulerFactory :
484+
BeanUtils.instantiateClass(this.schedulerFactoryClass));
475485
initSchedulerFactory(schedulerFactory);
476486

477487
if (this.resourceLoader != null) {

0 commit comments

Comments
 (0)