Top Description Fields Constructors Methods
org.springframework.scheduling.quartz

public Class SchedulerFactoryBean

extends SchedulerAccessor
implements org.springframework.beans.factory.FactoryBean<org.quartz.Scheduler>, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle
Class Inheritance
All Implemented Interfaces
org.springframework.context.SmartLifecycle, org.springframework.context.Phased, org.springframework.context.Lifecycle, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.FactoryBean
Imports
java.io.IOException, java.util.Map, .Properties, java.util.concurrent.Executor, .TimeUnit, javax.sql.DataSource, org.quartz.Scheduler, org.quartz.SchedulerException, org.quartz.SchedulerFactory, org.quartz.impl.RemoteScheduler, org.quartz.impl.SchedulerRepository, org.quartz.impl.StdSchedulerFactory, org.quartz.simpl.SimpleThreadPool, org.quartz.spi.JobFactory, org.springframework.beans.BeanUtils, org.springframework.beans.factory.BeanNameAware, .DisposableBean, .FactoryBean, .InitializingBean, org.springframework.context.ApplicationContext, .ApplicationContextAware, .SmartLifecycle, org.springframework.core.io.Resource, .ResourceLoader, org.springframework.core.io.support.PropertiesLoaderUtils, org.springframework.lang.Nullable, org.springframework.scheduling.SchedulingException, org.springframework.util.Assert, .CollectionUtils

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.

Author
Juergen Hoeller
Since
18.02.2004
See Also
setDataSource, org.quartz.Scheduler, org.quartz.SchedulerFactory, org.quartz.impl.StdSchedulerFactory, org.springframework.transaction.interceptor.TransactionProxyFactoryBean

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private String
private boolean
private String
private Resource
private static final ThreadLocal<DataSource>
private static final ThreadLocal<DataSource>
private static final ThreadLocal<ResourceLoader>
private static final ThreadLocal<Executor>
private DataSource
public static final int
DEFAULT_THREAD_COUNT

The default thread count.

private boolean
private org.quartz.spi.JobFactory
private boolean
private DataSource
private int
public static final String
PROP_THREAD_COUNT

The thread count property.

private Properties
private org.quartz.Scheduler
private Map<String, ?>
private org.quartz.SchedulerFactory
private Class<? extends org.quartz.SchedulerFactory>
private String
private int
private Executor
private boolean
Inherited from org.springframework.scheduling.quartz.SchedulerAccessor:
loggerresourceLoader

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

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.quartz.Scheduler

Returns:

the Scheduler instance
createScheduler
(org.quartz.SchedulerFactory
the factory to create the Scheduler with
schedulerFactory
,
String
the name of the scheduler to create
schedulerName
)

Create the Scheduler instance for the given factory and scheduler name.

public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Shut down the Quartz scheduler on bean factory shutdown, stopping all scheduled jobs.

public static DataSource
getConfigTimeDataSource()

Return the DataSource for the currently configured Quartz Scheduler, to be used by LocalDataSourceJobStore.

public static DataSource
getConfigTimeNonTransactionalDataSource()

Return the non-transactional DataSource for the currently configured Quartz Scheduler, to be used by LocalDataSourceJobStore.

public static ResourceLoader
getConfigTimeResourceLoader()

Return the ResourceLoader for the currently configured Quartz Scheduler, to be used by ResourceLoaderClassLoadHelper.

public static Executor
getConfigTimeTaskExecutor()

Return the Executor for the currently configured Quartz Scheduler, to be used by LocalTaskExecutorThreadPool.

public org.quartz.Scheduler
getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

public Class<? extends org.quartz.Scheduler>
getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.

public int
getPhase()

Overrides default org.springframework.context.SmartLifecycle.getPhase.

Return the phase in which this scheduler will be started and stopped.

public org.quartz.Scheduler
getScheduler()

Implements abstract org.springframework.scheduling.quartz.SchedulerAccessor.getScheduler.

Template method that determines the Scheduler to operate on.

private void
initSchedulerFactory(org.quartz.impl.StdSchedulerFactory
the SchedulerFactory to initialize
schedulerFactory
)

Initialize the given SchedulerFactory, applying locally defined Quartz properties to it.

public boolean
isAutoStartup()

Overrides default org.springframework.context.SmartLifecycle.isAutoStartup.

Return whether this scheduler is configured for auto-startup.

public boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

public boolean
isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton?

private void
populateSchedulerContext(org.quartz.Scheduler scheduler)

Expose the specified context attributes and/or the current ApplicationContext in the Quartz SchedulerContext.

private org.quartz.Scheduler
prepareScheduler(org.quartz.SchedulerFactory schedulerFactory)

private org.quartz.SchedulerFactory

Returns:

the initialized SchedulerFactory
prepareSchedulerFactory
()

Create 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.springframework.context.ApplicationContextAware.setApplicationContext.

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 ApplicationContext reference to expose in the SchedulerContext, for example "applicationContext".

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 BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.
name
)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

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
setDataSource(DataSource dataSource)

Set the default DataSource to be used by the Scheduler.

public void
setExposeSchedulerInRepository(boolean exposeSchedulerInRepository)

Set whether to expose the Spring-managed Scheduler instance in the Quartz SchedulerRepository.

public void
setJobFactory(org.quartz.spi.JobFactory jobFactory)

Set the Quartz JobFactory to use for this Scheduler.

public void
setNonTransactionalDataSource(DataSource nonTransactionalDataSource)

Set the DataSource to be used for non-transactional access.

public void
setPhase(int phase)

Specify the phase in which this scheduler should be started and stopped.

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.quartz.SchedulerFactory schedulerFactory)

Set an external Quartz SchedulerFactory instance to use.

public void
setSchedulerFactoryClass(Class<? extends org.quartz.SchedulerFactory> schedulerFactoryClass)

Set the Quartz SchedulerFactory implementation to use.

public void
setSchedulerName(String schedulerName)

Set the name of the Scheduler to create via the SchedulerFactory, as an alternative to the org.quartz.scheduler.instanceName property.

public void
setStartupDelay(int startupDelay)

Set the number of seconds to wait after initialization before starting the scheduler asynchronously.

public void
setTaskExecutor(Executor taskExecutor)

Set a Spring-managed Executor to use as Quartz backend.

public void
setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)

Set whether to wait for running jobs to complete on shutdown.

public void
start()

Implements org.springframework.context.Lifecycle.start.

Start this component.

protected void
startScheduler(final org.quartz.Scheduler
the Scheduler to start
scheduler
,
final int
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.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

Inherited from org.springframework.scheduling.quartz.SchedulerAccessor:
registerJobsAndTriggersregisterListenerssetCalendarssetGlobalJobListenerssetGlobalTriggerListenerssetJobDetailssetJobSchedulingDataLocationsetJobSchedulingDataLocationssetOverwriteExistingJobssetResourceLoadersetSchedulerListenerssetTransactionManagersetTriggers

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
applicationContextSchedulerContextKeyback to summary
private String applicationContextSchedulerContextKey
Annotations
@Nullable
autoStartupback to summary
private boolean autoStartup
beanNameback to summary
private String beanName
Annotations
@Nullable
configLocationback to summary
private Resource configLocation
Annotations
@Nullable
configTimeDataSourceHolderback to summary
private static final ThreadLocal<DataSource> configTimeDataSourceHolder
configTimeNonTransactionalDataSourceHolderback to summary
private static final ThreadLocal<DataSource> configTimeNonTransactionalDataSourceHolder
configTimeResourceLoaderHolderback to summary
private static final ThreadLocal<ResourceLoader> configTimeResourceLoaderHolder
configTimeTaskExecutorHolderback to summary
private static final ThreadLocal<Executor> configTimeTaskExecutorHolder
dataSourceback to summary
private DataSource dataSource
Annotations
@Nullable
DEFAULT_THREAD_COUNTback to summary
public static final int DEFAULT_THREAD_COUNT

The default thread count.

exposeSchedulerInRepositoryback to summary
private boolean exposeSchedulerInRepository
jobFactoryback to summary
private org.quartz.spi.JobFactory jobFactory
Annotations
@Nullable
jobFactorySetback to summary
private boolean jobFactorySet
nonTransactionalDataSourceback to summary
private DataSource nonTransactionalDataSource
Annotations
@Nullable
phaseback to summary
private int phase
PROP_THREAD_COUNTback to summary
public static final String PROP_THREAD_COUNT

The thread count property.

quartzPropertiesback to summary
private Properties quartzProperties
Annotations
@Nullable
schedulerback to summary
private org.quartz.Scheduler scheduler
Annotations
@Nullable
schedulerContextMapback to summary
private Map<String, ?> schedulerContextMap
Annotations
@Nullable
schedulerFactoryback to summary
private org.quartz.SchedulerFactory schedulerFactory
Annotations
@Nullable
schedulerFactoryClassback to summary
private Class<? extends org.quartz.SchedulerFactory> schedulerFactoryClass
schedulerNameback to summary
private String schedulerName
Annotations
@Nullable
startupDelayback to summary
private int startupDelay
taskExecutorback to summary
private Executor taskExecutor
Annotations
@Nullable
waitForJobsToCompleteOnShutdownback to summary
private boolean waitForJobsToCompleteOnShutdown

Constructor Detail

SchedulerFactoryBeanback to summary
public SchedulerFactoryBean()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet() throws Exception

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
Exceptions
Exception:
in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
createSchedulerback to summary
protected org.quartz.Scheduler createScheduler(org.quartz.SchedulerFactory schedulerFactory, String schedulerName) throws org.quartz.SchedulerException

Create the Scheduler instance for the given factory and scheduler name. Called by afterPropertiesSet.

The default implementation invokes SchedulerFactory's getScheduler method. Can be overridden for custom Scheduler creation.

Parameters
schedulerFactory:org.quartz.SchedulerFactory

the factory to create the Scheduler with

schedulerName:String

the name of the scheduler to create

Returns:org.quartz.Scheduler

the Scheduler instance

Exceptions
SchedulerException:
if thrown by Quartz methods
See Also
afterPropertiesSet, org.quartz.SchedulerFactory#getScheduler
destroyback to summary
public void destroy() throws org.quartz.SchedulerException

Implements org.springframework.beans.factory.DisposableBean.destroy.

Shut down the Quartz scheduler on bean factory shutdown, stopping all scheduled jobs.

Annotations
@Override
getConfigTimeDataSourceback to summary
public static DataSource getConfigTimeDataSource()

Return the DataSource for the currently configured Quartz Scheduler, to be used by LocalDataSourceJobStore.

This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.

Annotations
@Nullable
Since
1.1
See Also
setDataSource, LocalDataSourceJobStore
getConfigTimeNonTransactionalDataSourceback to summary
public static DataSource getConfigTimeNonTransactionalDataSource()

Return the non-transactional DataSource for the currently configured Quartz Scheduler, to be used by LocalDataSourceJobStore.

This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.

Annotations
@Nullable
Since
1.1
See Also
setNonTransactionalDataSource, LocalDataSourceJobStore
getConfigTimeResourceLoaderback to summary
public static ResourceLoader getConfigTimeResourceLoader()

Return the ResourceLoader for the currently configured Quartz Scheduler, to be used by ResourceLoaderClassLoadHelper.

This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.

Annotations
@Nullable
See Also
setApplicationContext, ResourceLoaderClassLoadHelper
getConfigTimeTaskExecutorback to summary
public static Executor getConfigTimeTaskExecutor()

Return the Executor for the currently configured Quartz Scheduler, to be used by LocalTaskExecutorThreadPool.

This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.

Annotations
@Nullable
Since
2.0
See Also
setTaskExecutor, LocalTaskExecutorThreadPool
getObjectback to summary
public org.quartz.Scheduler getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Doc from org.springframework.beans.factory.FactoryBean.getObject.

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.

Returns:org.quartz.Scheduler

an instance of the bean (can be null)

Annotations
@Override
@Nullable
getObjectTypeback to summary
public Class<? extends org.quartz.Scheduler> getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Doc from org.springframework.beans.factory.FactoryBean.getObjectType.

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.

Returns:Class<? extends org.quartz.Scheduler>

the type of object that this FactoryBean creates, or null if not known at the time of the call

Annotations
@Override
getPhaseback to summary
public int getPhase()

Overrides default org.springframework.context.SmartLifecycle.getPhase.

Return the phase in which this scheduler will be started and stopped.

Annotations
@Override
getSchedulerback to summary
public org.quartz.Scheduler getScheduler()

Implements abstract org.springframework.scheduling.quartz.SchedulerAccessor.getScheduler.

Doc from org.springframework.scheduling.quartz.SchedulerAccessor.getScheduler.

Template method that determines the Scheduler to operate on. To be implemented by subclasses.

Annotations
@Override
initSchedulerFactoryback to summary
private void initSchedulerFactory(org.quartz.impl.StdSchedulerFactory schedulerFactory) throws org.quartz.SchedulerException, IOException

Initialize the given SchedulerFactory, applying locally defined Quartz properties to it.

Parameters
schedulerFactory:org.quartz.impl.StdSchedulerFactory

the SchedulerFactory to initialize

isAutoStartupback to summary
public boolean isAutoStartup()

Overrides default org.springframework.context.SmartLifecycle.isAutoStartup.

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.

Annotations
@Override
isRunningback to summary
public boolean isRunning() throws SchedulingException

Implements org.springframework.context.Lifecycle.isRunning.

Doc from org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

In the case of a container, this will return true only if all components that apply are currently running.

Returns:boolean

whether the component is currently running

Annotations
@Override
isSingletonback to summary
public boolean isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Doc from org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton? That is, will 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.

Returns:boolean

whether the exposed object is a singleton

Annotations
@Override
populateSchedulerContextback to summary
private void populateSchedulerContext(org.quartz.Scheduler scheduler) throws org.quartz.SchedulerException

Expose the specified context attributes and/or the current ApplicationContext in the Quartz SchedulerContext.

prepareSchedulerback to summary
private org.quartz.Scheduler prepareScheduler(org.quartz.SchedulerFactory schedulerFactory) throws org.quartz.SchedulerException
prepareSchedulerFactoryback to summary
private org.quartz.SchedulerFactory prepareSchedulerFactory() throws org.quartz.SchedulerException, IOException

Create a SchedulerFactory if necessary and apply locally defined Quartz properties to it.

Returns:org.quartz.SchedulerFactory

the initialized SchedulerFactory

setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

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 org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
applicationContext:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
setApplicationContextSchedulerContextKeyback to summary
public void setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey)

Set the key of an ApplicationContext reference to expose in the SchedulerContext, for example "applicationContext". Default is none. Only applicable when running in a Spring ApplicationContext.

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.

See Also
JobDetailFactoryBean#setApplicationContextJobDataKey, org.springframework.context.ApplicationContext
setAutoStartupback 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.

setBeanNameback to summary
public void setBeanName(String name)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Doc from org.springframework.beans.factory.BeanNameAware.setBeanName.

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 InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
name: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 BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.

Annotations
@Override
setConfigLocationback 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.

See Also
setQuartzProperties
setDataSourceback to summary
public void setDataSource(DataSource dataSource)

Set the default DataSource to be used by the Scheduler.

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 LocalDataSourceJobStore but that's the default when using this method anyway.)

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.

Since
1.1
See Also
setNonTransactionalDataSource, setQuartzProperties, setTransactionManager, LocalDataSourceJobStore
setExposeSchedulerInRepositoryback to summary
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.

setJobFactoryback to summary
public void setJobFactory(org.quartz.spi.JobFactory jobFactory)

Set the Quartz JobFactory to use for this Scheduler.

Default is Spring's AdaptableJobFactory, which supports java.lang.Runnable objects as well as standard Quartz org.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.

Since
2.0
See Also
AdaptableJobFactory, SpringBeanJobFactory
setNonTransactionalDataSourceback to summary
public void setNonTransactionalDataSource(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.

Since
1.1
See Also
setDataSource, LocalDataSourceJobStore
setPhaseback 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 Integer.MAX_VALUE meaning that this scheduler starts as late as possible and stops as soon as possible.

Since
3.0
setQuartzPropertiesback 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.

See Also
setConfigLocation
setSchedulerContextAsMapback 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.

Parameters
schedulerContextAsMap:Map<String, ?>

a Map with String keys and any objects as values (for example Spring-managed beans)

See Also
JobDetailFactoryBean#setJobDataAsMap
setSchedulerFactoryback to summary
public void setSchedulerFactory(org.quartz.SchedulerFactory schedulerFactory)

Set an external Quartz SchedulerFactory instance to use.

Default is an internal StdSchedulerFactory instance. If this method is called, it overrides any class specified through setSchedulerFactoryClass as well as any settings specified through setConfigLocation, setQuartzProperties, setTaskExecutor or setDataSource.

Note

With an externally provided SchedulerFactory instance, local settings such as setConfigLocation or setQuartzProperties will be ignored here in SchedulerFactoryBean, expecting the external SchedulerFactory instance to get initialized on its own.

Since
4.3.15
See Also
setSchedulerFactoryClass
setSchedulerFactoryClassback to summary
public void setSchedulerFactoryClass(Class<? extends org.quartz.SchedulerFactory> schedulerFactoryClass)

Set the Quartz SchedulerFactory implementation to use.

Default is the StdSchedulerFactory class, reading in the standard quartz.properties from quartz.jar. For applying custom Quartz properties, specify "configLocation" and/or "quartzProperties" etc on this local SchedulerFactoryBean instance.

See Also
org.quartz.impl.StdSchedulerFactory, setConfigLocation, setQuartzProperties, setTaskExecutor, setDataSource
setSchedulerNameback to summary
public void setSchedulerName(String schedulerName)

Set the name of the Scheduler to create via the SchedulerFactory, as an alternative to the org.quartz.scheduler.instanceName property.

If not specified, the name will be taken from Quartz properties (org.quartz.scheduler.instanceName), or from the declared SchedulerFactoryBean bean name as a fallback.

See Also
setBeanName, StdSchedulerFactory#PROP_SCHED_INSTANCE_NAME, org.quartz.SchedulerFactory#getScheduler(), org.quartz.SchedulerFactory#getScheduler(String)
setStartupDelayback 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.

setTaskExecutorback to summary
public void setTaskExecutor(Executor taskExecutor)

Set a Spring-managed Executor to use as Quartz backend. Exposed as thread pool through the Quartz SPI.

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.

Since
2.0
See Also
setQuartzProperties, LocalTaskExecutorThreadPool, org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor, org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor
setWaitForJobsToCompleteOnShutdownback 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.

See Also
org.quartz.Scheduler#shutdown(boolean)
startback to summary
public void start() throws SchedulingException

Implements org.springframework.context.Lifecycle.start.

Doc from org.springframework.context.Lifecycle.start.

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.

Annotations
@Override
startSchedulerback to summary
protected void startScheduler(final org.quartz.Scheduler scheduler, final int startupDelay) throws org.quartz.SchedulerException

Start the Quartz Scheduler, respecting the "startupDelay" setting.

Parameters
scheduler:org.quartz.Scheduler

the Scheduler to start

startupDelay:int

the number of seconds to wait before starting the Scheduler asynchronously

stopback to summary
public void stop() throws SchedulingException

Implements org.springframework.context.Lifecycle.stop.

Doc from org.springframework.context.Lifecycle.stop.

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 that apply.

Annotations
@Override