Class ScheduledAnnotationBeanPostProcessor
- All Implemented Interfaces:
EventListener
,Aware
,BeanFactoryAware
,BeanNameAware
,BeanPostProcessor
,DestructionAwareBeanPostProcessor
,DisposableBean
,SmartInitializingSingleton
,MergedBeanDefinitionPostProcessor
,ApplicationContextAware
,ApplicationListener<ContextRefreshedEvent>
,EmbeddedValueResolverAware
,Ordered
,ScheduledTaskHolder
@Scheduled
to be invoked by a
TaskScheduler
according to the
"fixedRate", "fixedDelay", or "cron" expression provided via the annotation.
This post-processor is automatically registered by Spring's
<task:annotation-driven>
XML element and also by the
@EnableScheduling
annotation.
Autodetects any SchedulingConfigurer
instances in the container,
allowing for customization of the scheduler to be used or for fine-grained
control over task registration (e.g. registration of Trigger
tasks).
See the @EnableScheduling
javadocs for complete usage
details.
- Since:
- 3.0
- Author:
- Mark Fisher, Juergen Hoeller, Chris Beams, Elizabeth Chatman, Victor Brown, Sam Brannen, Simon Baslé
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The default name of theTaskScheduler
bean to pick up: "taskScheduler".protected final Log
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Constructor Summary
ConstructorDescriptionCreate a defaultScheduledAnnotationBeanPostProcessor
.Create aScheduledAnnotationBeanPostProcessor
delegating to the specifiedScheduledTaskRegistrar
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked right at the end of the singleton pre-instantiation phase, with a guarantee that all regular singleton beans have been created already.protected Runnable
createRunnable
(Object target, Method method) Create aRunnable
for the given bean instance, calling the specified scheduled method.void
destroy()
Invoked by the containingBeanFactory
on destruction of a bean.int
getOrder()
Get the order value of this object.Return all currently scheduled tasks, fromScheduled
methods as well as from programmaticSchedulingConfigurer
interaction.void
Handle an application event.postProcessAfterInitialization
(Object bean, String beanName) Apply thisBeanPostProcessor
to the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method).void
postProcessBeforeDestruction
(Object bean, String beanName) Apply this BeanPostProcessor to the given bean instance before its destruction, e.g.postProcessBeforeInitialization
(Object bean, String beanName) Apply thisBeanPostProcessor
to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method).void
postProcessMergedBeanDefinition
(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) Post-process the given merged bean definition for the specified bean.protected void
processScheduled
(Scheduled scheduled, Method method, Object bean) Process the given@Scheduled
method declaration on the given bean, attempting to distinguish reactive methods from synchronous methods.protected void
processScheduledAsync
(Scheduled scheduled, Method method, Object bean) Process the given@Scheduled
bean method declaration which returns aPublisher
, or the given Kotlin suspending function converted to aPublisher
.protected void
processScheduledSync
(Scheduled scheduled, Method method, Object bean) Process the given@Scheduled
method declaration on the given bean, as a synchronous method.protected void
processScheduledTask
(Scheduled scheduled, Runnable runnable, Method method, Object bean) Parse theScheduled
annotation and schedule the providedRunnable
accordingly.boolean
requiresDestruction
(Object bean) Determine whether the given bean instance requires destruction by this post-processor.void
setApplicationContext
(ApplicationContext applicationContext) Setting anApplicationContext
is optional: If set, registered tasks will be activated in theContextRefreshedEvent
phase; if not set, it will happen atafterSingletonsInstantiated()
time.void
setBeanFactory
(BeanFactory beanFactory) Making aBeanFactory
available is optional; if not set,SchedulingConfigurer
beans won't get autodetected and ascheduler
has to be explicitly configured.void
setBeanName
(String beanName) Set the name of the bean in the bean factory that created this bean.void
setEmbeddedValueResolver
(StringValueResolver resolver) Set the StringValueResolver to use for resolving embedded definition values.void
setScheduler
(Object scheduler) Set theTaskScheduler
that will invoke the scheduled methods, or aScheduledExecutorService
to be wrapped as a TaskScheduler.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor
resetBeanDefinition
-
Field Details
-
DEFAULT_TASK_SCHEDULER_BEAN_NAME
The default name of theTaskScheduler
bean to pick up: "taskScheduler".Note that the initial lookup happens by type; this is just the fallback in case of multiple scheduler beans found in the context.
- Since:
- 4.2
- See Also:
-
logger
-
-
Constructor Details
-
ScheduledAnnotationBeanPostProcessor
public ScheduledAnnotationBeanPostProcessor()Create a defaultScheduledAnnotationBeanPostProcessor
. -
ScheduledAnnotationBeanPostProcessor
Create aScheduledAnnotationBeanPostProcessor
delegating to the specifiedScheduledTaskRegistrar
.- Parameters:
registrar
- the ScheduledTaskRegistrar to register@Scheduled
tasks on- Since:
- 5.1
-
-
Method Details
-
getOrder
public int getOrder()Description copied from interface:Ordered
Get the order value of this object.Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet
load-on-startup
values).Same order values will result in arbitrary sort positions for the affected objects.
-
setScheduler
Set theTaskScheduler
that will invoke the scheduled methods, or aScheduledExecutorService
to be wrapped as a TaskScheduler.If not specified, default scheduler resolution will apply: searching for a unique
TaskScheduler
bean in the context, or for aTaskScheduler
bean named "taskScheduler" otherwise; the same lookup will also be performed for aScheduledExecutorService
bean. If neither of the two is resolvable, a local single-threaded default scheduler will be created within the registrar.- See Also:
-
setEmbeddedValueResolver
Description copied from interface:EmbeddedValueResolverAware
Set the StringValueResolver to use for resolving embedded definition values.- Specified by:
setEmbeddedValueResolver
in interfaceEmbeddedValueResolverAware
-
setBeanName
Description copied from interface: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.- Specified by:
setBeanName
in interfaceBeanNameAware
- Parameters:
beanName
- 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.
-
setBeanFactory
Making aBeanFactory
available is optional; if not set,SchedulingConfigurer
beans won't get autodetected and ascheduler
has to be explicitly configured.- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Parameters:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- See Also:
-
setApplicationContext
Setting anApplicationContext
is optional: If set, registered tasks will be activated in theContextRefreshedEvent
phase; if not set, it will happen atafterSingletonsInstantiated()
time.- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Parameters:
applicationContext
- the ApplicationContext object to be used by this object- See Also:
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()Description copied from interface:SmartInitializingSingleton
Invoked right at the end of the singleton pre-instantiation phase, with a guarantee that all regular singleton beans have been created already.ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
calls within this method won't trigger accidental side effects during bootstrap.NOTE: This callback won't be triggered for singleton beans lazily initialized on demand after
BeanFactory
bootstrap, and not for any other bean scope either. Carefully use it for beans with the intended bootstrap semantics only.- Specified by:
afterSingletonsInstantiated
in interfaceSmartInitializingSingleton
-
onApplicationEvent
Description copied from interface:ApplicationListener
Handle an application event.- Specified by:
onApplicationEvent
in interfaceApplicationListener<ContextRefreshedEvent>
- Parameters:
event
- the event to respond to
-
postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) Description copied from interface:MergedBeanDefinitionPostProcessor
Post-process the given merged bean definition for the specified bean.- Specified by:
postProcessMergedBeanDefinition
in interfaceMergedBeanDefinitionPostProcessor
- Parameters:
beanDefinition
- the merged bean definition for the beanbeanType
- the actual type of the managed bean instancebeanName
- the name of the bean- See Also:
-
postProcessBeforeInitialization
Description copied from interface:BeanPostProcessor
Apply thisBeanPostProcessor
to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.The default implementation returns the given
bean
as-is.- Specified by:
postProcessBeforeInitialization
in interfaceBeanPostProcessor
- Parameters:
bean
- the new bean instancebeanName
- the name of the bean- Returns:
- the bean instance to use, either the original or a wrapped one;
if
null
, no subsequent BeanPostProcessors will be invoked - See Also:
-
postProcessAfterInitialization
Description copied from interface:BeanPostProcessor
Apply thisBeanPostProcessor
to the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSet
or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance and the objects created by the FactoryBean (as of Spring 2.0). The post-processor can decide whether to apply to either the FactoryBean or created objects or both through corresponding
bean instanceof FactoryBean
checks.This callback will also be invoked after a short-circuiting triggered by a
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
method, in contrast to all otherBeanPostProcessor
callbacks.The default implementation returns the given
bean
as-is.- Specified by:
postProcessAfterInitialization
in interfaceBeanPostProcessor
- Parameters:
bean
- the new bean instancebeanName
- the name of the bean- Returns:
- the bean instance to use, either the original or a wrapped one;
if
null
, no subsequent BeanPostProcessors will be invoked - See Also:
-
processScheduled
Process the given@Scheduled
method declaration on the given bean, attempting to distinguish reactive methods from synchronous methods.- Parameters:
scheduled
- the@Scheduled
annotationmethod
- the method that the annotation has been declared onbean
- the target bean instance- See Also:
-
processScheduledTask
protected void processScheduledTask(Scheduled scheduled, Runnable runnable, Method method, Object bean) Parse theScheduled
annotation and schedule the providedRunnable
accordingly. The Runnable can represent either a synchronous method invocation (seeprocessScheduledSync(Scheduled, Method, Object)
) or an asynchronous one (seeprocessScheduledAsync(Scheduled, Method, Object)
). -
processScheduledSync
Process the given@Scheduled
method declaration on the given bean, as a synchronous method. The method must accept no arguments. Its return value is ignored (if any), and the scheduled invocations of the method take place using the underlyingTaskScheduler
infrastructure.- Parameters:
scheduled
- the@Scheduled
annotationmethod
- the method that the annotation has been declared onbean
- the target bean instance- See Also:
-
processScheduledAsync
Process the given@Scheduled
bean method declaration which returns aPublisher
, or the given Kotlin suspending function converted to aPublisher
. ARunnable
which subscribes to that publisher is then repeatedly scheduled according to the annotation configuration.Note that for fixed delay configuration, the subscription is turned into a blocking call instead. Types for which a
ReactiveAdapter
is registered but which cannot be deferred (i.e. not aPublisher
) are not supported.- Parameters:
scheduled
- the@Scheduled
annotationmethod
- the method that the annotation has been declared on, which must either return a Publisher-adaptable type or be a Kotlin suspending functionbean
- the target bean instance- See Also:
-
ScheduledAnnotationReactiveSupport
-
createRunnable
Create aRunnable
for the given bean instance, calling the specified scheduled method.The default implementation creates a
ScheduledMethodRunnable
.- Parameters:
target
- the target bean instancemethod
- the scheduled method to call- Since:
- 5.1
- See Also:
-
getScheduledTasks
Return all currently scheduled tasks, fromScheduled
methods as well as from programmaticSchedulingConfigurer
interaction.Note that this includes upcoming scheduled subscriptions for reactive methods but doesn't cover any currently active subscription for such methods.
- Specified by:
getScheduledTasks
in interfaceScheduledTaskHolder
- Since:
- 5.0.2
-
postProcessBeforeDestruction
Description copied from interface:DestructionAwareBeanPostProcessor
Apply this BeanPostProcessor to the given bean instance before its destruction, e.g. invoking custom destruction callbacks.Like DisposableBean's
destroy
and a custom destroy method, this callback will only apply to beans which the container fully manages the lifecycle for. This is usually the case for singletons and scoped beans.- Specified by:
postProcessBeforeDestruction
in interfaceDestructionAwareBeanPostProcessor
- Parameters:
bean
- the bean instance to be destroyedbeanName
- the name of the bean- See Also:
-
requiresDestruction
Description copied from interface:DestructionAwareBeanPostProcessor
Determine whether the given bean instance requires destruction by this post-processor.The default implementation returns
true
. If a pre-5 implementation ofDestructionAwareBeanPostProcessor
does not provide a concrete implementation of this method, Spring silently assumestrue
as well.- Specified by:
requiresDestruction
in interfaceDestructionAwareBeanPostProcessor
- Parameters:
bean
- the bean instance to check- Returns:
true
ifDestructionAwareBeanPostProcessor.postProcessBeforeDestruction(java.lang.Object, java.lang.String)
is supposed to be called for this bean instance eventually, orfalse
if not needed
-
destroy
public void destroy()Description copied from interface:DisposableBean
Invoked by the containingBeanFactory
on destruction of a bean.- Specified by:
destroy
in interfaceDisposableBean
-