提交 4d5fe57a 编写于 作者: C Chris Beams

Polish scheduled task execution infrastructure

In anticipation of substantive changes required to implement "initial
delay" support in the <task:scheduled> element and @Scheduled
annotation, the following updates have been made to the components and
infrastructure supporting scheduled task execution:

 - Fix code style violations
 - Fix compiler warnings
 - Add Javadoc where missing, update to use {@code} tags, etc.
 - Organize imports to follow conventions
上级 e2fbaa84
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -24,11 +24,11 @@ import java.lang.annotation.Target; ...@@ -24,11 +24,11 @@ import java.lang.annotation.Target;
/** /**
* Annotation that marks a method to be scheduled. Exactly one of the * Annotation that marks a method to be scheduled. Exactly one of the
* <code>cron</code>, <code>fixedDelay</code>, or <code>fixedRate</code> * {@link #cron()}, {@link #fixedDelay()}, or {@link #fixedRate()}
* attributes must be provided. * attributes must be specified.
* *
* <p>The annotated method must expect no arguments and have a * <p>The annotated method must expect no arguments and have a
* <code>void</code> return type. * {@code void} return type.
* *
* <p>Processing of {@code @Scheduled} annotations is performed by * <p>Processing of {@code @Scheduled} annotations is performed by
* registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be * registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be
...@@ -49,9 +49,10 @@ public @interface Scheduled { ...@@ -49,9 +49,10 @@ public @interface Scheduled {
/** /**
* A cron-like expression, extending the usual UN*X definition to include * A cron-like expression, extending the usual UN*X definition to include
* triggers on the second as well as minute, hour, day of month, month * triggers on the second as well as minute, hour, day of month, month
* and day of week. e.g. <code>"0 * * * * MON-FRI"</code> means once * and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on
* per minute on weekdays (at the top of the minute - the 0th second). * weekdays (at the top of the minute - the 0th second).
* @return an expression that can be parsed to a cron schedule * @return an expression that can be parsed to a cron schedule
* @see org.springframework.scheduling.support.CronSequenceGenerator
*/ */
String cron() default ""; String cron() default "";
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.scheduling.annotation; package org.springframework.scheduling.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -104,7 +105,6 @@ public class ScheduledAnnotationBeanPostProcessor ...@@ -104,7 +105,6 @@ public class ScheduledAnnotationBeanPostProcessor
return LOWEST_PRECEDENCE; return LOWEST_PRECEDENCE;
} }
public Object postProcessBeforeInitialization(Object bean, String beanName) { public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean; return bean;
} }
...@@ -124,9 +124,11 @@ public class ScheduledAnnotationBeanPostProcessor ...@@ -124,9 +124,11 @@ public class ScheduledAnnotationBeanPostProcessor
// found a @Scheduled method on the target class for this JDK proxy -> is it // found a @Scheduled method on the target class for this JDK proxy -> is it
// also present on the proxy itself? // also present on the proxy itself?
method = bean.getClass().getMethod(method.getName(), method.getParameterTypes()); method = bean.getClass().getMethod(method.getName(), method.getParameterTypes());
} catch (SecurityException ex) { }
catch (SecurityException ex) {
ReflectionUtils.handleReflectionException(ex); ReflectionUtils.handleReflectionException(ex);
} catch (NoSuchMethodException ex) { }
catch (NoSuchMethodException ex) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"@Scheduled method '%s' found on bean target class '%s', " + "@Scheduled method '%s' found on bean target class '%s', " +
"but not found in any interface(s) for bean JDK proxy. Either " + "but not found in any interface(s) for bean JDK proxy. Either " +
...@@ -137,7 +139,7 @@ public class ScheduledAnnotationBeanPostProcessor ...@@ -137,7 +139,7 @@ public class ScheduledAnnotationBeanPostProcessor
} }
Runnable runnable = new ScheduledMethodRunnable(bean, method); Runnable runnable = new ScheduledMethodRunnable(bean, method);
boolean processedSchedule = false; boolean processedSchedule = false;
String errorMessage = "Exactly one of 'cron', 'fixedDelay', or 'fixedRate' is required."; String errorMessage = "Exactly one of the 'cron', 'fixedDelay', or 'fixedRate' attributes is required.";
String cron = annotation.cron(); String cron = annotation.cron();
if (!"".equals(cron)) { if (!"".equals(cron)) {
processedSchedule = true; processedSchedule = true;
...@@ -170,7 +172,8 @@ public class ScheduledAnnotationBeanPostProcessor ...@@ -170,7 +172,8 @@ public class ScheduledAnnotationBeanPostProcessor
return; return;
} }
Map<String, SchedulingConfigurer> configurers = applicationContext.getBeansOfType(SchedulingConfigurer.class); Map<String, SchedulingConfigurer> configurers =
this.applicationContext.getBeansOfType(SchedulingConfigurer.class);
if (this.cronTasks.isEmpty() && this.fixedDelayTasks.isEmpty() && if (this.cronTasks.isEmpty() && this.fixedDelayTasks.isEmpty() &&
this.fixedRateTasks.isEmpty() && configurers.isEmpty()) { this.fixedRateTasks.isEmpty() && configurers.isEmpty()) {
...@@ -190,19 +193,23 @@ public class ScheduledAnnotationBeanPostProcessor ...@@ -190,19 +193,23 @@ public class ScheduledAnnotationBeanPostProcessor
configurer.configureTasks(this.registrar); configurer.configureTasks(this.registrar);
} }
if (registrar.getScheduler() == null) { if (this.registrar.getScheduler() == null) {
Map<String, ? super Object> schedulers = new HashMap<String, Object>(); Map<String, ? super Object> schedulers = new HashMap<String, Object>();
schedulers.putAll(applicationContext.getBeansOfType(TaskScheduler.class)); schedulers.putAll(applicationContext.getBeansOfType(TaskScheduler.class));
schedulers.putAll(applicationContext.getBeansOfType(ScheduledExecutorService.class)); schedulers.putAll(applicationContext.getBeansOfType(ScheduledExecutorService.class));
if (schedulers.size() == 0) { if (schedulers.size() == 0) {
// do nothing -> fall back to default scheduler // do nothing -> fall back to default scheduler
} else if (schedulers.size() == 1) { }
else if (schedulers.size() == 1) {
this.registrar.setScheduler(schedulers.values().iterator().next()); this.registrar.setScheduler(schedulers.values().iterator().next());
} else if (schedulers.size() >= 2){ }
throw new IllegalStateException("More than one TaskScheduler and/or ScheduledExecutorService " + else if (schedulers.size() >= 2){
"exist within the context. Remove all but one of the beans; or implement the " + throw new IllegalStateException(
"SchedulingConfigurer interface and call ScheduledTaskRegistrar#setScheduler " + "More than one TaskScheduler and/or ScheduledExecutorService " +
"explicitly within the configureTasks() callback. Found the following beans: " + schedulers.keySet()); "exist within the context. Remove all but one of the beans; or " +
"implement the SchedulingConfigurer interface and call " +
"ScheduledTaskRegistrar#setScheduler explicitly within the " +
"configureTasks() callback. Found the following beans: " + schedulers.keySet());
} }
} }
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -19,12 +19,15 @@ package org.springframework.scheduling.annotation; ...@@ -19,12 +19,15 @@ package org.springframework.scheduling.annotation;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
/** /**
* Interface to be implemented by @{@link org.springframework.context.annotation.Configuration} * Optional interface to be implemented by @{@link
* classes annotated with @{@link EnableScheduling} that wish to register scheduled tasks * org.springframework.context.annotation.Configuration Configuration} classes annotated
* in a <em>programmatic</em> fashion as opposed to the <em>declarative</em> approach of * with @{@link EnableScheduling}. Typically used for setting a specific
* using the @{@link Scheduled} annotation. For example, this may be necessary when * {@link org.springframework.scheduling.TaskScheduler TaskScheduler} bean to be used when
* implementing {@link org.springframework.scheduling.Trigger Trigger}-based tasks, which * executing scheduled tasks or for registering scheduled tasks in a <em>programmatic</em>
* are not supported by the {@code @Scheduled} annotation. * fashion as opposed to the <em>declarative</em> approach of using the @{@link Scheduled}
* annotation. For example, this may be necessary when implementing {@link
* org.springframework.scheduling.Trigger Trigger}-based tasks, which are not supported by
* the {@code @Scheduled} annotation.
* *
* <p>See @{@link EnableScheduling} for detailed usage examples. * <p>See @{@link EnableScheduling} for detailed usage examples.
* *
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -65,7 +65,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -65,7 +65,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
/** /**
* Set the TaskScheduler to register scheduled tasks with. * Set the {@link TaskScheduler} to register scheduled tasks with.
*/ */
public void setTaskScheduler(TaskScheduler taskScheduler) { public void setTaskScheduler(TaskScheduler taskScheduler) {
Assert.notNull(taskScheduler, "TaskScheduler must not be null"); Assert.notNull(taskScheduler, "TaskScheduler must not be null");
...@@ -73,9 +73,9 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -73,9 +73,9 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
} }
/** /**
* Set the {@link org.springframework.scheduling.TaskScheduler} to register scheduled * Set the {@link TaskScheduler} to register scheduled tasks with, or a
* tasks with, or a {@link java.util.concurrent.ScheduledExecutorService} to be * {@link java.util.concurrent.ScheduledExecutorService} to be wrapped as a
* wrapped as a TaskScheduler. * {@code TaskScheduler}.
*/ */
public void setScheduler(Object scheduler) { public void setScheduler(Object scheduler) {
Assert.notNull(scheduler, "Scheduler object must not be null"); Assert.notNull(scheduler, "Scheduler object must not be null");
...@@ -91,7 +91,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -91,7 +91,7 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
} }
/** /**
* Return the scheduler instance for this registrar (may be null) * Return the {@link TaskScheduler} instance for this registrar (may be {@code null}).
*/ */
public TaskScheduler getScheduler() { public TaskScheduler getScheduler() {
return this.taskScheduler; return this.taskScheduler;
...@@ -121,6 +121,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -121,6 +121,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
this.fixedRateTasks = fixedRateTasks; this.fixedRateTasks = fixedRateTasks;
} }
/**
* Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
*/
public void setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) {
this.fixedDelayTasks = fixedDelayTasks;
}
/** /**
* Add a Runnable task to be triggered per the given {@link Trigger}. * Add a Runnable task to be triggered per the given {@link Trigger}.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long) * @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
...@@ -142,17 +150,6 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -142,17 +150,6 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
this.cronTasks.put(task, cronExpression); this.cronTasks.put(task, cronExpression);
} }
/**
* Add a Runnable task to be triggered with the given fixed delay.
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
*/
public void addFixedDelayTask(Runnable task, long delay) {
if (this.fixedDelayTasks == null) {
this.fixedDelayTasks = new HashMap<Runnable, Long>();
}
this.fixedDelayTasks.put(task, delay);
}
/** /**
* Add a Runnable task to be triggered at the given fixed-rate period. * Add a Runnable task to be triggered at the given fixed-rate period.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long) * @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
...@@ -165,14 +162,20 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -165,14 +162,20 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
} }
/** /**
* Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values. * Add a Runnable task to be triggered with the given fixed delay.
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long) * @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
*/ */
public void setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) { public void addFixedDelayTask(Runnable task, long delay) {
this.fixedDelayTasks = fixedDelayTasks; if (this.fixedDelayTasks == null) {
this.fixedDelayTasks = new HashMap<Runnable, Long>();
}
this.fixedDelayTasks.put(task, delay);
} }
/**
* Schedule all registered tasks against the underlying {@linkplain
* #setTaskScheduler(TaskScheduler) task scheduler.
*/
public void afterPropertiesSet() { public void afterPropertiesSet() {
if (this.taskScheduler == null) { if (this.taskScheduler == null) {
this.localExecutor = Executors.newSingleThreadScheduledExecutor(); this.localExecutor = Executors.newSingleThreadScheduledExecutor();
...@@ -200,7 +203,6 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean ...@@ -200,7 +203,6 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
} }
} }
public void destroy() { public void destroy() {
for (ScheduledFuture<?> future : this.scheduledFutures) { for (ScheduledFuture<?> future : this.scheduledFutures) {
future.cancel(true); future.cancel(true);
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -16,10 +16,6 @@ ...@@ -16,10 +16,6 @@
package org.springframework.scheduling.config; package org.springframework.scheduling.config;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
...@@ -28,6 +24,10 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; ...@@ -28,6 +24,10 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/** /**
* Parser for the 'scheduled-tasks' element of the scheduling namespace. * Parser for the 'scheduled-tasks' element of the scheduling namespace.
* *
...@@ -87,9 +87,8 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini ...@@ -87,9 +87,8 @@ public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefini
if (!(hasCronAttribute | hasFixedDelayAttribute | hasFixedRateAttribute | hasTriggerAttribute)) { if (!(hasCronAttribute | hasFixedDelayAttribute | hasFixedRateAttribute | hasTriggerAttribute)) {
parserContext.getReaderContext().error( parserContext.getReaderContext().error(
"exactly one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement); "one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
// Continue with the possible next task element continue; // with the possible next task element
continue;
} }
if (hasCronAttribute) { if (hasCronAttribute) {
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -16,15 +16,11 @@ ...@@ -16,15 +16,11 @@
package org.springframework.scheduling.annotation; package org.springframework.scheduling.annotation;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import java.util.Date; import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -34,6 +30,10 @@ import org.springframework.scheduling.TriggerContext; ...@@ -34,6 +30,10 @@ import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/** /**
* Tests use of @EnableScheduling on @Configuration classes. * Tests use of @EnableScheduling on @Configuration classes.
* *
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -25,7 +25,6 @@ import java.lang.reflect.Method; ...@@ -25,7 +25,6 @@ import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.DirectFieldAccessor;
...@@ -37,6 +36,8 @@ import org.springframework.context.support.StaticApplicationContext; ...@@ -37,6 +36,8 @@ import org.springframework.context.support.StaticApplicationContext;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.ScheduledMethodRunnable; import org.springframework.scheduling.support.ScheduledMethodRunnable;
import static org.junit.Assert.*;
/** /**
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller * @author Juergen Hoeller
...@@ -269,7 +270,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -269,7 +270,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
public static class FixedDelayTestBean { static class FixedDelayTestBean {
@Scheduled(fixedDelay=5000) @Scheduled(fixedDelay=5000)
public void fixedDelay() { public void fixedDelay() {
...@@ -277,7 +278,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -277,7 +278,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
public static class FixedRateTestBean { static class FixedRateTestBean {
@Scheduled(fixedRate=3000) @Scheduled(fixedRate=3000)
public void fixedRate() { public void fixedRate() {
...@@ -285,7 +286,15 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -285,7 +286,15 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
public static class CronTestBean { static class FixedRateWithInitialDelayTestBean {
@Scheduled(initialDelay=1000, fixedRate=3000)
public void fixedRate() {
}
}
static class CronTestBean {
@Scheduled(cron="*/7 * * * * ?") @Scheduled(cron="*/7 * * * * ?")
public void cron() throws IOException { public void cron() throws IOException {
...@@ -295,7 +304,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -295,7 +304,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
private static class EmptyAnnotationTestBean { static class EmptyAnnotationTestBean {
@Scheduled @Scheduled
public void invalid() { public void invalid() {
...@@ -304,7 +313,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -304,7 +313,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
private static class InvalidCronTestBean { static class InvalidCronTestBean {
@Scheduled(cron="abc") @Scheduled(cron="abc")
public void invalid() { public void invalid() {
...@@ -313,7 +322,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -313,7 +322,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
private static class NonVoidReturnTypeTestBean { static class NonVoidReturnTypeTestBean {
@Scheduled(fixedRate=3000) @Scheduled(fixedRate=3000)
public String invalid() { public String invalid() {
...@@ -323,7 +332,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -323,7 +332,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
private static class NonEmptyParamListTestBean { static class NonEmptyParamListTestBean {
@Scheduled(fixedRate=3000) @Scheduled(fixedRate=3000)
public void invalid(String oops) { public void invalid(String oops) {
...@@ -344,7 +353,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -344,7 +353,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
private static @interface Hourly {} private static @interface Hourly {}
private static class MetaAnnotationFixedRateTestBean { static class MetaAnnotationFixedRateTestBean {
@EveryFiveSeconds @EveryFiveSeconds
public void checkForUpdates() { public void checkForUpdates() {
...@@ -352,7 +361,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -352,7 +361,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
private static class MetaAnnotationCronTestBean { static class MetaAnnotationCronTestBean {
@Hourly @Hourly
public void generateReport() { public void generateReport() {
...@@ -360,7 +369,7 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -360,7 +369,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
} }
private static class PropertyPlaceholderTestBean { static class PropertyPlaceholderTestBean {
@Scheduled(cron = "${schedules.businessHours}") @Scheduled(cron = "${schedules.businessHours}")
public void x() { public void x() {
...@@ -370,11 +379,11 @@ public class ScheduledAnnotationBeanPostProcessorTests { ...@@ -370,11 +379,11 @@ public class ScheduledAnnotationBeanPostProcessorTests {
@Scheduled(cron = "${schedules.businessHours}") @Scheduled(cron = "${schedules.businessHours}")
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
private static @interface BusinessHours {} private static @interface BusinessHours {}
private static class PropertyPlaceholderMetaAnnotationTestBean { static class PropertyPlaceholderMetaAnnotationTestBean {
@BusinessHours @BusinessHours
public void y() { public void y() {
......
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
...@@ -21,7 +21,6 @@ import java.util.Collection; ...@@ -21,7 +21,6 @@ import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -32,6 +31,8 @@ import org.springframework.scheduling.Trigger; ...@@ -32,6 +31,8 @@ import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.ScheduledMethodRunnable; import org.springframework.scheduling.support.ScheduledMethodRunnable;
import static org.junit.Assert.*;
/** /**
* @author Mark Fisher * @author Mark Fisher
*/ */
......
...@@ -472,7 +472,7 @@ public class TaskExecutorExample { ...@@ -472,7 +472,7 @@ public class TaskExecutorExample {
example.</para> example.</para>
<programlisting language="xml">&lt;task:scheduled-tasks scheduler="myScheduler"&gt; <programlisting language="xml">&lt;task:scheduled-tasks scheduler="myScheduler"&gt;
&lt;task:scheduled ref="someObject" method="someMethod" fixed-delay="5000"/&gt; &lt;task:scheduled ref="beanA" method="methodA" fixed-delay="5000"/&gt;
&lt;/task:scheduled-tasks&gt; &lt;/task:scheduled-tasks&gt;
&lt;task:scheduler id="myScheduler" pool-size="10"/&gt;</programlisting> &lt;task:scheduler id="myScheduler" pool-size="10"/&gt;</programlisting>
...@@ -480,13 +480,16 @@ public class TaskExecutorExample { ...@@ -480,13 +480,16 @@ public class TaskExecutorExample {
<para>As you can see, the scheduler is referenced by the outer element, <para>As you can see, the scheduler is referenced by the outer element,
and each individual task includes the configuration of its trigger and each individual task includes the configuration of its trigger
metadata. In the preceding example, that metadata defines a periodic metadata. In the preceding example, that metadata defines a periodic
trigger with a fixed delay. It could also be configured with a trigger with a fixed delay indicating the number of milliseconds to wait
"fixed-rate", or for more control, a "cron" attribute could be provided after each task execution has completed. Another option is 'fixed-rate',
instead. Here's an example featuring these other options.</para> indicating how often the method should be executed regardless of how long
any previous execution takes. For more control, a "cron" attribute may be
provided instead. Here is an example demonstrating these other options.
</para>
<programlisting language="xml">&lt;task:scheduled-tasks scheduler="myScheduler"&gt; <programlisting language="xml">&lt;task:scheduled-tasks scheduler="myScheduler"&gt;
&lt;task:scheduled ref="someObject" method="someMethod" fixed-rate="5000"/&gt; &lt;task:scheduled ref="beanB" method="methodB" fixed-rate="5000"/&gt;
&lt;task:scheduled ref="anotherObject" method="anotherMethod" cron="*/5 * * * * MON-FRI"/&gt; &lt;task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI"/&gt;
&lt;/task:scheduled-tasks&gt; &lt;/task:scheduled-tasks&gt;
&lt;task:scheduler id="myScheduler" pool-size="10"/&gt;</programlisting> &lt;task:scheduler id="myScheduler" pool-size="10"/&gt;</programlisting>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册