Skip to content

Commit 1284086

Browse files
committed
SPR-6669 @scheduled may now be used as a meta-annotation
1 parent 543515e commit 1284086

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

org.springframework.context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -35,7 +35,7 @@
3535
* @since 3.0
3636
* @see ScheduledAnnotationBeanPostProcessor
3737
*/
38-
@Target(ElementType.METHOD)
38+
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
3939
@Retention(RetentionPolicy.RUNTIME)
4040
@Documented
4141
public @interface Scheduled {

org.springframework.context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -18,6 +18,10 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020

21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
2125
import java.util.Map;
2226

2327
import org.junit.Test;
@@ -33,7 +37,7 @@
3337
/**
3438
* @author Mark Fisher
3539
*/
36-
@SuppressWarnings("unchecked")
40+
@SuppressWarnings({"unchecked", "unused"})
3741
public class ScheduledAnnotationBeanPostProcessorTests {
3842

3943
@Test
@@ -97,7 +101,7 @@ public void cronTask() {
97101
Object target = context.getBean("target");
98102
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
99103
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
100-
Map<Runnable, Long> cronTasks = (Map<Runnable, Long>)
104+
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
101105
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
102106
assertEquals(1, cronTasks.size());
103107
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
@@ -108,6 +112,54 @@ public void cronTask() {
108112
assertEquals("*/7 * * * * ?", cronTasks.values().iterator().next());
109113
}
110114

115+
@Test
116+
public void metaAnnotationWithFixedRate() {
117+
StaticApplicationContext context = new StaticApplicationContext();
118+
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
119+
BeanDefinition targetDefinition = new RootBeanDefinition(
120+
ScheduledAnnotationBeanPostProcessorTests.MetaAnnotationFixedRateTestBean.class);
121+
context.registerBeanDefinition("postProcessor", processorDefinition);
122+
context.registerBeanDefinition("target", targetDefinition);
123+
context.refresh();
124+
Object postProcessor = context.getBean("postProcessor");
125+
Object target = context.getBean("target");
126+
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
127+
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
128+
Map<Runnable, Long> fixedRateTasks = (Map<Runnable, Long>)
129+
new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
130+
assertEquals(1, fixedRateTasks.size());
131+
MethodInvokingRunnable runnable = (MethodInvokingRunnable) fixedRateTasks.keySet().iterator().next();
132+
Object targetObject = runnable.getTargetObject();
133+
String targetMethod = runnable.getTargetMethod();
134+
assertEquals(target, targetObject);
135+
assertEquals("checkForUpdates", targetMethod);
136+
assertEquals(new Long(5000), fixedRateTasks.values().iterator().next());
137+
}
138+
139+
@Test
140+
public void metaAnnotationWithCronExpression() {
141+
StaticApplicationContext context = new StaticApplicationContext();
142+
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
143+
BeanDefinition targetDefinition = new RootBeanDefinition(
144+
ScheduledAnnotationBeanPostProcessorTests.MetaAnnotationCronTestBean.class);
145+
context.registerBeanDefinition("postProcessor", processorDefinition);
146+
context.registerBeanDefinition("target", targetDefinition);
147+
context.refresh();
148+
Object postProcessor = context.getBean("postProcessor");
149+
Object target = context.getBean("target");
150+
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
151+
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
152+
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
153+
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
154+
assertEquals(1, cronTasks.size());
155+
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
156+
Object targetObject = runnable.getTargetObject();
157+
String targetMethod = runnable.getTargetMethod();
158+
assertEquals(target, targetObject);
159+
assertEquals("generateReport", targetMethod);
160+
assertEquals("0 0 * * * ?", cronTasks.values().iterator().next());
161+
}
162+
111163
@Test(expected = BeanCreationException.class)
112164
public void emptyAnnotation() {
113165
StaticApplicationContext context = new StaticApplicationContext();
@@ -216,4 +268,32 @@ public void invalid(String oops) {
216268

217269
}
218270

271+
272+
@Scheduled(fixedRate = 5000)
273+
@Target(ElementType.METHOD)
274+
@Retention(RetentionPolicy.RUNTIME)
275+
private static @interface EveryFiveSeconds {}
276+
277+
278+
@Scheduled(cron = "0 0 * * * ?")
279+
@Target(ElementType.METHOD)
280+
@Retention(RetentionPolicy.RUNTIME)
281+
private static @interface Hourly {}
282+
283+
284+
private static class MetaAnnotationFixedRateTestBean {
285+
286+
@EveryFiveSeconds
287+
public void checkForUpdates() {
288+
}
289+
}
290+
291+
292+
private static class MetaAnnotationCronTestBean {
293+
294+
@Hourly
295+
public void generateReport() {
296+
}
297+
}
298+
219299
}

0 commit comments

Comments
 (0)