|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
37 | 37 | * @Configuration
|
38 | 38 | * @EnableScheduling
|
39 | 39 | * public class AppConfig {
|
| 40 | + * |
40 | 41 | * // various @Bean definitions
|
41 | 42 | * }</pre>
|
42 | 43 | *
|
43 | 44 | * This enables detection of @{@link Scheduled} annotations on any Spring-managed
|
44 |
| - * bean in the container. For example, given a class {@code MyTask} |
| 45 | + * bean in the container. For example, given a class {@code MyTask} |
45 | 46 | *
|
46 | 47 | * <pre class="code">
|
47 | 48 | * package com.myco.tasks;
|
48 | 49 | *
|
49 | 50 | * public class MyTask {
|
| 51 | + * |
50 | 52 | * @Scheduled(fixedRate=1000)
|
51 | 53 | * public void work() {
|
52 | 54 | * // task execution logic
|
|
60 | 62 | * @Configuration
|
61 | 63 | * @EnableScheduling
|
62 | 64 | * public class AppConfig {
|
| 65 | + * |
63 | 66 | * @Bean
|
64 | 67 | * public MyTask task() {
|
65 | 68 | * return new MyTask();
|
|
72 | 75 | *
|
73 | 76 | * <pre class="code">
|
74 | 77 | * @Configuration
|
| 78 | + * @EnableScheduling |
75 | 79 | * @ComponentScan(basePackages="com.myco.tasks")
|
76 | 80 | * public class AppConfig {
|
77 | 81 | * }</pre>
|
|
83 | 87 | * @Configuration
|
84 | 88 | * @EnableScheduling
|
85 | 89 | * public class AppConfig {
|
| 90 | + * |
86 | 91 | * @Scheduled(fixedRate=1000)
|
87 | 92 | * public void work() {
|
88 | 93 | * // task execution logic
|
|
100 | 105 | * @Configuration
|
101 | 106 | * @EnableScheduling
|
102 | 107 | * public class AppConfig implements SchedulingConfigurer {
|
| 108 | + * |
103 | 109 | * @Override
|
104 | 110 | * public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
105 | 111 | * taskRegistrar.setScheduler(taskExecutor());
|
|
111 | 117 | * }
|
112 | 118 | * }</pre>
|
113 | 119 | *
|
114 |
| - * Note in the example above the use of {@code @Bean(destroyMethod="shutdown")}. This |
115 |
| - * ensures that the task executor is properly shut down when the Spring application |
116 |
| - * context itself is closed. |
| 120 | + * <p>Note in the example above the use of {@code @Bean(destroyMethod="shutdown")}. |
| 121 | + * This ensures that the task executor is properly shut down when the Spring |
| 122 | + * application context itself is closed. |
117 | 123 | *
|
118 |
| - * Implementing {@code SchedulingConfigurer} also allows for fine-grained |
| 124 | + * <p>Implementing {@code SchedulingConfigurer} also allows for fine-grained |
119 | 125 | * control over task registration via the {@code ScheduledTaskRegistrar}.
|
120 | 126 | * For example, the following configures the execution of a particular bean
|
121 | 127 | * method per a custom {@code Trigger} implementation:
|
|
124 | 130 | * @Configuration
|
125 | 131 | * @EnableScheduling
|
126 | 132 | * public class AppConfig implements SchedulingConfigurer {
|
| 133 | + * |
127 | 134 | * @Override
|
128 | 135 | * public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
129 | 136 | * taskRegistrar.setScheduler(taskScheduler());
|
|
150 | 157 | *
|
151 | 158 | * <p>For reference, the example above can be compared to the following Spring XML
|
152 | 159 | * configuration:
|
| 160 | + * |
153 | 161 | * <pre class="code">
|
154 | 162 | * {@code
|
155 | 163 | * <beans>
|
| 164 | + * |
156 | 165 | * <task:annotation-driven scheduler="taskScheduler"/>
|
| 166 | + * |
157 | 167 | * <task:scheduler id="taskScheduler" pool-size="42"/>
|
158 |
| - * <task:scheduled ref="myTask" method="work" fixed-rate="1000"/> |
| 168 | + * |
| 169 | + * <task:scheduled-tasks scheduler="taskScheduler"> |
| 170 | + * <task:scheduled ref="myTask" method="work" fixed-rate="1000"/> |
| 171 | + * </task:scheduled-tasks> |
| 172 | + * |
159 | 173 | * <bean id="myTask" class="com.foo.MyTask"/>
|
| 174 | + * |
160 | 175 | * </beans>
|
161 | 176 | * }</pre>
|
162 |
| - * the examples are equivalent save that in XML a <em>fixed-rate</em> period is used |
| 177 | + * |
| 178 | + * The examples are equivalent save that in XML a <em>fixed-rate</em> period is used |
163 | 179 | * instead of a custom <em>{@code Trigger}</em> implementation; this is because the
|
164 | 180 | * {@code task:} namespace {@code scheduled} cannot easily expose such support. This is
|
165 | 181 | * but one demonstration how the code-based approach allows for maximum configurability
|
166 | 182 | * through direct access to actual componentry.<p>
|
167 | 183 | *
|
168 | 184 | * @author Chris Beams
|
| 185 | + * @author Juergen Hoeller |
169 | 186 | * @since 3.1
|
170 | 187 | * @see Scheduled
|
171 | 188 | * @see SchedulingConfiguration
|
|
0 commit comments