|
|
|
|
@ -21,14 +21,136 @@ import java.lang.annotation.ElementType;
@@ -21,14 +21,136 @@ import java.lang.annotation.ElementType;
|
|
|
|
|
import java.lang.annotation.Retention; |
|
|
|
|
import java.lang.annotation.RetentionPolicy; |
|
|
|
|
import java.lang.annotation.Target; |
|
|
|
|
import java.util.concurrent.Executor; |
|
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.context.annotation.Import; |
|
|
|
|
import org.springframework.scheduling.Trigger; |
|
|
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Enables Spring's scheduled task execution capability. |
|
|
|
|
* Enables Spring's scheduled task execution capability, similar to |
|
|
|
|
* functionality found in Spring's {@code <task:*>} XML namespace. To be used |
|
|
|
|
* on @{@link Configuration} classes as follows: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
|
* @EnableScheduling |
|
|
|
|
* public class AppConfig { |
|
|
|
|
* // various @Bean definitions
|
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* This enables detection of @{@link Scheduled} annotations on any Spring-managed |
|
|
|
|
* bean in the container. For example, given a class {@code MyTask} |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* package com.myco.tasks; |
|
|
|
|
* |
|
|
|
|
* public class MyTask { |
|
|
|
|
* @Scheduled(fixedRate=1000) |
|
|
|
|
* public void work() { |
|
|
|
|
* // task execution logic
|
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* the following configuration would ensure that {@code MyTask.work()} is called |
|
|
|
|
* once every 1000 ms: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
|
* @EnableScheduling |
|
|
|
|
* public class AppConfig { |
|
|
|
|
* @Bean |
|
|
|
|
* public MyTask task() { |
|
|
|
|
* return new MyTask(); |
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* Alternatively, if {@code MyTask} were annotated with {@code @Component}, the |
|
|
|
|
* following configuration would ensure that its {@code @Scheduled} method is |
|
|
|
|
* invoked at the desired interval: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
|
* @ComponentScan(basePackages="com.myco.tasks") |
|
|
|
|
* public class AppConfig { |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* Methods annotated with {@code @Scheduled} may even be declared directly within |
|
|
|
|
* {@code @Configuration} classes: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
|
* @EnableScheduling |
|
|
|
|
* public class AppConfig { |
|
|
|
|
* @Scheduled(fixedRate=1000) |
|
|
|
|
* public void work() { |
|
|
|
|
* // task execution logic
|
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* In all of the above scenarios, a default single-threaded task executor is used. |
|
|
|
|
* When more control is desired, a {@code @Configuration} class may implement |
|
|
|
|
* {@link SchedulingConfigurer}. This allows access to the underlying |
|
|
|
|
* {@link ScheduledTaskRegistrar} instance. For example, the following example |
|
|
|
|
* demonstrates how to customize the {@link Executor} used to execute scheduled |
|
|
|
|
* tasks: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
|
* @EnableScheduling |
|
|
|
|
* public class AppConfig implements SchedulingConfigurer { |
|
|
|
|
* @Override |
|
|
|
|
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { |
|
|
|
|
* taskRegistrar.setScheduler(taskExecutor()); |
|
|
|
|
* } |
|
|
|
|
* |
|
|
|
|
* @Bean |
|
|
|
|
* public Executor taskExecutor() { |
|
|
|
|
* return Executors.newScheduledThreadPool(100); |
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* Implementing {@code SchedulingConfigurer} also allows for fine-grained |
|
|
|
|
* control over task registration via the {@code ScheduledTaskRegistrar}. |
|
|
|
|
* For example, the following configures the execution of a particular bean |
|
|
|
|
* method per a custom {@code Trigger} implementation: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
|
* @EnableScheduling |
|
|
|
|
* public class AppConfig implements SchedulingConfigurer { |
|
|
|
|
* @Override |
|
|
|
|
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { |
|
|
|
|
* taskRegistrar.setScheduler(taskExecutor()); |
|
|
|
|
* taskRegistrar.addTriggerTask( |
|
|
|
|
* addTriggerTask( |
|
|
|
|
* new Runnable() { |
|
|
|
|
* task().work(); |
|
|
|
|
* }, |
|
|
|
|
* new CustomTrigger() |
|
|
|
|
* ); |
|
|
|
|
* } |
|
|
|
|
* |
|
|
|
|
* @Bean |
|
|
|
|
* public Executor taskExecutor() { |
|
|
|
|
* return Executors.newScheduledThreadPool(100); |
|
|
|
|
* } |
|
|
|
|
* |
|
|
|
|
* @Bean |
|
|
|
|
* public MyTask task() { |
|
|
|
|
* return new MyTask(); |
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* @author Chris Beams |
|
|
|
|
* @since 3.1 |
|
|
|
|
* @see Scheduled |
|
|
|
|
* @see SchedulingConfiguration |
|
|
|
|
* @see SchedulingConfigurer |
|
|
|
|
* @see ScheduledTaskRegistrar |
|
|
|
|
* @see Trigger |
|
|
|
|
* @see ScheduledAnnotationBeanPostProcessor |
|
|
|
|
*/ |
|
|
|
|
@Target(ElementType.TYPE) |
|
|
|
|
@Retention(RetentionPolicy.RUNTIME) |
|
|
|
|
|