FactoryBean that creates and configures a Quartz org.quartz.Scheduler ,
manages its lifecycle as part of the Spring application context, and exposes the
Scheduler as bean reference for dependency injection.
Allows registration of JobDetails, Calendars and Triggers, automatically starting the scheduler on initialization and shutting it down on destruction. In scenarios that just require static registration of jobs at startup, there is no need to access the Scheduler instance itself in application code.
For dynamic registration of jobs at runtime, use a bean reference to
this SchedulerFactoryBean to get direct access to the Quartz Scheduler
(org.quartz.Scheduler). This allows you to create new jobs
and triggers, and also to control and monitor the entire Scheduler.
Note that Quartz instantiates a new Job for each execution, in contrast to Timer which uses a TimerTask instance that is shared between repeated executions. Just JobDetail descriptors are shared.
When using persistent jobs, it is strongly recommended to perform all
operations on the Scheduler within Spring-managed (or plain JTA) transactions.
Else, database locking will not properly work and might even break.
(See setDataSource javadoc for details.)
The preferred way to achieve transactional execution is to demarcate declarative transactions at the business facade level, which will automatically apply to Scheduler operations performed within those scopes. Alternatively, you may add transactional advice for the Scheduler itself.
Compatible with Quartz 2.1.4 and higher, as of Spring 4.1.
setDataSource, org.quartz.Scheduler , org.quartz.SchedulerFactory , org.quartz.impl.StdSchedulerFactory , org.springframework.transaction.interceptor.TransactionProxyFactoryBean
| Modifier and Type | Field and Description |
|---|---|
| private ApplicationContext | |
| private String | |
| private boolean | |
| private String | |
| private Resource | |
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private static final ThreadLocal | |
| private DataSource | |
| public static final int | DEFAULT_THREAD_COUNT
The default thread count. |
| private boolean | |
| private org. | |
| private boolean | |
| private DataSource | |
| private int | |
| public static final String | PROP_THREAD_COUNT
The thread count property. |
| private Properties | |
| private org. | |
| private Map | |
| private org. | |
| private Class | |
| private String | |
| private int | |
| private Executor | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. |
| protected org. | Returns: the Scheduler instancethe factory to create the Scheduler with schedulerFactory,the name of the scheduler to create schedulerName)Create the Scheduler instance for the given factory and scheduler name. |
| public void | destroy()
Implements org. Shut down the Quartz scheduler on bean factory shutdown, stopping all scheduled jobs. |
| public static DataSource | getConfigTimeDataSource()
Return the |
| public static DataSource | getConfigTimeNonTransactionalDataSource()
Return the non-transactional |
| public static ResourceLoader | getConfigTimeResourceLoader()
Return the |
| public static Executor | getConfigTimeTaskExecutor()
Return the |
| public org. | getObject()
Implements org. Return an instance (possibly shared or independent) of the object managed by this factory. |
| public Class | getObjectType()
Implements org. Return the type of object that this FactoryBean creates,
or |
| public int | getPhase()
Overrides default org. Return the phase in which this scheduler will be started and stopped. |
| public org. | getScheduler()
Implements abstract org. Template method that determines the Scheduler to operate on. |
| private void | initSchedulerFactory(org.
the SchedulerFactory to initialize schedulerFactoryInitialize the given SchedulerFactory, applying locally defined Quartz properties to it. |
| public boolean | isAutoStartup()
Overrides default org. Return whether this scheduler is configured for auto-startup. |
| public boolean | isRunning()
Implements org. Check whether this component is currently running. |
| public boolean | isSingleton()
Overrides default org. Is the object managed by this factory a singleton? |
| private void | populateSchedulerContext(org.
Expose the specified context attributes and/or the current ApplicationContext in the Quartz SchedulerContext. |
| private org. | |
| private org. | Returns: the initialized SchedulerFactoryCreate a SchedulerFactory if necessary and apply locally defined Quartz properties to it. |
| public void | setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object applicationContext)Implements org. Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object. |
| public void | setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey)
Set the key of an |
| public void | setAutoStartup(boolean autoStartup)
Set whether to automatically start the scheduler after initialization. |
| public void | setBeanName(String
the name of the bean in the factory.
Note that this name is the actual bean name used in the factory, which may
differ from the originally specified name: in particular for inner bean
names, the actual bean name might have been made unique through appending
"#..." suffixes. Use the name)BeanFactoryUtils#originalBeanName(String)
method to extract the original bean name (without suffix), if desired.Implements org. Set the name of the bean in the bean factory that created this bean. |
| public void | setConfigLocation(Resource configLocation)
Set the location of the Quartz properties config file, for example as classpath resource "classpath:quartz.properties". |
| public void | |
| public void | setExposeSchedulerInRepository(boolean exposeSchedulerInRepository)
Set whether to expose the Spring-managed |
| public void | setJobFactory(org.
Set the Quartz |
| public void | setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
Set the |
| public void | |
| public void | setQuartzProperties(Properties quartzProperties)
Set Quartz properties, like "org.quartz.threadPool.class". |
| public void | setSchedulerContextAsMap(Map<String, ?>
a Map with String keys and any objects as
values (for example Spring-managed beans) schedulerContextAsMap)Register objects in the Scheduler context via a given Map. |
| public void | setSchedulerFactory(org.
Set an external Quartz |
| public void | setSchedulerFactoryClass(Class<? extends org.
Set the Quartz |
| public void | setSchedulerName(String schedulerName)
Set the name of the Scheduler to create via the SchedulerFactory, as an
alternative to the |
| public void | setStartupDelay(int startupDelay)
Set the number of seconds to wait after initialization before starting the scheduler asynchronously. |
| public void | |
| public void | setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)
Set whether to wait for running jobs to complete on shutdown. |
| public void | |
| protected void | startScheduler(final org.
the Scheduler to start scheduler,the number of seconds to wait before starting
the Scheduler asynchronously startupDelay)Start the Quartz Scheduler, respecting the "startupDelay" setting. |
| public void | stop()
Implements org. Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. |
| applicationContext | back to summary |
|---|---|
| private ApplicationContext applicationContext
| |
| applicationContextSchedulerContextKey | back to summary |
|---|---|
| private String applicationContextSchedulerContextKey
| |
| autoStartup | back to summary |
|---|---|
| private boolean autoStartup | |
| beanName | back to summary |
|---|---|
| private String beanName
| |
| configLocation | back to summary |
|---|---|
| private Resource configLocation
| |
| configTimeDataSourceHolder | back to summary |
|---|---|
| private static final ThreadLocal<DataSource> configTimeDataSourceHolder | |
| configTimeNonTransactionalDataSourceHolder | back to summary |
|---|---|
| private static final ThreadLocal<DataSource> configTimeNonTransactionalDataSourceHolder | |
| configTimeResourceLoaderHolder | back to summary |
|---|---|
| private static final ThreadLocal<ResourceLoader> configTimeResourceLoaderHolder | |
| configTimeTaskExecutorHolder | back to summary |
|---|---|
| private static final ThreadLocal<Executor> configTimeTaskExecutorHolder | |
| dataSource | back to summary |
|---|---|
| private DataSource dataSource
| |
| DEFAULT_THREAD_COUNT | back to summary |
|---|---|
| public static final int DEFAULT_THREAD_COUNT The default thread count. | |
| exposeSchedulerInRepository | back to summary |
|---|---|
| private boolean exposeSchedulerInRepository | |
| jobFactory | back to summary |
|---|---|
| private org. | |
| jobFactorySet | back to summary |
|---|---|
| private boolean jobFactorySet | |
| nonTransactionalDataSource | back to summary |
|---|---|
| private DataSource nonTransactionalDataSource
| |
| phase | back to summary |
|---|---|
| private int phase | |
| PROP_THREAD_COUNT | back to summary |
|---|---|
| public static final String PROP_THREAD_COUNT The thread count property. | |
| quartzProperties | back to summary |
|---|---|
| private Properties quartzProperties
| |
| scheduler | back to summary |
|---|---|
| private org. | |
| schedulerContextMap | back to summary |
|---|---|
| private Map<String, ?> schedulerContextMap
| |
| schedulerFactory | back to summary |
|---|---|
| private org. | |
| schedulerFactoryClass | back to summary |
|---|---|
| private Class<? extends org. | |
| schedulerName | back to summary |
|---|---|
| private String schedulerName
| |
| startupDelay | back to summary |
|---|---|
| private int startupDelay | |
| taskExecutor | back to summary |
|---|---|
| private Executor taskExecutor
| |
| waitForJobsToCompleteOnShutdown | back to summary |
|---|---|
| private boolean waitForJobsToCompleteOnShutdown | |
| SchedulerFactoryBean | back to summary |
|---|---|
| public SchedulerFactoryBean() | |
| afterPropertiesSet | back to summary |
|---|---|
| public void afterPropertiesSet() throws Exception Implements org. Doc from org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. | |
| createScheduler | back to summary |
|---|---|
| protected org. Create the Scheduler instance for the given factory and scheduler name.
Called by The default implementation invokes SchedulerFactory's
| |
| destroy | back to summary |
|---|---|
| public void destroy() throws org.quartz.SchedulerException Implements org. Shut down the Quartz scheduler on bean factory shutdown, stopping all scheduled jobs.
| |
| getConfigTimeDataSource | back to summary |
|---|---|
| public static DataSource getConfigTimeDataSource() Return the This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
| |
| getConfigTimeNonTransactionalDataSource | back to summary |
|---|---|
| public static DataSource getConfigTimeNonTransactionalDataSource() Return the non-transactional This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
| |
| getConfigTimeResourceLoader | back to summary |
|---|---|
| public static ResourceLoader getConfigTimeResourceLoader() Return the This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
| |
| getConfigTimeTaskExecutor | back to summary |
|---|---|
| public static Executor getConfigTimeTaskExecutor() Return the This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
| |
| getObject | back to summary |
|---|---|
| public org. Implements org. Doc from org. Return an instance (possibly shared or independent) of the object managed by this factory. As with a If this FactoryBean is not fully initialized yet at the time of
the call (for example because it is involved in a circular reference),
throw a corresponding As of Spring 2.0, FactoryBeans are allowed to return | |
| getObjectType | back to summary |
|---|---|
| public Class Implements org. Doc from org. Return the type of object that this FactoryBean creates,
or This allows one to check for specific types of beans without instantiating objects, for example on autowiring. In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too. This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available. Note Autowiring will simply ignore FactoryBeans that return
| |
| getPhase | back to summary |
|---|---|
| public int getPhase() Overrides default org. Return the phase in which this scheduler will be started and stopped.
| |
| getScheduler | back to summary |
|---|---|
| public org. Implements abstract org. Doc from org. Template method that determines the Scheduler to operate on. To be implemented by subclasses.
| |
| initSchedulerFactory | back to summary |
|---|---|
| private void initSchedulerFactory(org. Initialize the given SchedulerFactory, applying locally defined Quartz properties to it.
| |
| isAutoStartup | back to summary |
|---|---|
| public boolean isAutoStartup() Overrides default org. Return whether this scheduler is configured for auto-startup. If "true", the scheduler will start after the context is refreshed and after the start delay, if any.
| |
| isRunning | back to summary |
|---|---|
| public boolean isRunning() throws SchedulingException Implements org. Doc from org. Check whether this component is currently running. In the case of a container, this will return
| |
| isSingleton | back to summary |
|---|---|
| public boolean isSingleton() Overrides default org. Doc from org. Is the object managed by this factory a singleton? That is,
will Note If a FactoryBean indicates to hold a singleton object,
the object returned from The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there. Note This method returning The default implementation returns
| |
| populateSchedulerContext | back to summary |
|---|---|
| private void populateSchedulerContext(org. Expose the specified context attributes and/or the current ApplicationContext in the Quartz SchedulerContext. | |
| prepareScheduler | back to summary |
|---|---|
| private org. | |
| prepareSchedulerFactory | back to summary |
|---|---|
| private org. Create a SchedulerFactory if necessary and apply locally defined Quartz properties to it.
| |
| setApplicationContext | back to summary |
|---|---|
| public void setApplicationContext(ApplicationContext applicationContext) Implements org. Doc from org. Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object. Invoked after population of normal bean properties but before an init callback such
as
| |
| setApplicationContextSchedulerContextKey | back to summary |
|---|---|
| public void setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey) Set the key of an Note When using persistent Jobs whose JobDetail will be kept in the database, do not put an ApplicationContext reference into the JobDataMap but rather into the SchedulerContext. In case of a QuartzJobBean, the reference will be applied to the Job instance as bean property. An "applicationContext" attribute will correspond to a "setApplicationContext" method in that scenario. Note that BeanFactory callback interfaces like ApplicationContextAware are not automatically applied to Quartz Job instances, because Quartz itself is responsible for the lifecycle of its Jobs. | |
| setAutoStartup | back to summary |
|---|---|
| public void setAutoStartup(boolean autoStartup) Set whether to automatically start the scheduler after initialization. Default is "true"; set this to "false" to allow for manual startup. | |
| setBeanName | back to summary |
|---|---|
| public void setBeanName(String name) Implements org. Doc from org. Set the name of the bean in the bean factory that created this bean. Invoked after population of normal bean properties but before an
init callback such as
| |
| setConfigLocation | back to summary |
|---|---|
| public void setConfigLocation(Resource configLocation) Set the location of the Quartz properties config file, for example as classpath resource "classpath:quartz.properties". Note Can be omitted when all necessary properties are specified locally via this bean, or when relying on Quartz' default configuration.
| |
| setDataSource | back to summary |
|---|---|
| public void setDataSource(DataSource dataSource) Set the default Note If this is set, the Quartz settings should not define
a job store "dataSource" to avoid meaningless double configuration.
Also, do not define a "org.quartz.jobStore.class" property at all.
(You may explicitly define Spring's A Spring-specific subclass of Quartz' JobStoreCMT will be used. It is therefore strongly recommended to perform all operations on the Scheduler within Spring-managed (or plain JTA) transactions. Else, database locking will not properly work and might even break (e.g. if trying to obtain a lock on Oracle without a transaction). Supports both transactional and non-transactional DataSource access. With a non-XA DataSource and local Spring transactions, a single DataSource argument is sufficient. In case of an XA DataSource and global JTA transactions, SchedulerFactoryBean's "nonTransactionalDataSource" property should be set, passing in a non-XA DataSource that will not participate in global transactions.
| |
| setExposeSchedulerInRepository | back to summary |
|---|---|
| public void setExposeSchedulerInRepository(boolean exposeSchedulerInRepository) Set whether to expose the Spring-managed Switch this flag to "true" in order to expose the Scheduler globally. This is not recommended unless you have an existing Spring application that relies on this behavior. Note that such global exposure was the accidental default in earlier Spring versions; this has been fixed as of Spring 2.5.6. | |
| setJobFactory | back to summary |
|---|---|
| public void setJobFactory(org. Set the Quartz Default is Spring's Specify an instance of Spring's
| |
| setNonTransactionalDataSource | back to summary |
|---|---|
| public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource) Set the This is only necessary if the default DataSource is an XA DataSource that will always participate in transactions: A non-XA version of that DataSource should be specified as "nonTransactionalDataSource" in such a scenario. This is not relevant with a local DataSource instance and Spring transactions. Specifying a single default DataSource as "dataSource" is sufficient there.
| |
| setPhase | back to summary |
|---|---|
| public void setPhase(int phase) Specify the phase in which this scheduler should be started and stopped.
The startup order proceeds from lowest to highest, and the shutdown order
is the reverse of that. By default this value is
| |
| setQuartzProperties | back to summary |
|---|---|
| public void setQuartzProperties(Properties quartzProperties) Set Quartz properties, like "org.quartz.threadPool.class". Can be used to override values in a Quartz properties config file, or to specify all necessary properties locally.
| |
| setSchedulerContextAsMap | back to summary |
|---|---|
| public void setSchedulerContextAsMap(Map<String, ?> schedulerContextAsMap) Register objects in the Scheduler context via a given Map. These objects will be available to any Job that runs in this Scheduler. Note When using persistent Jobs whose JobDetail will be kept in the database, do not put Spring-managed beans or an ApplicationContext reference into the JobDataMap but rather into the SchedulerContext.
| |
| setSchedulerFactory | back to summary |
|---|---|
| public void setSchedulerFactory(org. Set an external Quartz Default is an internal Note With an externally provided
| |
| setSchedulerFactoryClass | back to summary |
|---|---|
| public void setSchedulerFactoryClass(Class<? extends org. Set the Quartz Default is the | |
| setSchedulerName | back to summary |
|---|---|
| public void setSchedulerName(String schedulerName) Set the name of the Scheduler to create via the SchedulerFactory, as an
alternative to the If not specified, the name will be taken from Quartz properties
( | |
| setStartupDelay | back to summary |
|---|---|
| public void setStartupDelay(int startupDelay) Set the number of seconds to wait after initialization before starting the scheduler asynchronously. Default is 0, meaning immediate synchronous startup on initialization of this bean. Setting this to 10 or 20 seconds makes sense if no jobs should be run before the entire application has started up. | |
| setTaskExecutor | back to summary |
|---|---|
| public void setTaskExecutor(Executor taskExecutor) Set a Spring-managed Can be used to assign a local JDK ThreadPoolExecutor or a CommonJ WorkManager as Quartz backend, to avoid Quartz's manual thread creation. By default, a Quartz SimpleThreadPool will be used, configured through the corresponding Quartz properties. | |
| setWaitForJobsToCompleteOnShutdown | back to summary |
|---|---|
| public void setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown) Set whether to wait for running jobs to complete on shutdown. Default is "false". Switch this to "true" if you prefer fully completed jobs at the expense of a longer shutdown phase. | |
| start | back to summary |
|---|---|
| public void start() throws SchedulingException Implements org. Doc from org. Start this component. Should not throw an exception if the component is already running. In the case of a container, this will propagate the start signal to all components that apply.
| |
| startScheduler | back to summary |
|---|---|
| protected void startScheduler(final org. Start the Quartz Scheduler, respecting the "startupDelay" setting.
| |
| stop | back to summary |
|---|---|
| public void stop() throws SchedulingException Implements org. Doc from org. Stop this component, typically in a synchronous fashion, such that the component is
fully stopped upon return of this method. Consider implementing Note that this stop notification is not guaranteed to come before destruction:
On regular shutdown, Should not throw an exception if the component is not running (not started yet). In the case of a container, this will propagate the stop signal to all components that apply.
| |