1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 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.
17
17
package org .springframework .scheduling .annotation ;
18
18
19
19
import java .lang .reflect .Method ;
20
+
20
21
import java .util .HashMap ;
21
22
import java .util .Map ;
22
23
import java .util .concurrent .ScheduledExecutorService ;
33
34
import org .springframework .core .annotation .AnnotationUtils ;
34
35
import org .springframework .scheduling .TaskScheduler ;
35
36
import org .springframework .scheduling .Trigger ;
37
+ import org .springframework .scheduling .config .CronTask ;
38
+ import org .springframework .scheduling .config .IntervalTask ;
36
39
import org .springframework .scheduling .config .ScheduledTaskRegistrar ;
37
40
import org .springframework .scheduling .support .ScheduledMethodRunnable ;
38
41
import org .springframework .util .Assert ;
@@ -74,13 +77,7 @@ public class ScheduledAnnotationBeanPostProcessor
74
77
75
78
private ApplicationContext applicationContext ;
76
79
77
- private ScheduledTaskRegistrar registrar ;
78
-
79
- private final Map <Runnable , String > cronTasks = new HashMap <Runnable , String >();
80
-
81
- private final Map <Runnable , Long > fixedDelayTasks = new HashMap <Runnable , Long >();
82
-
83
- private final Map <Runnable , Long > fixedRateTasks = new HashMap <Runnable , Long >();
80
+ private final ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar ();
84
81
85
82
86
83
/**
@@ -104,7 +101,6 @@ public int getOrder() {
104
101
return LOWEST_PRECEDENCE ;
105
102
}
106
103
107
-
108
104
public Object postProcessBeforeInitialization (Object bean , String beanName ) {
109
105
return bean ;
110
106
}
@@ -124,9 +120,11 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
124
120
// found a @Scheduled method on the target class for this JDK proxy -> is it
125
121
// also present on the proxy itself?
126
122
method = bean .getClass ().getMethod (method .getName (), method .getParameterTypes ());
127
- } catch (SecurityException ex ) {
123
+ }
124
+ catch (SecurityException ex ) {
128
125
ReflectionUtils .handleReflectionException (ex );
129
- } catch (NoSuchMethodException ex ) {
126
+ }
127
+ catch (NoSuchMethodException ex ) {
130
128
throw new IllegalStateException (String .format (
131
129
"@Scheduled method '%s' found on bean target class '%s', " +
132
130
"but not found in any interface(s) for bean JDK proxy. Either " +
@@ -137,26 +135,27 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
137
135
}
138
136
Runnable runnable = new ScheduledMethodRunnable (bean , method );
139
137
boolean processedSchedule = false ;
140
- String errorMessage = "Exactly one of 'cron', 'fixedDelay', or 'fixedRate' is required." ;
138
+ String errorMessage = "Exactly one of the 'cron', 'fixedDelay', or 'fixedRate' attributes is required." ;
141
139
String cron = annotation .cron ();
142
140
if (!"" .equals (cron )) {
143
141
processedSchedule = true ;
144
142
if (embeddedValueResolver != null ) {
145
143
cron = embeddedValueResolver .resolveStringValue (cron );
146
144
}
147
- cronTasks . put ( runnable , cron );
145
+ registrar . addCronTask ( new CronTask ( runnable , cron ) );
148
146
}
147
+ long initialDelay = annotation .initialDelay ();
149
148
long fixedDelay = annotation .fixedDelay ();
150
149
if (fixedDelay >= 0 ) {
151
150
Assert .isTrue (!processedSchedule , errorMessage );
152
151
processedSchedule = true ;
153
- fixedDelayTasks . put ( runnable , fixedDelay );
152
+ registrar . addFixedDelayTask ( new IntervalTask ( runnable , fixedDelay , initialDelay ) );
154
153
}
155
154
long fixedRate = annotation .fixedRate ();
156
155
if (fixedRate >= 0 ) {
157
156
Assert .isTrue (!processedSchedule , errorMessage );
158
157
processedSchedule = true ;
159
- fixedRateTasks . put ( runnable , fixedRate );
158
+ registrar . addFixedRateTask ( new IntervalTask ( runnable , fixedRate , initialDelay ) );
160
159
}
161
160
Assert .isTrue (processedSchedule , errorMessage );
162
161
}
@@ -170,17 +169,8 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
170
169
return ;
171
170
}
172
171
173
- Map <String , SchedulingConfigurer > configurers = applicationContext .getBeansOfType (SchedulingConfigurer .class );
174
-
175
- if (this .cronTasks .isEmpty () && this .fixedDelayTasks .isEmpty () &&
176
- this .fixedRateTasks .isEmpty () && configurers .isEmpty ()) {
177
- return ;
178
- }
179
-
180
- this .registrar = new ScheduledTaskRegistrar ();
181
- this .registrar .setCronTasks (this .cronTasks );
182
- this .registrar .setFixedDelayTasks (this .fixedDelayTasks );
183
- this .registrar .setFixedRateTasks (this .fixedRateTasks );
172
+ Map <String , SchedulingConfigurer > configurers =
173
+ this .applicationContext .getBeansOfType (SchedulingConfigurer .class );
184
174
185
175
if (this .scheduler != null ) {
186
176
this .registrar .setScheduler (this .scheduler );
@@ -190,19 +180,23 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
190
180
configurer .configureTasks (this .registrar );
191
181
}
192
182
193
- if (registrar .getScheduler () == null ) {
183
+ if (this . registrar . hasTasks () && this . registrar .getScheduler () == null ) {
194
184
Map <String , ? super Object > schedulers = new HashMap <String , Object >();
195
185
schedulers .putAll (applicationContext .getBeansOfType (TaskScheduler .class ));
196
186
schedulers .putAll (applicationContext .getBeansOfType (ScheduledExecutorService .class ));
197
187
if (schedulers .size () == 0 ) {
198
188
// do nothing -> fall back to default scheduler
199
- } else if (schedulers .size () == 1 ) {
189
+ }
190
+ else if (schedulers .size () == 1 ) {
200
191
this .registrar .setScheduler (schedulers .values ().iterator ().next ());
201
- } else if (schedulers .size () >= 2 ){
202
- throw new IllegalStateException ("More than one TaskScheduler and/or ScheduledExecutorService " +
203
- "exist within the context. Remove all but one of the beans; or implement the " +
204
- "SchedulingConfigurer interface and call ScheduledTaskRegistrar#setScheduler " +
205
- "explicitly within the configureTasks() callback. Found the following beans: " + schedulers .keySet ());
192
+ }
193
+ else if (schedulers .size () >= 2 ){
194
+ throw new IllegalStateException (
195
+ "More than one TaskScheduler and/or ScheduledExecutorService " +
196
+ "exist within the context. Remove all but one of the beans; or " +
197
+ "implement the SchedulingConfigurer interface and call " +
198
+ "ScheduledTaskRegistrar#setScheduler explicitly within the " +
199
+ "configureTasks() callback. Found the following beans: " + schedulers .keySet ());
206
200
}
207
201
}
208
202
0 commit comments