|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.scheduling.quartz.SchedulerFactoryBean
public class SchedulerFactoryBean
FactoryBean that sets up a Quartz Scheduler
,
manages its lifecycle as part of the Spring application context,
and exposes the Scheduler 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.
This version of Spring's SchedulerFactoryBean requires Quartz 1.5 or higher.
setDataSource(javax.sql.DataSource)
,
Scheduler
,
SchedulerFactory
,
StdSchedulerFactory
,
TransactionProxyFactoryBean
Field Summary | |
---|---|
static int |
DEFAULT_THREAD_COUNT
|
protected Log |
logger
|
static String |
PROP_THREAD_COUNT
|
Constructor Summary | |
---|---|
SchedulerFactoryBean()
|
Method Summary | |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware). |
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 the DataSource for the currently configured Quartz Scheduler, to be used by LocalDataSourceJobStore. |
static DataSource |
getConfigTimeNonTransactionalDataSource()
Return the non-transactional DataSource for the currently configured Quartz Scheduler, to be used by LocalDataSourceJobStore. |
static TaskExecutor |
getConfigTimeTaskExecutor()
Return the TaskExecutor for the currently configured Quartz Scheduler, to be used by LocalTaskExecutorThreadPool. |
Object |
getObject()
Return an instance (possibly shared or independent) of the object managed by this factory. |
Class |
getObjectType()
Return the type of object that this FactoryBean creates, or null if not known in advance. |
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 an ApplicationContext reference to expose in the SchedulerContext, for example "applicationContext". |
void |
setAutoStartup(boolean autoStartup)
Set whether to automatically start the scheduler after initialization. |
void |
setCalendars(Map calendars)
Register a list of Quartz Calendar objects with the Scheduler that this FactoryBean creates, to be referenced by Triggers. |
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 default DataSource to be used by the Scheduler. |
void |
setGlobalJobListeners(JobListener[] globalJobListeners)
Specify global Quartz JobListeners to be registered with the Scheduler. |
void |
setGlobalTriggerListeners(TriggerListener[] globalTriggerListeners)
Specify global Quartz TriggerListeners to be registered with the Scheduler. |
void |
setJobDetails(JobDetail[] jobDetails)
Register a list of JobDetail objects with the Scheduler that this FactoryBean creates, to be referenced by Triggers. |
void |
setJobFactory(JobFactory jobFactory)
Set the Quartz JobFactory to use for this Scheduler. |
void |
setJobListeners(JobListener[] jobListeners)
Specify named Quartz JobListeners to be registered with the Scheduler. |
void |
setJobSchedulingDataLocation(String jobSchedulingDataLocation)
Set the location of a Quartz job definition XML file that follows the "job_scheduling_data_1_0" DTD. |
void |
setJobSchedulingDataLocations(String[] jobSchedulingDataLocations)
Set the locations of Quartz job definition XML files that follow the "job_scheduling_data_1_0" DTD. |
void |
setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
Set the DataSource to be used by the Scheduler for non-transactional access. |
void |
setOverwriteExistingJobs(boolean overwriteExistingJobs)
Set whether any jobs defined on this SchedulerFactoryBean should overwrite existing job definitions. |
void |
setQuartzProperties(Properties quartzProperties)
Set Quartz properties, like "org.quartz.threadPool.class". |
void |
setSchedulerContextAsMap(Map schedulerContextAsMap)
Register objects in the Scheduler context via a given Map. |
void |
setSchedulerFactoryClass(Class schedulerFactoryClass)
Set the Quartz SchedulerFactory implementation to use. |
void |
setSchedulerListeners(SchedulerListener[] schedulerListeners)
Specify Quartz SchedulerListeners to be registered with the Scheduler. |
void |
setSchedulerName(String schedulerName)
Set the name of the Scheduler to fetch from the SchedulerFactory. |
void |
setStartupDelay(int startupDelay)
Set the number of seconds to wait after initialization before starting the scheduler asynchronously. |
void |
setTaskExecutor(TaskExecutor taskExecutor)
Set the Spring TaskExecutor to use as Quartz backend. |
void |
setTransactionManager(PlatformTransactionManager transactionManager)
Set the transaction manager to be used for registering jobs and triggers that are defined by this SchedulerFactoryBean. |
void |
setTriggerListeners(TriggerListener[] triggerListeners)
Specify named Quartz TriggerListeners to be registered with the Scheduler. |
void |
setTriggers(Trigger[] triggers)
Register a list of Trigger objects with the Scheduler that this FactoryBean creates. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String PROP_THREAD_COUNT
public static final int DEFAULT_THREAD_COUNT
protected final Log logger
Constructor Detail |
---|
public SchedulerFactoryBean()
Method Detail |
---|
public static TaskExecutor getConfigTimeTaskExecutor()
This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
setDataSource(javax.sql.DataSource)
,
LocalDataSourceJobStore
public static DataSource getConfigTimeDataSource()
This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
setDataSource(javax.sql.DataSource)
,
LocalDataSourceJobStore
public static DataSource getConfigTimeNonTransactionalDataSource()
This instance will be set before initialization of the corresponding Scheduler, and reset immediately afterwards. It is thus only available during configuration.
setNonTransactionalDataSource(javax.sql.DataSource)
,
LocalDataSourceJobStore
public void setSchedulerFactoryClass(Class schedulerFactoryClass)
Default is StdSchedulerFactory, reading in the standard quartz.properties from quartz.jar. To use custom Quartz properties, specify "configLocation" or "quartzProperties".
StdSchedulerFactory
,
setConfigLocation(org.springframework.core.io.Resource)
,
setQuartzProperties(java.util.Properties)
public void setSchedulerName(String schedulerName)
SchedulerFactory.getScheduler(String)
,
SchedulerFactory.getScheduler()
public void setConfigLocation(Resource configLocation)
Note: Can be omitted when all necessary properties are specified locally via this bean, or when relying on Quartz' default configuration.
setQuartzProperties(java.util.Properties)
public void setQuartzProperties(Properties quartzProperties)
Can be used to override values in a Quartz properties config file, or to specify all necessary properties locally.
setConfigLocation(org.springframework.core.io.Resource)
public void setTaskExecutor(TaskExecutor taskExecutor)
Can be used to assign a JDK 1.5 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.
setQuartzProperties(java.util.Properties)
,
LocalTaskExecutorThreadPool
,
ThreadPoolTaskExecutor
,
WorkManagerTaskExecutor
public void setDataSource(DataSource dataSource)
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(javax.sql.DataSource)
,
setQuartzProperties(java.util.Properties)
,
setTransactionManager(org.springframework.transaction.PlatformTransactionManager)
,
LocalDataSourceJobStore
public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
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.
setDataSource(javax.sql.DataSource)
,
LocalDataSourceJobStore
public void setTransactionManager(PlatformTransactionManager transactionManager)
setDataSource(javax.sql.DataSource)
public void setSchedulerContextAsMap(Map schedulerContextAsMap)
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
- Map with String keys and any objects as
values (for example Spring-managed beans)JobDetailBean.setJobDataAsMap(java.util.Map)
public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
public void setApplicationContextSchedulerContextKey(String applicationContextSchedulerContextKey)
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 reponsible for the lifecycle of its Jobs.
JobDetailBean.setApplicationContextJobDataKey(java.lang.String)
,
ApplicationContext
public void setJobFactory(JobFactory jobFactory)
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.
AdaptableJobFactory
,
SpringBeanJobFactory
public void setOverwriteExistingJobs(boolean overwriteExistingJobs)
public void setJobSchedulingDataLocation(String jobSchedulingDataLocation)
ResourceJobSchedulingDataProcessor
,
JobSchedulingDataProcessor
public void setJobSchedulingDataLocations(String[] jobSchedulingDataLocations)
ResourceJobSchedulingDataProcessor
,
JobSchedulingDataProcessor
public void setJobDetails(JobDetail[] jobDetails)
This is not necessary when a Trigger determines the JobDetail itself: In this case, the JobDetail will be implicitly registered in combination with the Trigger.
setTriggers(org.quartz.Trigger[])
,
JobDetail
,
JobDetailBean
,
JobDetailAwareTrigger
,
Trigger.setJobName(java.lang.String)
public void setCalendars(Map calendars)
calendars
- Map with calendar names as keys as Calendar
objects as valuesCalendar
,
Trigger.setCalendarName(java.lang.String)
public void setTriggers(Trigger[] triggers)
If the Trigger determines the corresponding JobDetail itself, the job will be automatically registered with the Scheduler. Else, the respective JobDetail needs to be registered via the "jobDetails" property of this FactoryBean.
setJobDetails(org.quartz.JobDetail[])
,
JobDetail
,
JobDetailAwareTrigger
,
CronTriggerBean
,
SimpleTriggerBean
public void setSchedulerListeners(SchedulerListener[] schedulerListeners)
public void setGlobalJobListeners(JobListener[] globalJobListeners)
public void setJobListeners(JobListener[] jobListeners)
JobListener.getName()
,
JobDetail.addJobListener(java.lang.String)
,
JobDetailBean.setJobListenerNames(java.lang.String[])
public void setGlobalTriggerListeners(TriggerListener[] globalTriggerListeners)
public void setTriggerListeners(TriggerListener[] triggerListeners)
TriggerListener.getName()
,
Trigger.addTriggerListener(java.lang.String)
,
CronTriggerBean.setTriggerListenerNames(java.lang.String[])
,
SimpleTriggerBean.setTriggerListenerNames(java.lang.String[])
public void setAutoStartup(boolean autoStartup)
public void setStartupDelay(int startupDelay)
Setting this to 10 or 20 seconds makes sense if no jobs should be run before the entire application has started up.
public void setWaitForJobsToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)
Default is "false". Switch this to "true" if you prefer fully completed jobs at the expense of a longer shutdown phase.
Scheduler.shutdown(boolean)
public void afterPropertiesSet() throws Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) throws SchedulerException
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
SchedulerException
- if thrown by Quartz methodsafterPropertiesSet()
,
SchedulerFactory.getScheduler()
protected void startScheduler(Scheduler scheduler, int startupDelay) throws SchedulerException
scheduler
- the Scheduler to startstartupDelay
- the number of seconds to wait before starting
the Scheduler asynchronously
SchedulerException
public Object getObject()
FactoryBean
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
in interface FactoryBean
null
)FactoryBeanNotInitializedException
public Class getObjectType()
FactoryBean
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.
getObjectType
in interface FactoryBean
null
if not known at the time of the callListableBeanFactory.getBeansOfType(java.lang.Class)
public boolean isSingleton()
FactoryBean
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
.
isSingleton
in interface FactoryBean
FactoryBean.getObject()
,
SmartFactoryBean.isPrototype()
public void start() throws SchedulingException
Lifecycle
In the case of a container, this will propagate the start signal to all components that apply.
start
in interface Lifecycle
SchedulingException
public void stop() throws SchedulingException
Lifecycle
In the case of a container, this will propagate the stop signal to all components that apply.
stop
in interface Lifecycle
SchedulingException
public boolean isRunning() throws SchedulingException
Lifecycle
In the case of a container, this will return true
only if all components that apply are currently running.
isRunning
in interface Lifecycle
SchedulingException
public void destroy() throws SchedulerException
destroy
in interface DisposableBean
SchedulerException
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |