11/* 
2-  * Copyright 2002-2011  the original author or authors. 
2+  * Copyright 2002-2012  the original author or authors. 
33 * 
44 * Licensed under the Apache License, Version 2.0 (the "License"); 
55 * you may not use this file except in compliance with the License. 
1717package  org .springframework .scheduling .annotation ;
1818
1919import  java .lang .reflect .Method ;
20+ 
2021import  java .util .HashMap ;
2122import  java .util .Map ;
2223import  java .util .concurrent .ScheduledExecutorService ;
3334import  org .springframework .core .annotation .AnnotationUtils ;
3435import  org .springframework .scheduling .TaskScheduler ;
3536import  org .springframework .scheduling .Trigger ;
37+ import  org .springframework .scheduling .config .CronTask ;
38+ import  org .springframework .scheduling .config .IntervalTask ;
3639import  org .springframework .scheduling .config .ScheduledTaskRegistrar ;
3740import  org .springframework .scheduling .support .ScheduledMethodRunnable ;
3841import  org .springframework .util .Assert ;
@@ -74,13 +77,7 @@ public class ScheduledAnnotationBeanPostProcessor
7477
7578	private  ApplicationContext  applicationContext ;
7679
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 ();
8481
8582
8683	/** 
@@ -104,7 +101,6 @@ public int getOrder() {
104101		return  LOWEST_PRECEDENCE ;
105102	}
106103
107- 
108104	public  Object  postProcessBeforeInitialization (Object  bean , String  beanName ) {
109105		return  bean ;
110106	}
@@ -124,9 +120,11 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
124120							// found a @Scheduled method on the target class for this JDK proxy -> is it 
125121							// also present on the proxy itself? 
126122							method  = bean .getClass ().getMethod (method .getName (), method .getParameterTypes ());
127- 						} catch  (SecurityException  ex ) {
123+ 						}
124+ 						catch  (SecurityException  ex ) {
128125							ReflectionUtils .handleReflectionException (ex );
129- 						} catch  (NoSuchMethodException  ex ) {
126+ 						}
127+ 						catch  (NoSuchMethodException  ex ) {
130128							throw  new  IllegalStateException (String .format (
131129									"@Scheduled method '%s' found on bean target class '%s', "  +
132130									"but not found in any interface(s) for bean JDK proxy. Either "  +
@@ -137,26 +135,27 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
137135					}
138136					Runnable  runnable  = new  ScheduledMethodRunnable (bean , method );
139137					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." ;
141139					String  cron  = annotation .cron ();
142140					if  (!"" .equals (cron )) {
143141						processedSchedule  = true ;
144142						if  (embeddedValueResolver  != null ) {
145143							cron  = embeddedValueResolver .resolveStringValue (cron );
146144						}
147- 						cronTasks . put ( runnable , cron );
145+ 						registrar . addCronTask ( new   CronTask ( runnable , cron ) );
148146					}
147+ 					long  initialDelay  = annotation .initialDelay ();
149148					long  fixedDelay  = annotation .fixedDelay ();
150149					if  (fixedDelay  >= 0 ) {
151150						Assert .isTrue (!processedSchedule , errorMessage );
152151						processedSchedule  = true ;
153- 						fixedDelayTasks . put ( runnable , fixedDelay );
152+ 						registrar . addFixedDelayTask ( new   IntervalTask ( runnable , fixedDelay ,  initialDelay ) );
154153					}
155154					long  fixedRate  = annotation .fixedRate ();
156155					if  (fixedRate  >= 0 ) {
157156						Assert .isTrue (!processedSchedule , errorMessage );
158157						processedSchedule  = true ;
159- 						fixedRateTasks . put ( runnable , fixedRate );
158+ 						registrar . addFixedRateTask ( new   IntervalTask ( runnable , fixedRate ,  initialDelay ) );
160159					}
161160					Assert .isTrue (processedSchedule , errorMessage );
162161				}
@@ -170,17 +169,8 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
170169			return ;
171170		}
172171
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 );
184174
185175		if  (this .scheduler  != null ) {
186176			this .registrar .setScheduler (this .scheduler );
@@ -190,19 +180,23 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
190180			configurer .configureTasks (this .registrar );
191181		}
192182
193- 		if  (registrar .getScheduler () == null ) {
183+ 		if  (this . registrar . hasTasks () &&  this . registrar .getScheduler () == null ) {
194184			Map <String , ? super  Object > schedulers  = new  HashMap <String , Object >();
195185			schedulers .putAll (applicationContext .getBeansOfType (TaskScheduler .class ));
196186			schedulers .putAll (applicationContext .getBeansOfType (ScheduledExecutorService .class ));
197187			if  (schedulers .size () == 0 ) {
198188				// do nothing -> fall back to default scheduler 
199- 			} else  if  (schedulers .size () == 1 ) {
189+ 			}
190+ 			else  if  (schedulers .size () == 1 ) {
200191				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 ());
206200			}
207201		}
208202
0 commit comments