Class ScheduledAnnotationBeanPostProcessor

java.lang.Object
org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor
All Implemented Interfaces:
EventListener, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.SmartInitializingSingleton, org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, ApplicationContextAware, ApplicationListener<ApplicationContextEvent>, EmbeddedValueResolverAware, org.springframework.core.Ordered, ScheduledTaskHolder

public class ScheduledAnnotationBeanPostProcessor extends Object implements ScheduledTaskHolder, org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor, org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor, org.springframework.core.Ordered, EmbeddedValueResolverAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.BeanFactoryAware, ApplicationContextAware, org.springframework.beans.factory.SmartInitializingSingleton, org.springframework.beans.factory.DisposableBean, ApplicationListener<ApplicationContextEvent>
Bean post-processor that registers methods annotated with @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 Details

    • DEFAULT_TASK_SCHEDULER_BEAN_NAME

      public static final String DEFAULT_TASK_SCHEDULER_BEAN_NAME
      The default name of the TaskScheduler 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

      protected final org.apache.commons.logging.Log logger
  • Constructor Details

    • ScheduledAnnotationBeanPostProcessor

      public ScheduledAnnotationBeanPostProcessor()
      Create a default ScheduledAnnotationBeanPostProcessor.
    • ScheduledAnnotationBeanPostProcessor

      public ScheduledAnnotationBeanPostProcessor(ScheduledTaskRegistrar registrar)
      Create a ScheduledAnnotationBeanPostProcessor delegating to the specified ScheduledTaskRegistrar.
      Parameters:
      registrar - the ScheduledTaskRegistrar to register @Scheduled tasks on
      Since:
      5.1
  • Method Details

    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
    • setScheduler

      public void setScheduler(Object scheduler)
      Set the TaskScheduler that will invoke the scheduled methods, or a ScheduledExecutorService 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 a TaskScheduler bean named "taskScheduler" otherwise; the same lookup will also be performed for a ScheduledExecutorService bean. If neither of the two is resolvable, a local single-threaded default scheduler will be created within the registrar.

      See Also:
    • setEmbeddedValueResolver

      public void setEmbeddedValueResolver(org.springframework.util.StringValueResolver resolver)
      Description copied from interface: EmbeddedValueResolverAware
      Set the StringValueResolver to use for resolving embedded definition values.
      Specified by:
      setEmbeddedValueResolver in interface EmbeddedValueResolverAware
    • setBeanName

      public void setBeanName(String beanName)
      Specified by:
      setBeanName in interface org.springframework.beans.factory.BeanNameAware
    • setBeanFactory

      public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
      Making a BeanFactory available is optional; if not set, SchedulingConfigurer beans won't get autodetected and a scheduler has to be explicitly configured.
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
    • setApplicationContext

      public void setApplicationContext(ApplicationContext applicationContext)
      Setting an ApplicationContext is optional: If set, registered tasks will be activated in the ContextRefreshedEvent phase; if not set, it will happen at afterSingletonsInstantiated() time.
      Specified by:
      setApplicationContext in interface ApplicationContextAware
      Parameters:
      applicationContext - the ApplicationContext object to be used by this object
      See Also:
      • BeanInitializationException
    • afterSingletonsInstantiated

      public void afterSingletonsInstantiated()
      Specified by:
      afterSingletonsInstantiated in interface org.springframework.beans.factory.SmartInitializingSingleton
    • postProcessMergedBeanDefinition

      public void postProcessMergedBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition beanDefinition, Class<?> beanType, String beanName)
      Specified by:
      postProcessMergedBeanDefinition in interface org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor
    • postProcessBeforeInitialization

      public Object postProcessBeforeInitialization(Object bean, String beanName)
      Specified by:
      postProcessBeforeInitialization in interface org.springframework.beans.factory.config.BeanPostProcessor
    • postProcessAfterInitialization

      public Object postProcessAfterInitialization(Object bean, String beanName)
      Specified by:
      postProcessAfterInitialization in interface org.springframework.beans.factory.config.BeanPostProcessor
    • processScheduled

      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.
      Parameters:
      scheduled - the @Scheduled annotation
      method - the method that the annotation has been declared on
      bean - the target bean instance
      See Also:
      • processScheduledSync(Scheduled, Method, Object)
      • processScheduledAsync(Scheduled, Method, Object)
    • createRunnable

      protected Runnable createRunnable(Object target, Method method, @Nullable String qualifier)
      Create a Runnable for the given bean instance, calling the specified scheduled method.

      The default implementation creates a ScheduledMethodRunnable.

      Parameters:
      target - the target bean instance
      method - the scheduled method to call
      Since:
      6.1
    • createRunnable

      @Deprecated(since="6.1") @Nullable protected Runnable createRunnable(Object target, Method method)
      Create a Runnable for the given bean instance, calling the specified scheduled method.
      Parameters:
      target - the target bean instance
      method - the scheduled method to call
      Since:
      5.1
    • getScheduledTasks

      public Set<ScheduledTask> getScheduledTasks()
      Return all currently scheduled tasks, from Scheduled methods as well as from programmatic SchedulingConfigurer 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 interface ScheduledTaskHolder
      Since:
      5.0.2
    • postProcessBeforeDestruction

      public void postProcessBeforeDestruction(Object bean, String beanName)
      Specified by:
      postProcessBeforeDestruction in interface org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor
    • requiresDestruction

      public boolean requiresDestruction(Object bean)
      Specified by:
      requiresDestruction in interface org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • onApplicationEvent

      public void onApplicationEvent(ApplicationContextEvent event)
      Reacts to ContextRefreshedEvent as well as ContextClosedEvent: performing finishRegistration() and early cancelling of scheduled tasks, respectively.
      Specified by:
      onApplicationEvent in interface ApplicationListener<ApplicationContextEvent>
      Parameters:
      event - the event to respond to