SPR-6669 @Scheduled may now be used as a meta-annotation

This commit is contained in:
Mark Fisher 2010-01-11 18:36:48 +00:00
parent 543515e36c
commit 1284086ffa
2 changed files with 85 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,7 +35,7 @@ import java.lang.annotation.Target;
* @since 3.0 * @since 3.0
* @see ScheduledAnnotationBeanPostProcessor * @see ScheduledAnnotationBeanPostProcessor
*/ */
@Target(ElementType.METHOD) @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
public @interface Scheduled { public @interface Scheduled {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,10 @@ package org.springframework.scheduling.annotation;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
@ -33,7 +37,7 @@ import org.springframework.scheduling.support.MethodInvokingRunnable;
/** /**
* @author Mark Fisher * @author Mark Fisher
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({"unchecked", "unused"})
public class ScheduledAnnotationBeanPostProcessorTests { public class ScheduledAnnotationBeanPostProcessorTests {
@Test @Test
@ -97,7 +101,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
Object target = context.getBean("target"); Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar"); new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
Map<Runnable, Long> cronTasks = (Map<Runnable, Long>) Map<Runnable, String> cronTasks = (Map<Runnable, String>)
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks"); new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
assertEquals(1, cronTasks.size()); assertEquals(1, cronTasks.size());
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next(); MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
@ -108,6 +112,54 @@ public class ScheduledAnnotationBeanPostProcessorTests {
assertEquals("*/7 * * * * ?", cronTasks.values().iterator().next()); assertEquals("*/7 * * * * ?", cronTasks.values().iterator().next());
} }
@Test
public void metaAnnotationWithFixedRate() {
StaticApplicationContext context = new StaticApplicationContext();
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(
ScheduledAnnotationBeanPostProcessorTests.MetaAnnotationFixedRateTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
Map<Runnable, Long> fixedRateTasks = (Map<Runnable, Long>)
new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
assertEquals(1, fixedRateTasks.size());
MethodInvokingRunnable runnable = (MethodInvokingRunnable) fixedRateTasks.keySet().iterator().next();
Object targetObject = runnable.getTargetObject();
String targetMethod = runnable.getTargetMethod();
assertEquals(target, targetObject);
assertEquals("checkForUpdates", targetMethod);
assertEquals(new Long(5000), fixedRateTasks.values().iterator().next());
}
@Test
public void metaAnnotationWithCronExpression() {
StaticApplicationContext context = new StaticApplicationContext();
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(
ScheduledAnnotationBeanPostProcessorTests.MetaAnnotationCronTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
Map<Runnable, String> cronTasks = (Map<Runnable, String>)
new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
assertEquals(1, cronTasks.size());
MethodInvokingRunnable runnable = (MethodInvokingRunnable) cronTasks.keySet().iterator().next();
Object targetObject = runnable.getTargetObject();
String targetMethod = runnable.getTargetMethod();
assertEquals(target, targetObject);
assertEquals("generateReport", targetMethod);
assertEquals("0 0 * * * ?", cronTasks.values().iterator().next());
}
@Test(expected = BeanCreationException.class) @Test(expected = BeanCreationException.class)
public void emptyAnnotation() { public void emptyAnnotation() {
StaticApplicationContext context = new StaticApplicationContext(); StaticApplicationContext context = new StaticApplicationContext();
@ -216,4 +268,32 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
@Scheduled(fixedRate = 5000)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
private static @interface EveryFiveSeconds {}
@Scheduled(cron = "0 0 * * * ?")
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
private static @interface Hourly {}
private static class MetaAnnotationFixedRateTestBean {
@EveryFiveSeconds
public void checkForUpdates() {
}
}
private static class MetaAnnotationCronTestBean {
@Hourly
public void generateReport() {
}
}
} }