Annotation Type EnableScheduling
@Target(TYPE) @Retention(RUNTIME) @Import(SchedulingConfiguration.class) @Documented public @interface EnableScheduling
Enables Spring's scheduled task execution capability, similar to functionality found in Spring's<task:*>XML namespace. To be used on @Configurationclasses as follows:@Configuration @EnableScheduling public class AppConfig { // various @Bean definitions }This enables detection of @Scheduledannotations on any Spring-managed bean in the container. For example, given a classMyTaskpackage com.myco.tasks; public class MyTask { @Scheduled(fixedRate=1000) public void work() { // task execution logic } }the following configuration would ensure thatMyTask.work()is called once every 1000 ms:@Configuration @EnableScheduling public class AppConfig { @Bean public MyTask task() { return new MyTask(); } }Alternatively, ifMyTaskwere annotated with@Component, the following configuration would ensure that its@Scheduledmethod is invoked at the desired interval:@Configuration @EnableScheduling @ComponentScan(basePackages="com.myco.tasks") public class AppConfig { }Methods annotated with@Scheduledmay even be declared directly within@Configurationclasses:@Configuration @EnableScheduling public class AppConfig { @Scheduled(fixedRate=1000) public void work() { // task execution logic } }By default, will be searching for an associated scheduler definition: either a unique
TaskSchedulerbean in the context, or aTaskSchedulerbean named "taskScheduler" otherwise; the same lookup will also be performed for aScheduledExecutorServicebean. If neither of the two is resolvable, a local single-threaded default scheduler will be created and used within the registrar.When more control is desired, a
@Configurationclass may implementSchedulingConfigurer. This allows access to the underlyingScheduledTaskRegistrarinstance. For example, the following example demonstrates how to customize theExecutorused to execute scheduled tasks:@Configuration @EnableScheduling public class AppConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(taskExecutor()); } @Bean(destroyMethod="shutdown") public Executor taskExecutor() { return Executors.newScheduledThreadPool(100); } }Note in the example above the use of
@Bean(destroyMethod="shutdown"). This ensures that the task executor is properly shut down when the Spring application context itself is closed.Implementing
SchedulingConfigureralso allows for fine-grained control over task registration via theScheduledTaskRegistrar. For example, the following configures the execution of a particular bean method per a customTriggerimplementation:@Configuration @EnableScheduling public class AppConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(taskScheduler()); taskRegistrar.addTriggerTask( new Runnable() { public void run() { myTask().work(); } }, new CustomTrigger() ); } @Bean(destroyMethod="shutdown") public Executor taskScheduler() { return Executors.newScheduledThreadPool(42); } @Bean public MyTask myTask() { return new MyTask(); } }For reference, the example above can be compared to the following Spring XML configuration:
The examples are equivalent save that in XML a fixed-rate period is used instead of a custom<beans> <task:annotation-driven scheduler="taskScheduler"/> <task:scheduler id="taskScheduler" pool-size="42"/> <task:scheduled-tasks scheduler="taskScheduler"> <task:scheduled ref="myTask" method="work" fixed-rate="1000"/> </task:scheduled-tasks> <bean id="myTask" class="com.foo.MyTask"/> </beans>Triggerimplementation; this is because thetask:namespacescheduledcannot easily expose such support. This is but one demonstration how the code-based approach allows for maximum configurability through direct access to actual componentry.- Since:
- 3.1
- Author:
- Chris Beams, Juergen Hoeller
- See Also:
Scheduled,SchedulingConfiguration,SchedulingConfigurer,ScheduledTaskRegistrar,Trigger,ScheduledAnnotationBeanPostProcessor