001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.scheduling.backportconcurrent;
018:
019: import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
020:
021: /**
022: * JavaBean that describes a scheduled executor task, consisting of the
023: * {@link Runnable} and a delay plus period. The period needs to be specified;
024: * there is no point in a default for it.
025: *
026: * <p>The JSR-166 backport
027: * {@link edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService}
028: * does not offer more sophisticated scheduling options such as cron expressions.
029: * Consider using Quartz for such advanced needs.
030: *
031: * <p>Note that the
032: * {@link edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService}
033: * mechanism uses a {@link Runnable} instance that is shared between repeated executions,
034: * in contrast to Quartz which creates a new Job instance for each execution.
035: *
036: * <p>This class is analogous to the {@link org.springframework.scheduling.timer.ScheduledTimerTask}
037: * class for the JDK 1.3 {@link java.util.Timer} facility.
038: *
039: * @author Juergen Hoeller
040: * @since 2.0.3
041: * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
042: * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
043: * @see org.springframework.scheduling.timer.ScheduledTimerTask
044: */
045: public class ScheduledExecutorTask {
046:
047: private Runnable runnable;
048:
049: private long delay = 0;
050:
051: private long period = -1;
052:
053: private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
054:
055: private boolean fixedRate = false;
056:
057: /**
058: * Create a new ScheduledExecutorTask,
059: * to be populated via bean properties.
060: * @see #setDelay
061: * @see #setPeriod
062: * @see #setFixedRate
063: */
064: public ScheduledExecutorTask() {
065: }
066:
067: /**
068: * Create a new ScheduledExecutorTask, with default
069: * one-time execution without delay.
070: * @param executorTask the Runnable to schedule
071: */
072: public ScheduledExecutorTask(Runnable executorTask) {
073: this .runnable = executorTask;
074: }
075:
076: /**
077: * Create a new ScheduledExecutorTask, with default
078: * one-time execution with the given delay.
079: * @param executorTask the Runnable to schedule
080: * @param delay the delay before starting the task for the first time (ms)
081: */
082: public ScheduledExecutorTask(Runnable executorTask, long delay) {
083: this .runnable = executorTask;
084: this .delay = delay;
085: }
086:
087: /**
088: * Create a new ScheduledExecutorTask.
089: * @param executorTask the Runnable to schedule
090: * @param delay the delay before starting the task for the first time (ms)
091: * @param period the period between repeated task executions (ms)
092: * @param fixedRate whether to schedule as fixed-rate execution
093: */
094: public ScheduledExecutorTask(Runnable executorTask, long delay,
095: long period, boolean fixedRate) {
096: this .runnable = executorTask;
097: this .delay = delay;
098: this .period = period;
099: this .fixedRate = fixedRate;
100: }
101:
102: /**
103: * Set the Runnable to schedule as executor task.
104: */
105: public void setRunnable(Runnable executorTask) {
106: this .runnable = executorTask;
107: }
108:
109: /**
110: * Return the Runnable to schedule as executor task.
111: */
112: public Runnable getRunnable() {
113: return this .runnable;
114: }
115:
116: /**
117: * Set the delay before starting the task for the first time,
118: * in milliseconds. Default is 0, immediately starting the
119: * task after successful scheduling.
120: */
121: public void setDelay(long delay) {
122: this .delay = delay;
123: }
124:
125: /**
126: * Return the delay before starting the job for the first time.
127: */
128: public long getDelay() {
129: return this .delay;
130: }
131:
132: /**
133: * Set the period between repeated task executions, in milliseconds.
134: * <p>Default is -1, leading to one-time execution. In case of a positive value,
135: * the task will be executed repeatedly, with the given interval inbetween executions.
136: * <p>Note that the semantics of the period value vary between fixed-rate and
137: * fixed-delay execution.
138: * <p><b>Note:</b> A period of 0 (for example as fixed delay) is <i>not</i> supported,
139: * simply because <code>edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService</code> itself
140: * does not support it. Hence a value of 0 will be treated as one-time execution;
141: * however, that value should never be specified explicitly in the first place!
142: * @see #setFixedRate
143: * @see #isOneTimeTask()
144: * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
145: */
146: public void setPeriod(long period) {
147: this .period = period;
148: }
149:
150: /**
151: * Return the period between repeated task executions.
152: */
153: public long getPeriod() {
154: return this .period;
155: }
156:
157: /**
158: * Is this task only ever going to execute once?
159: * @return <code>true</code> if this task is only ever going to execute once
160: * @see #getPeriod()
161: */
162: public boolean isOneTimeTask() {
163: return (this .period <= 0);
164: }
165:
166: /**
167: * Specify the time unit for the delay and period values.
168: * Default is milliseconds (<code>TimeUnit.MILLISECONDS</code>).
169: * @see edu.emory.mathcs.backport.java.util.concurrent.TimeUnit#MILLISECONDS
170: * @see edu.emory.mathcs.backport.java.util.concurrent.TimeUnit#SECONDS
171: */
172: public void setTimeUnit(TimeUnit timeUnit) {
173: this .timeUnit = (timeUnit != null ? timeUnit
174: : TimeUnit.MILLISECONDS);
175: }
176:
177: /**
178: * Return the time unit for the delay and period values.
179: */
180: public TimeUnit getTimeUnit() {
181: return this .timeUnit;
182: }
183:
184: /**
185: * Set whether to schedule as fixed-rate execution, rather than
186: * fixed-delay execution. Default is "false", that is, fixed delay.
187: * <p>See ScheduledExecutorService javadoc for details on those execution modes.
188: * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
189: * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
190: */
191: public void setFixedRate(boolean fixedRate) {
192: this .fixedRate = fixedRate;
193: }
194:
195: /**
196: * Return whether to schedule as fixed-rate execution.
197: */
198: public boolean isFixedRate() {
199: return this.fixedRate;
200: }
201:
202: }
|