Browse Source
java.util.concurrent's ScheduledExecutorService and its #schedule* methods allow for an 'initialDelay' parameter in milliseconds. Similarly, Spring's TaskExecutor abstraction allows for a concrete 'startTime' expressed as a Date. However, Spring's <task:scheduled> XML element and @Scheduled annotation have, to date, not allowed for an initial delay parameter that can be propagated down to the underlying TaskScheduler/ScheduledExecutorService. This commit introduces initial-delay and #initialDelay attributes to task:scheduled and @Scheduled respectively, both indicating the number of milliseconds to wait before the first invocation of the method in question. Specifying a delay in this fashion is only valid in conjunction with fixed-rate and fixed-delay tasks (i.e. not with cron or trigger tasks). The principal changes required to support these new attributes lie in ScheduledTaskRegistrar, which previously supported registration of tasks in the form of a Runnable and a Long parameter indicating (in the case of fixed-rate and fixed-delay tasks), the interval with which the task should be executed. In order to accommodate a third (and optional) 'initialDelay' parameter, the IntervalTask class has been added as a holder for the Runnable to be executed, the interval in which to run it, and the optional initial delay. For symmetry, a TriggerTask and CronTask have also been added, the latter subclassing the former. And a 'Task' class has been added as a common ancestor for all the above. One oddity of the implementation is in the naming of the new setters in ScheduledTaskRegistrar. Prior to this commit, the setters were named #setFixedDelayTasks, #setFixedRateTasks, etc, each accepting a Map<Runnable, long>. In adding new setters for each task type, each accepting a List<IntervalTask>, List<CronTask> etc, naturally the approach would be to use method overloading and to introduce methods of the same name but with differing parameter types. Unfortunately however, Spring does not support injection against overloaded methods (due to fundamental limitations of the underlying JDK Introspector). This is not a problem when working with the ScheduledTaskRegistrar directly, e.g. from within a @Configuration class that implements SchedulingConfigurer, but is a problem from the point of view of the ScheduledTasksBeanDefinitionParser which parses the <task:scheduled> element - here the ScheduledTaskRegistrar is treated as a Spring bean and is thus subject to these limitations. The solution to this problem was simply to avoid overloading altogether, thus the naming of the new methods ending in "List", e.g. #setFixedDelayTasksList, etc. These methods exist primarily for use by the BeanDefinitionParser and are not really intended for use by application developers. The Javadoc for each of the new methods makes note of this. Issue: SPR-7022pull/84/head
Chris Beams
13 years ago
18 changed files with 659 additions and 112 deletions
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.config; |
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled; |
||||
import org.springframework.scheduling.support.CronTrigger; |
||||
|
||||
/** |
||||
* {@link TriggerTask} implementation defining a {@code Runnable} to be executed according |
||||
* to a {@linkplain org.springframework.scheduling.support.CronSequenceGenerator standard |
||||
* cron expression}. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.2 |
||||
* @see Scheduled#cron() |
||||
* @see ScheduledTaskRegistrar#setCronTasksList(java.util.List) |
||||
* @see org.springframework.scheduling.TaskScheduler |
||||
*/ |
||||
public class CronTask extends TriggerTask { |
||||
|
||||
private String expression; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@code CronTask}. |
||||
* @param runnable the underlying task to execute |
||||
* @param expression cron expression defining when the task should be executed |
||||
*/ |
||||
public CronTask(Runnable runnable, String expression) { |
||||
this(runnable, new CronTrigger(expression)); |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code CronTask}. |
||||
* @param runnable the underlying task to execute |
||||
* @param cronTrigger the cron trigger defining when the task should be executed |
||||
*/ |
||||
public CronTask(Runnable runnable, CronTrigger cronTrigger) { |
||||
super(runnable, cronTrigger); |
||||
this.expression = cronTrigger.getExpression(); |
||||
} |
||||
|
||||
public String getExpression() { |
||||
return expression; |
||||
} |
||||
} |
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.config; |
||||
|
||||
/** |
||||
* {@link Task} implementation defining a {@code Runnable} to be executed at a given |
||||
* millisecond interval which may be treated as fixed-rate or fixed-delay depending on |
||||
* context. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.2 |
||||
* @see org.springframework.scheduling.annotation.Scheduled#fixedRate() |
||||
* @see org.springframework.scheduling.annotation.Scheduled#fixedDelay() |
||||
* @see ScheduledTaskRegistrar#setFixedRateTasksList(java.util.List) |
||||
* @see ScheduledTaskRegistrar#setFixedDelayTasksList(java.util.List) |
||||
* @see org.springframework.scheduling.TaskScheduler |
||||
*/ |
||||
public class IntervalTask extends Task { |
||||
|
||||
private final long interval; |
||||
|
||||
private final long initialDelay; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@code IntervalTask}. |
||||
* @param runnable the underlying task to execute |
||||
* @param interval how often in milliseconds the task should be executed |
||||
* @param initialDelay initial delay before first execution of the task |
||||
*/ |
||||
public IntervalTask(Runnable runnable, long interval, long initialDelay) { |
||||
super(runnable); |
||||
this.initialDelay = initialDelay; |
||||
this.interval = interval; |
||||
} |
||||
|
||||
/** |
||||
* Create a new {@code IntervalTask} with no initial delay. |
||||
* @param runnable the underlying task to execute |
||||
* @param interval how often in milliseconds the task should be executed |
||||
*/ |
||||
public IntervalTask(Runnable runnable, long interval) { |
||||
this(runnable, interval, 0); |
||||
} |
||||
|
||||
|
||||
public long getInterval() { |
||||
return interval; |
||||
} |
||||
|
||||
public long getInitialDelay() { |
||||
return initialDelay; |
||||
} |
||||
} |
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.config; |
||||
|
||||
/** |
||||
* Holder class defining a {@code Runnable} to be executed as a task, typically at a |
||||
* scheduled time or interval. See subclass hierarchy for various scheduling approaches. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.2 |
||||
*/ |
||||
public class Task { |
||||
|
||||
private final Runnable runnable; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@code Task}. |
||||
* @param runnable the underlying task to execute. |
||||
*/ |
||||
public Task(Runnable runnable) { |
||||
this.runnable = runnable; |
||||
} |
||||
|
||||
|
||||
public Runnable getRunnable() { |
||||
return runnable; |
||||
} |
||||
} |
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.scheduling.config; |
||||
|
||||
import org.springframework.scheduling.Trigger; |
||||
|
||||
/** |
||||
* {@link Task} implementation defining a {@code Runnable} to be executed according to a |
||||
* given {@link Trigger}. |
||||
* |
||||
* @author Chris Beams |
||||
* @since 3.2 |
||||
* @see Trigger#nextExecutionTime(org.springframework.scheduling.TriggerContext) |
||||
* @see ScheduledTaskRegistrar#setTriggerTasksList(java.util.List) |
||||
* @see org.springframework.scheduling.TaskScheduler#schedule(Runnable, Trigger) |
||||
*/ |
||||
public class TriggerTask extends Task { |
||||
|
||||
private final Trigger trigger; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@link TriggerTask}. |
||||
* @param runnable the underlying task to execute |
||||
* @param trigger specifies when the task should be executed |
||||
*/ |
||||
public TriggerTask(Runnable runnable, Trigger trigger) { |
||||
super(runnable); |
||||
this.trigger = trigger; |
||||
} |
||||
|
||||
|
||||
public Trigger getTrigger() { |
||||
return trigger; |
||||
} |
||||
} |
Loading…
Reference in new issue