39
39
import org .springframework .beans .factory .config .PropertyPlaceholderConfigurer ;
40
40
import org .springframework .beans .factory .support .RootBeanDefinition ;
41
41
import org .springframework .context .support .StaticApplicationContext ;
42
+ import org .springframework .core .annotation .AliasFor ;
42
43
import org .springframework .scheduling .Trigger ;
43
44
import org .springframework .scheduling .TriggerContext ;
44
45
import org .springframework .scheduling .config .CronTask ;
@@ -332,6 +333,32 @@ public void metaAnnotationWithFixedRate() {
332
333
assertEquals (5000L , task .getInterval ());
333
334
}
334
335
336
+ @ Test
337
+ public void composedAnnotationWithInitialDelayAndFixedRate () {
338
+ BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
339
+ BeanDefinition targetDefinition = new RootBeanDefinition (ComposedAnnotationFixedRateTestBean .class );
340
+ context .registerBeanDefinition ("postProcessor" , processorDefinition );
341
+ context .registerBeanDefinition ("target" , targetDefinition );
342
+ context .refresh ();
343
+
344
+ Object postProcessor = context .getBean ("postProcessor" );
345
+ Object target = context .getBean ("target" );
346
+ ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar ) new DirectFieldAccessor (
347
+ postProcessor ).getPropertyValue ("registrar" );
348
+ @ SuppressWarnings ("unchecked" )
349
+ List <IntervalTask > fixedRateTasks = (List <IntervalTask >) new DirectFieldAccessor (registrar ).getPropertyValue (
350
+ "fixedRateTasks" );
351
+ assertEquals (1 , fixedRateTasks .size ());
352
+ IntervalTask task = fixedRateTasks .get (0 );
353
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) task .getRunnable ();
354
+ Object targetObject = runnable .getTarget ();
355
+ Method targetMethod = runnable .getMethod ();
356
+ assertEquals (target , targetObject );
357
+ assertEquals ("checkForUpdates" , targetMethod .getName ());
358
+ assertEquals (5000L , task .getInterval ());
359
+ assertEquals (1000L , task .getInitialDelay ());
360
+ }
361
+
335
362
@ Test
336
363
public void metaAnnotationWithCronExpression () {
337
364
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
@@ -604,7 +631,7 @@ static class FixedRatesSubBean extends FixedRatesBaseBean {
604
631
}
605
632
606
633
607
- static interface FixedRatesDefaultMethod {
634
+ interface FixedRatesDefaultMethod {
608
635
609
636
@ Scheduled (fixedRate =4000 )
610
637
@ Scheduled (fixedRate =4000 , initialDelay =2000 )
@@ -681,14 +708,25 @@ public void invalid(String oops) {
681
708
@ Scheduled (fixedRate =5000 )
682
709
@ Target (ElementType .METHOD )
683
710
@ Retention (RetentionPolicy .RUNTIME )
684
- private static @interface EveryFiveSeconds {}
685
-
711
+ private @interface EveryFiveSeconds {
712
+ }
686
713
687
714
@ Scheduled (cron ="0 0 * * * ?" )
688
715
@ Target (ElementType .METHOD )
689
716
@ Retention (RetentionPolicy .RUNTIME )
690
- private static @interface Hourly {}
717
+ private @interface Hourly {
718
+ }
719
+
720
+ @ Scheduled (initialDelay = 1000 )
721
+ @ Retention (RetentionPolicy .RUNTIME )
722
+ private @interface WaitASec {
723
+
724
+ @ AliasFor (annotation = Scheduled .class )
725
+ long fixedDelay () default -1 ;
691
726
727
+ @ AliasFor (annotation = Scheduled .class )
728
+ long fixedRate () default -1 ;
729
+ }
692
730
693
731
static class MetaAnnotationFixedRateTestBean {
694
732
@@ -697,6 +735,12 @@ public void checkForUpdates() {
697
735
}
698
736
}
699
737
738
+ static class ComposedAnnotationFixedRateTestBean {
739
+
740
+ @ WaitASec (fixedRate = 5000 )
741
+ public void checkForUpdates () {
742
+ }
743
+ }
700
744
701
745
static class MetaAnnotationCronTestBean {
702
746
@@ -705,7 +749,6 @@ public void generateReport() {
705
749
}
706
750
}
707
751
708
-
709
752
static class PropertyPlaceholderWithCronTestBean {
710
753
711
754
@ Scheduled (cron = "${schedules.businessHours}" )
@@ -741,7 +784,7 @@ public void x() {
741
784
@ Scheduled (cron ="${schedules.businessHours}" )
742
785
@ Target (ElementType .METHOD )
743
786
@ Retention (RetentionPolicy .RUNTIME )
744
- private static @interface BusinessHours {
787
+ private @interface BusinessHours {
745
788
}
746
789
747
790
0 commit comments