FactoryBean that creates and configures a 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.
public void setDataSource(javax.sql.DataSource dataSource)
Set the default DataSource to be used by the Scheduler.
If set, this will override corresponding settings in Quartz properties.
Note: If this is set, the Quartz settings should not define
a job store "dataSource" to avoid meaningless double configuration.
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.
public void setNonTransactionalDataSource(javax.sql.DataSource nonTransactionalDataSource)
Set the DataSource to be used for non-transactional access.
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.
public void setSchedulerContextAsMap(java.util.Map<java.lang.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.
schedulerContextAsMap - a Map with String keys and any objects as
values (for example Spring-managed beans)
Set the Quartz JobFactory to use for this Scheduler.
Default is Spring's AdaptableJobFactory, which supports
Runnable objects as well as standard Quartz
Job instances. Note that this default only applies
to a local Scheduler, not to a RemoteScheduler (where setting
a custom JobFactory is not supported by Quartz).
Specify an instance of Spring's SpringBeanJobFactory here
(typically as an inner bean definition) to automatically populate a job's
bean properties from the specified job data map and scheduler context.
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 Integer.MAX_VALUE
meaning that this scheduler starts as late as possible and stops as soon
public int getPhase()
Return the phase in which this scheduler will be started and stopped.
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.
public void setExposeSchedulerInRepository(boolean exposeSchedulerInRepository)
Set whether to expose the Spring-managed Scheduler instance in the
Quartz SchedulerRepository. Default is "false", since the Spring-managed
Scheduler is usually exclusively intended for access within the Spring context.
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.
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.
name - 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 BeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.
Return an instance (possibly shared or independent) of the object
managed by this factory.
As with a BeanFactory, this allows support for both the
Singleton and Prototype design pattern.
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 FactoryBeanNotInitializedException.
As of Spring 2.0, FactoryBeans are allowed to return null
objects. The factory will consider this as normal value to be used; it
will not throw a FactoryBeanNotInitializedException in this case anymore.
FactoryBean implementations are encouraged to throw
FactoryBeanNotInitializedException themselves now, as appropriate.
Return the type of object that this FactoryBean creates,
or null if not known in advance.
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
null here. Therefore it is highly recommended to implement
this method properly, using the current state of the FactoryBean.
Is the object managed by this factory a singleton? That is,
will FactoryBean.getObject() always return the same object
(a reference that can be cached)?
NOTE: If a FactoryBean indicates to hold a singleton object,
the object returned from getObject() might get cached
by the owning BeanFactory. Hence, do not return true
unless the FactoryBean always exposes the same reference.
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 false does not
necessarily indicate that returned objects are independent instances.
An implementation of the extended SmartFactoryBean interface
may explicitly indicate independent instances through its
SmartFactoryBean.isPrototype() method. Plain FactoryBean
implementations which do not implement this extended interface are
simply assumed to always return independent instances if the
isSingleton() implementation returns false.
The default implementation returns true, since a
FactoryBean typically manages a singleton instance.
Stop this component, typically in a synchronous fashion, such that the component is
fully stopped upon return of this method. Consider implementing SmartLifecycle
and its stop(Runnable) variant when asynchronous stop behavior is necessary.
Note that this stop notification is not guaranteed to come before destruction:
On regular shutdown, Lifecycle beans will first receive a stop notification
before the general destruction callbacks are being propagated; however, on hot
refresh during a context's lifetime or on aborted refresh attempts, a given bean's
destroy method will be called without any consideration of stop signals upfront.
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