类 SchedulerFactoryBean
- java.lang.Object
- org.springframework.scheduling.quartz.SchedulerAccessor
- org.springframework.scheduling.quartz.SchedulerFactoryBean
- 所有已实现的接口:
Aware
,BeanNameAware
,DisposableBean
,FactoryBean<Scheduler>
,InitializingBean
,ApplicationContextAware
,Lifecycle
,Phased
,ResourceLoaderAware
,SmartLifecycle
public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean<Scheduler>, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean, SmartLifecycle
FactoryBean
that creates and configures a QuartzScheduler
, 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.
- 从以下版本开始:
- 18.02.2004
- 作者:
- Juergen Hoeller
- 另请参阅:
setDataSource(javax.sql.DataSource)
,Scheduler
,SchedulerFactory
,StdSchedulerFactory
,TransactionProxyFactoryBean
字段概要
字段 修饰符和类型 字段 说明 static int
DEFAULT_THREAD_COUNT
static String
PROP_THREAD_COUNT
从类继承的字段 org.springframework.scheduling.quartz.SchedulerAccessor
logger, resourceLoader
构造器概要
构造器 构造器 说明 SchedulerFactoryBean()
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected Scheduler
createScheduler(SchedulerFactory schedulerFactory, String schedulerName)
Create the Scheduler instance for the given factory and scheduler name.void
destroy()
Shut down the Quartz scheduler on bean factory shutdown, stopping all scheduled jobs.static DataSource
getConfigTimeDataSource()
Return theDataSource
for the currently configured Quartz Scheduler, to be used byLocalDataSourceJobStore
.static DataSource
getConfigTimeNonTransactionalDataSource()
Return the non-transactionalDataSource
for the currently configured Quartz Scheduler, to be used byLocalDataSourceJobStore
.static ResourceLoader
getConfigTimeResourceLoader()
Return theResourceLoader
for the currently configured Quartz Scheduler, to be used byResourceLoaderClassLoadHelper
.static Executor
getConfigTimeTaskExecutor()
Return theExecutor
for the currently configured Quartz Scheduler, to be used byLocalTaskExecutorThreadPool
.Scheduler
getObject()
Return an instance (possibly shared or independent) of the object managed by this factory.Class<? extends Scheduler>
getObjectType()
Return the type of object that this FactoryBean creates, ornull
if not known in advance.int
getPhase()
Return the phase in which this scheduler will be started and stopped.Scheduler
getScheduler()
Template method that determines the Scheduler to operate on.boolean
isAutoStartup()
Return whether this scheduler is configured for auto-startup.boolean
isRunning()
Check whether this component is currently running.boolean
isSingleton()
Is the object managed by this factory a singleton?void
setApplicationContext(ApplicationContext applicationContext)
Set the ApplicationContext that this object runs in.void
setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey)
Set the key of anApplicationContext
reference to expose in the SchedulerContext, for example "applicationContext".void
setAutoStartup(boolean autoStartup)
Set whether to automatically start the scheduler after initialization.void
setBeanName(String name)
Set the name of the bean in the bean factory that created this bean.void
setConfigLocation(Resource configLocation)
Set the location of the Quartz properties config file, for example as classpath resource "classpath:quartz.properties".void
setDataSource(DataSource dataSource)
Set the defaultDataSource
to be used by the Scheduler.void
setExposeSchedulerInRepository(boolean exposeSchedulerInRepository)
Set whether to expose the Spring-managedScheduler
instance in the QuartzSchedulerRepository
.void
setJobFactory(JobFactory jobFactory)
Set the QuartzJobFactory
to use for this Scheduler.void
setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
Set theDataSource
to be used for non-transactional access.void
setPhase(int phase)
Specify the phase in which this scheduler should be started and stopped.void
setQuartzProperties(Properties quartzProperties)
Set Quartz properties, like "org.quartz.threadPool.class".void
setSchedulerContextAsMap(Map<String,?> schedulerContextAsMap)
Register objects in the Scheduler context via a given Map.void
setSchedulerFactory(SchedulerFactory schedulerFactory)
Set an external QuartzSchedulerFactory
instance to use.void
setSchedulerFactoryClass(Class<? extends SchedulerFactory> schedulerFactoryClass)
Set the QuartzSchedulerFactory
implementation to use.void
setSchedulerName(String schedulerName)
Set the name of the Scheduler to create via the SchedulerFactory.void
setStartupDelay(int startupDelay)
Set the number of seconds to wait after initialization before starting the scheduler asynchronously.void
setTaskExecutor(Executor taskExecutor)
Set a Spring-managedExecutor
to use as Quartz backend.void
setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)
Set whether to wait for running jobs to complete on shutdown.void
start()
Start this component.protected void
startScheduler(Scheduler scheduler, int startupDelay)
Start the Quartz Scheduler, respecting the "startupDelay" setting.void
stop()
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.void
stop(Runnable callback)
Indicates that a Lifecycle component must stop if it is currently running.从类继承的方法 org.springframework.scheduling.quartz.SchedulerAccessor
registerJobsAndTriggers, registerListeners, setCalendars, setGlobalJobListeners, setGlobalTriggerListeners, setJobDetails, setJobSchedulingDataLocation, setJobSchedulingDataLocations, setOverwriteExistingJobs, setResourceLoader, setSchedulerListeners, setTransactionManager, setTriggers
字段详细资料
PROP_THREAD_COUNT
public static final String PROP_THREAD_COUNT
- 另请参阅:
- 常量字段值
DEFAULT_THREAD_COUNT
public static final int DEFAULT_THREAD_COUNT
- 另请参阅:
- 常量字段值
构造器详细资料
SchedulerFactoryBean
public SchedulerFactoryBean()
方法详细资料
getConfigTimeResourceLoader
public static ResourceLoader getConfigTimeResourceLoader()
Return theResourceLoader
for the currently configured Quartz Scheduler, to be used byResourceLoaderClassLoadHelper
.This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
getConfigTimeTaskExecutor
public static Executor getConfigTimeTaskExecutor()
Return theExecutor
for the currently configured Quartz Scheduler, to be used byLocalTaskExecutorThreadPool
.This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
getConfigTimeDataSource
public static DataSource getConfigTimeDataSource()
Return theDataSource
for the currently configured Quartz Scheduler, to be used byLocalDataSourceJobStore
.This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
- 从以下版本开始:
- 1.1
- 另请参阅:
setDataSource(javax.sql.DataSource)
,LocalDataSourceJobStore
getConfigTimeNonTransactionalDataSource
public static DataSource getConfigTimeNonTransactionalDataSource()
Return the non-transactionalDataSource
for the currently configured Quartz Scheduler, to be used byLocalDataSourceJobStore
.This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
setSchedulerFactory
public void setSchedulerFactory(SchedulerFactory schedulerFactory)
Set an external QuartzSchedulerFactory
instance to use.Default is an internal
StdSchedulerFactory
instance. If this method is called, it overrides any class specified throughsetSchedulerFactoryClass(java.lang.Class<? extends org.quartz.SchedulerFactory>)
as well as any settings specified throughsetConfigLocation(org.springframework.core.io.Resource)
,setQuartzProperties(java.util.Properties)
,setTaskExecutor(java.util.concurrent.Executor)
orsetDataSource(javax.sql.DataSource)
.NOTE: With an externally provided
SchedulerFactory
instance, local settings such assetConfigLocation(org.springframework.core.io.Resource)
orsetQuartzProperties(java.util.Properties)
will be ignored here inSchedulerFactoryBean
, expecting the externalSchedulerFactory
instance to get initialized on its own.
setSchedulerFactoryClass
public void setSchedulerFactoryClass(Class<? extends SchedulerFactory> schedulerFactoryClass)
Set the QuartzSchedulerFactory
implementation to use.Default is the
StdSchedulerFactory
class, reading in the standardquartz.properties
fromquartz.jar
. For applying custom Quartz properties, specify"configLocation"
and/or"quartzProperties"
etc on this localSchedulerFactoryBean
instance.
setSchedulerName
public void setSchedulerName(String schedulerName)
Set the name of the Scheduler to create via the SchedulerFactory.If not specified, the bean name will be used as default scheduler name.
setConfigLocation
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.
setQuartzProperties
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.
setTaskExecutor
public void setTaskExecutor(Executor taskExecutor)
Set a Spring-managedExecutor
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.
setDataSource
public void setDataSource(DataSource dataSource)
Set the defaultDataSource
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.
setNonTransactionalDataSource
public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
Set theDataSource
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.
- 从以下版本开始:
- 1.1
- 另请参阅:
setDataSource(javax.sql.DataSource)
,LocalDataSourceJobStore
setSchedulerContextAsMap
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.
- 参数:
schedulerContextAsMap
- a Map with String keys and any objects as values (for example Spring-managed beans)- 另请参阅:
JobDetailFactoryBean.setJobDataAsMap(java.util.Map<java.lang.String, ?>)
setApplicationContextSchedulerContextKey
public void setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey)
Set the key of anApplicationContext
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.
setJobFactory
public void setJobFactory(JobFactory jobFactory)
Set the QuartzJobFactory
to use for this Scheduler.Default is Spring's
AdaptableJobFactory
, which supportsRunnable
objects as well as standard QuartzJob
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.- 从以下版本开始:
- 2.0
- 另请参阅:
AdaptableJobFactory
,SpringBeanJobFactory
setAutoStartup
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.
isAutoStartup
public boolean 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.
setPhase
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 isInteger.MAX_VALUE
meaning that this scheduler starts as late as possible and stops as soon as possible.- 从以下版本开始:
- 3.0
getPhase
public int getPhase()
Return the phase in which this scheduler will be started and stopped.
setStartupDelay
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.
setExposeSchedulerInRepository
public void setExposeSchedulerInRepository(boolean exposeSchedulerInRepository)
Set whether to expose the Spring-managedScheduler
instance in the QuartzSchedulerRepository
. 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.
setWaitForJobsToCompleteOnShutdown
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.
setBeanName
public void setBeanName(String name)
从接口复制的说明:BeanNameAware
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.- 指定者:
setBeanName
在接口中BeanNameAware
- 参数:
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 theBeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.
setApplicationContext
public void setApplicationContext(ApplicationContext applicationContext)
从接口复制的说明:ApplicationContextAware
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
InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked afterResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
andMessageSourceAware
, if applicable.- 指定者:
setApplicationContext
在接口中ApplicationContextAware
- 参数:
applicationContext
- the ApplicationContext object to be used by this object- 另请参阅:
BeanInitializationException
afterPropertiesSet
public void afterPropertiesSet() throws Exception
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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.
- 指定者:
afterPropertiesSet
在接口中InitializingBean
- 抛出:
Exception
- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
createScheduler
protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) throws SchedulerException
Create the Scheduler instance for the given factory and scheduler name. Called byafterPropertiesSet()
.The default implementation invokes SchedulerFactory's
getScheduler
method. Can be overridden for custom Scheduler creation.- 参数:
schedulerFactory
- the factory to create the Scheduler withschedulerName
- the name of the scheduler to create- 返回:
- the Scheduler instance
- 抛出:
SchedulerException
- if thrown by Quartz methods- 另请参阅:
afterPropertiesSet()
,SchedulerFactory.getScheduler()
startScheduler
protected void startScheduler(Scheduler scheduler, int startupDelay) throws SchedulerException
Start the Quartz Scheduler, respecting the "startupDelay" setting.- 参数:
scheduler
- the Scheduler to startstartupDelay
- the number of seconds to wait before starting the Scheduler asynchronously- 抛出:
SchedulerException
getScheduler
public Scheduler getScheduler()
从类复制的说明:SchedulerAccessor
Template method that determines the Scheduler to operate on. To be implemented by subclasses.- 指定者:
getScheduler
在类中SchedulerAccessor
getObject
public Scheduler getObject()
从接口复制的说明:FactoryBean
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.- 指定者:
getObject
在接口中FactoryBean<Scheduler>
- 返回:
- an instance of the bean (can be
null
) - 另请参阅:
FactoryBeanNotInitializedException
getObjectType
public Class<? extends Scheduler> getObjectType()
从接口复制的说明:FactoryBean
Return the type of object that this FactoryBean creates, ornull
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.- 指定者:
getObjectType
在接口中FactoryBean<Scheduler>
- 返回:
- the type of object that this FactoryBean creates, or
null
if not known at the time of the call - 另请参阅:
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
isSingleton
public boolean isSingleton()
从接口复制的说明:FactoryBean
Is the object managed by this factory a singleton? That is, willFactoryBean.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 returntrue
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 extendedSmartFactoryBean
interface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()
method. PlainFactoryBean
implementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()
implementation returnsfalse
.- 指定者:
isSingleton
在接口中FactoryBean<Scheduler>
- 返回:
- whether the exposed object is a singleton
- 另请参阅:
FactoryBean.getObject()
,SmartFactoryBean.isPrototype()
start
public void start() throws SchedulingException
从接口复制的说明:Lifecycle
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.
- 指定者:
start
在接口中Lifecycle
- 抛出:
SchedulingException
- 另请参阅:
SmartLifecycle.isAutoStartup()
stop
public void stop() throws SchedulingException
从接口复制的说明:Lifecycle
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycle
and itsstop(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.
stop
public void stop(Runnable callback) throws SchedulingException
从接口复制的说明:SmartLifecycle
Indicates that a Lifecycle component must stop if it is currently running.The provided callback is used by the
LifecycleProcessor
to support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after theSmartLifecycle
component does indeed stop.The
LifecycleProcessor
will call only this variant of thestop
method; i.e.Lifecycle.stop()
will not be called forSmartLifecycle
implementations unless explicitly delegated to within the implementation of this method.- 指定者:
stop
在接口中SmartLifecycle
- 抛出:
SchedulingException
- 另请参阅:
Lifecycle.stop()
,Phased.getPhase()
isRunning
public boolean isRunning() throws SchedulingException
从接口复制的说明:Lifecycle
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.- 指定者:
isRunning
在接口中Lifecycle
- 返回:
- whether the component is currently running
- 抛出:
SchedulingException
destroy
public void destroy() throws SchedulerException
Shut down the Quartz scheduler on bean factory shutdown, stopping all scheduled jobs.- 指定者:
destroy
在接口中DisposableBean
- 抛出:
SchedulerException