-
Notifications
You must be signed in to change notification settings - Fork 38.3k
/
Copy pathEnableScheduling.java
207 lines (202 loc) · 6.96 KB
/
EnableScheduling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scheduling.annotation;
import java.lang.annotation.Documented;
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, similar to
* functionality found in Spring's {@code <task:*>} XML namespace. To be used
* on {@link Configuration @Configuration} classes as follows:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* public class AppConfig {
*
* // various @Bean definitions
* }</pre>
*
* <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;
*
* public class MyTask {
*
* @Scheduled(fixedRate=1000)
* public void work() {
* // task execution logic
* }
* }</pre>
*
* <p>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>
*
* <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:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* @ComponentScan(basePackages="com.myco.tasks")
* public class AppConfig {
* }</pre>
*
* <p>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>
*
* <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}
* bean. If neither of the two is resolvable, a local single-threaded default
* scheduler will be created and used within the registrar.
*
* <p>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(destroyMethod="shutdown")
* public Executor taskExecutor() {
* return Executors.newScheduledThreadPool(100);
* }
* }</pre>
*
* <p>Note in the example above the use of {@code @Bean(destroyMethod="shutdown")}.
* This ensures that the task executor is properly shut down when the Spring
* application context itself is closed.
*
* <p>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(taskScheduler());
* taskRegistrar.addTriggerTask(
* () -> myTask().work(),
* new CustomTrigger()
* );
* }
*
* @Bean(destroyMethod="shutdown")
* public Executor taskScheduler() {
* return Executors.newScheduledThreadPool(42);
* }
*
* @Bean
* public MyTask myTask() {
* return new MyTask();
* }
* }</pre>
*
* <p>For reference, the example above can be compared to the following Spring XML
* configuration:
*
* <pre class="code">
* <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>
* </pre>
*
* <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 the actual component.
*
* <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, for example, the common root web
* application context and any separate {@code DispatcherServlet} application contexts,
* if you need to apply its behavior at multiple levels.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see Scheduled
* @see SchedulingConfiguration
* @see SchedulingConfigurer
* @see ScheduledTaskRegistrar
* @see Trigger
* @see ScheduledAnnotationBeanPostProcessor
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {
}