From 857a5b03b76cb44e1873abc449a14fb8f5055ac5 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 30 Jan 2018 16:55:09 +0100 Subject: [PATCH] SchedulerFactoryBean accepts external Quartz SchedulerFactory instance Issue: SPR-16439 --- .../quartz/SchedulerFactoryBean.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) 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 e4923e042d0..3c2e6e43dd3 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -94,17 +94,13 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe public static final int DEFAULT_THREAD_COUNT = 10; - private static final ThreadLocal configTimeResourceLoaderHolder = - new ThreadLocal<>(); + private static final ThreadLocal configTimeResourceLoaderHolder = new ThreadLocal<>(); - private static final ThreadLocal configTimeTaskExecutorHolder = - new ThreadLocal<>(); + private static final ThreadLocal configTimeTaskExecutorHolder = new ThreadLocal<>(); - private static final ThreadLocal configTimeDataSourceHolder = - new ThreadLocal<>(); + private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal<>(); - private static final ThreadLocal configTimeNonTransactionalDataSourceHolder = - new ThreadLocal<>(); + private static final ThreadLocal configTimeNonTransactionalDataSourceHolder = new ThreadLocal<>(); /** @@ -166,6 +162,9 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe private Class schedulerFactoryClass = StdSchedulerFactory.class; + @Nullable + private SchedulerFactory schedulerFactory; + @Nullable private String schedulerName; @@ -185,7 +184,6 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe @Nullable private DataSource nonTransactionalDataSource; - @Nullable private Map schedulerContextMap; @@ -217,7 +215,7 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe /** - * Set the Quartz SchedulerFactory implementation to use. + * Set the Quartz {@link SchedulerFactory} implementation to use. *

Default is {@link StdSchedulerFactory}, reading in the standard * {@code quartz.properties} from {@code quartz.jar}. * To use custom Quartz properties, specify the "configLocation" @@ -230,6 +228,18 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe this.schedulerFactoryClass = schedulerFactoryClass; } + /** + * Set an external Quartz {@link SchedulerFactory} instance to use. + *

Default is an internal {@link StdSchedulerFactory} instance. + * If this method is being called, it overrides any class specified + * through {@link #setSchedulerFactoryClass}. + * @since 5.0.4 + * @see #setSchedulerFactoryClass + */ + public void setSchedulerFactory(SchedulerFactory schedulerFactory) { + this.schedulerFactory = schedulerFactory; + } + /** * Set the name of the Scheduler to create via the SchedulerFactory. *

If not specified, the bean name will be used as default scheduler name. @@ -317,7 +327,6 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe this.nonTransactionalDataSource = nonTransactionalDataSource; } - /** * Register objects in the Scheduler context via a given Map. * These objects will be available to any Job that runs in this Scheduler. @@ -470,8 +479,9 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe this.resourceLoader = this.applicationContext; } - // Create SchedulerFactory instance... - SchedulerFactory schedulerFactory = BeanUtils.instantiateClass(this.schedulerFactoryClass); + // Initialize the SchedulerFactory instance... + SchedulerFactory schedulerFactory = (this.schedulerFactory != null ? this.schedulerFactory : + BeanUtils.instantiateClass(this.schedulerFactoryClass)); initSchedulerFactory(schedulerFactory); if (this.resourceLoader != null) {