|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
|
* |
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -31,7 +31,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -31,7 +31,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
/** |
|
|
|
|
* 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: |
|
|
|
|
* on {@link Configuration @Configuration} classes as follows: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* @Configuration |
|
|
|
@ -41,8 +41,8 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -41,8 +41,8 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* // 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} |
|
|
|
|
* <p>This enables detection of {@link Scheduled @Scheduled} annotations on any |
|
|
|
|
* Spring-managed bean in the container. For example, given a class {@code MyTask}: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* package com.myco.tasks; |
|
|
|
@ -55,7 +55,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -55,7 +55,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* the following configuration would ensure that {@code MyTask.work()} is called |
|
|
|
|
* <p>the following configuration would ensure that {@code MyTask.work()} is called |
|
|
|
|
* once every 1000 ms: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
@ -69,7 +69,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -69,7 +69,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* Alternatively, if {@code MyTask} were annotated with {@code @Component}, the |
|
|
|
|
* <p>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: |
|
|
|
|
* |
|
|
|
@ -80,7 +80,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -80,7 +80,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* public class AppConfig { |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* Methods annotated with {@code @Scheduled} may even be declared directly within |
|
|
|
|
* <p>Methods annotated with {@code @Scheduled} may even be declared directly within |
|
|
|
|
* {@code @Configuration} classes: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
@ -94,7 +94,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -94,7 +94,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* } |
|
|
|
|
* }</pre> |
|
|
|
|
* |
|
|
|
|
* <p>By default, will be searching for an associated scheduler definition: either |
|
|
|
|
* <p>By default, Spring will search for an associated scheduler definition: either |
|
|
|
|
* a unique {@link org.springframework.scheduling.TaskScheduler} bean in the context, |
|
|
|
|
* or a {@code TaskScheduler} bean named "taskScheduler" otherwise; the same lookup |
|
|
|
|
* will also be performed for a {@link java.util.concurrent.ScheduledExecutorService} |
|
|
|
@ -141,11 +141,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -141,11 +141,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { |
|
|
|
|
* taskRegistrar.setScheduler(taskScheduler()); |
|
|
|
|
* taskRegistrar.addTriggerTask( |
|
|
|
|
* new Runnable() { |
|
|
|
|
* public void run() { |
|
|
|
|
* myTask().work(); |
|
|
|
|
* } |
|
|
|
|
* }, |
|
|
|
|
* () -> myTask().work(), |
|
|
|
|
* new CustomTrigger() |
|
|
|
|
* ); |
|
|
|
|
* } |
|
|
|
@ -165,7 +161,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -165,7 +161,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* configuration: |
|
|
|
|
* |
|
|
|
|
* <pre class="code"> |
|
|
|
|
* <beans> |
|
|
|
|
* <beans> |
|
|
|
|
* |
|
|
|
|
* <task:annotation-driven scheduler="taskScheduler"/> |
|
|
|
|
* |
|
|
|
@ -180,13 +176,13 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@@ -180,13 +176,13 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
|
* </beans> |
|
|
|
|
* </pre> |
|
|
|
|
* |
|
|
|
|
* The examples are equivalent save that in XML a <em>fixed-rate</em> period is used |
|
|
|
|
* <p>The examples are equivalent save that in XML a <em>fixed-rate</em> period is used |
|
|
|
|
* instead of a custom <em>{@code Trigger}</em> implementation; this is because the |
|
|
|
|
* {@code task:} namespace {@code scheduled} cannot easily expose such support. This is |
|
|
|
|
* but one demonstration how the code-based approach allows for maximum configurability |
|
|
|
|
* through direct access to actual componentry.<p> |
|
|
|
|
* through direct access to actual componentry. |
|
|
|
|
* |
|
|
|
|
* <b>Note: {@code @EnableScheduling} applies to its local application context only, |
|
|
|
|
* <p><b>Note: {@code @EnableScheduling} applies to its local application context only, |
|
|
|
|
* allowing for selective scheduling of beans at different levels.</b> Please redeclare |
|
|
|
|
* {@code @EnableScheduling} in each individual context, e.g. the common root web |
|
|
|
|
* application context and any separate {@code DispatcherServlet} application contexts, |
|
|
|
|