diff --git a/org.springframework.integration/.classpath b/org.springframework.integration/.classpath index 34c01dfac1..25f781f33a 100644 --- a/org.springframework.integration/.classpath +++ b/org.springframework.integration/.classpath @@ -15,5 +15,6 @@ + diff --git a/org.springframework.integration/ivy.xml b/org.springframework.integration/ivy.xml index 144dc2d43e..4a191a1cf6 100644 --- a/org.springframework.integration/ivy.xml +++ b/org.springframework.integration/ivy.xml @@ -26,6 +26,7 @@ + \ No newline at end of file diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/CronTrigger.java b/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/CronTrigger.java new file mode 100644 index 0000000000..1c6b4909ce --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/CronTrigger.java @@ -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()); + } + +} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/IntervalTrigger.java b/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/IntervalTrigger.java new file mode 100644 index 0000000000..57655d122f --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/IntervalTrigger.java @@ -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); + } + +} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/Trigger.java b/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/Trigger.java new file mode 100644 index 0000000000..efa12bea13 --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/scheduling/Trigger.java @@ -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 null if the + * task should not run again. + * + * @param lastScheduledRunTime last time the relevant task was scheduled to + * run, or null if it has never been scheduled + * @param lastCompleteTime last time the relevant task finished or + * null if it did not run to completion + * @return next time that a task should run + */ + public Date getNextRunTime(Date lastScheduledRunTime, Date lastCompleteTime); + +} diff --git a/org.springframework.integration/template.mf b/org.springframework.integration/template.mf index b8ed01ab5c..17d65d0821 100644 --- a/org.springframework.integration/template.mf +++ b/org.springframework.integration/template.mf @@ -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