Adding Trigger interface and the IntervalTrigger and CronTrigger implementations.

This commit is contained in:
Mark Fisher
2008-09-24 14:59:32 +00:00
parent de10530387
commit 98b32a9516
6 changed files with 189 additions and 1 deletions

View File

@@ -15,5 +15,6 @@
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.core/2.5.5.A/org.springframework.core-2.5.5.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.core/2.5.5.A/org.springframework.core-sources-2.5.5.A.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.transaction/2.5.5.A/org.springframework.transaction-2.5.5.A.jar" sourcepath="/IVY_CACHE/org.springframework/org.springframework.transaction/2.5.5.A/org.springframework.transaction-sources-2.5.5.A.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/org.springframework.test/2.5.5.A/org.springframework.test-2.5.5.A.jar" sourcepath="IVY_CACHE/org.springframework/org.springframework.test/2.5.5.A/org.springframework.test-sources-2.5.5.A.jar"/>
<classpathentry kind="var" path="IVY_CACHE/com.opensymphony.quartz/com.springsource.org.quartz/1.6.0/com.springsource.org.quartz-1.6.0.jar" sourcepath="/IVY_CACHE/com.opensymphony.quartz/com.springsource.org.quartz/1.6.0/com.springsource.org.quartz-sources-1.6.0.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -26,6 +26,7 @@
<dependency org="org.springframework" name="org.springframework.context" rev="2.5.5.A" conf="compile->runtime"/>
<dependency org="org.springframework" name="org.springframework.transaction" rev="2.5.5.A" conf="compile->runtime"/>
<dependency org="org.springframework" name="org.springframework.test" rev="2.5.5.A" conf="test->runtime"/>
<dependency org="com.opensymphony.quartz" name="com.springsource.org.quartz" rev="1.6.0" conf="compile->runtime"/>
</dependencies>
</ivy-module>

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2008 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.integration.scheduling;
import java.text.ParseException;
import java.util.Date;
import org.quartz.CronExpression;
import org.springframework.integration.ConfigurationException;
/**
* A trigger that uses a cron expression.
*
* @author Mark Fisher
*/
public class CronTrigger implements Trigger {
private final CronExpression expression;
/**
* Create a trigger for the given cron expression.
*/
public CronTrigger(String expression) {
try {
this.expression = new CronExpression(expression);
} catch (ParseException e) {
throw new ConfigurationException(
"failed to parse cron expression: " + expression);
}
}
/**
* Return the next time a task should run. Determined by this trigger's
* cron expression.
*/
public Date getNextRunTime(Date lastScheduledRunTime, Date lastCompleteTime) {
return this.expression.getNextValidTimeAfter(new Date());
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2002-2008 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.integration.scheduling;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.util.Assert;
/**
* A trigger for periodic execution. The interval may be applied as either
* fixed-rate or fixed-delay, and an initial delay value may also be
* configured. The default initial delay is 0, and the default behavior is
* fixed-delay: each subsequent delay is measured from the last completion
* time. To enable execution between the scheduled start time of each
* execution, set 'fixedRate' to true.
*
* @author Mark Fisher
*/
public class IntervalTrigger implements Trigger {
private final long interval;
private volatile long initialDelay = 0;
private volatile boolean fixedRate = false;
/**
* Create a trigger with the given interval in milliseconds.
*/
public IntervalTrigger(long interval) {
Assert.isTrue(interval >= 0, "interval must not be negative");
this.interval = interval;
}
/**
* Create a trigger with the given interval and time unit.
*/
public IntervalTrigger(long interval, TimeUnit unit) {
this(unit.toMillis(interval));
}
/**
* Specify the delay for the initial execution.
*/
public void setInitialDelay(long initialDelay) {
this.initialDelay = initialDelay;
}
/**
* Specify whether the interval should be measured between the
* scheduled start times rather than between actual completion times
* (the latter, "fixed delay" behavior, is the default).
*/
public void setFixedRate(boolean fixedRate) {
this.fixedRate = fixedRate;
}
/**
* Returns the next time a task should run.
*/
public Date getNextRunTime(Date lastScheduledRunTime, Date lastCompleteTime) {
if (lastScheduledRunTime == null) {
return new Date(System.currentTimeMillis() + this.initialDelay);
}
else if (this.fixedRate) {
return new Date(lastScheduledRunTime.getTime() + this.interval);
}
return new Date(lastCompleteTime.getTime() + this.interval);
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2008 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.integration.scheduling;
import java.util.Date;
/**
* A strategy for providing the next time a task should run.
*
* @author Mark Fisher
*/
public interface Trigger {
/**
* Returns the next time that a task should run or <code>null</code> if the
* task should not run again.
*
* @param lastScheduledRunTime last time the relevant task was scheduled to
* run, or <code>null</code> if it has never been scheduled
* @param lastCompleteTime last time the relevant task finished or
* <code>null</code> if it did not run to completion
* @return next time that a task should run
*/
public Date getNextRunTime(Date lastScheduledRunTime, Date lastCompleteTime);
}

View File

@@ -5,7 +5,8 @@ Bundle-ManifestVersion: 2
Import-Template:
org.springframework.*;version="[2.5.5.A, 3.0.0)",
org.apache.commons.logging;version="[1.1.1, 2.0.0)",
org.aopalliance.*;version="[1.0.0,2.0.0)"
org.aopalliance.*;version="[1.0.0,2.0.0)",
org.quartz.*;version="[1.6.0, 2.0.0)";resolution:=optional
Unversioned-Imports:
org.w3c.dom