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.concurrent;
018:
019: import 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 JDK 1.5 {@link java.util.concurrent.ScheduledExecutorService} does
027: * not offer more sophisticated scheduling options such as cron expressions.
028: * Consider using Quartz for such advanced needs.
029: *
030: * <p>Note that the {@link java.util.concurrent.ScheduledExecutorService} mechanism
031: * uses a {@link Runnable} instance that is shared between repeated executions,
032: * in contrast to Quartz which creates a new Job instance for each execution.
033: *
034: * <p>This class is analogous to the {@link org.springframework.scheduling.timer.ScheduledTimerTask}
035: * class for the JDK 1.3 {@link java.util.Timer} facility.
036: *
037: * @author Juergen Hoeller
038: * @since 2.0
039: * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
040: * @see java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
041: * @see org.springframework.scheduling.timer.ScheduledTimerTask
042: */
043: public class ScheduledExecutorTask {
044:
045: private Runnable runnable;
046:
047: private long delay = 0;
048:
049: private long period = -1;
050:
051: private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
052:
053: private boolean fixedRate = false;
054:
055: /**
056: * Create a new ScheduledExecutorTask,
057: * to be populated via bean properties.
058: * @see #setDelay
059: * @see #setPeriod
060: * @see #setFixedRate
061: */
062: public ScheduledExecutorTask() {
063: }
064:
065: /**
066: * Create a new ScheduledExecutorTask, with default
067: * one-time execution without delay.
068: * @param executorTask the Runnable to schedule
069: */
070: public ScheduledExecutorTask(Runnable executorTask) {
071: this .runnable = executorTask;
072: }
073:
074: /**
075: * Create a new ScheduledExecutorTask, with default
076: * one-time execution with the given delay.
077: * @param executorTask the Runnable to schedule
078: * @param delay the delay before starting the task for the first time (ms)
079: */
080: public ScheduledExecutorTask(Runnable executorTask, long delay) {
081: this .runnable = executorTask;
082: this .delay = delay;
083: }
084:
085: /**
086: * Create a new ScheduledExecutorTask.
087: * @param executorTask the Runnable to schedule
088: * @param delay the delay before starting the task for the first time (ms)
089: * @param period the period between repeated task executions (ms)
090: * @param fixedRate whether to schedule as fixed-rate execution
091: */
092: public ScheduledExecutorTask(Runnable executorTask, long delay,
093: long period, boolean fixedRate) {
094: this .runnable = executorTask;
095: this .delay = delay;
096: this .period = period;
097: this .fixedRate = fixedRate;
098: }
099:
100: /**
101: * Set the Runnable to schedule as executor task.
102: */
103: public void setRunnable(Runnable executorTask) {
104: this .runnable = executorTask;
105: }
106:
107: /**
108: * Return the Runnable to schedule as executor task.
109: */
110: public Runnable getRunnable() {
111: return this .runnable;
112: }
113:
114: /**
115: * Set the delay before starting the task for the first time,
116: * in milliseconds. Default is 0, immediately starting the
117: * task after successful scheduling.
118: */
119: public void setDelay(long delay) {
120: this .delay = delay;
121: }
122:
123: /**
124: * Return the delay before starting the job for the first time.
125: */
126: public long getDelay() {
127: return this .delay;
128: }
129:
130: /**
131: * Set the period between repeated task executions, in milliseconds.
132: * <p>Default is -1, leading to one-time execution. In case of a positive value,
133: * the task will be executed repeatedly, with the given interval inbetween executions.
134: * <p>Note that the semantics of the period value vary between fixed-rate and
135: * fixed-delay execution.
136: * <p><b>Note:</b> A period of 0 (for example as fixed delay) is <i>not</i> supported,
137: * simply because <code>java.util.concurrent.ScheduledExecutorService</code> itself
138: * does not support it. Hence a value of 0 will be treated as one-time execution;
139: * however, that value should never be specified explicitly in the first place!
140: * @see #setFixedRate
141: * @see #isOneTimeTask()
142: * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
143: */
144: public void setPeriod(long period) {
145: this .period = period;
146: }
147:
148: /**
149: * Return the period between repeated task executions.
150: */
151: public long getPeriod() {
152: return this .period;
153: }
154:
155: /**
156: * Is this task only ever going to execute once?
157: * @return <code>true</code> if this task is only ever going to execute once
158: * @see #getPeriod()
159: */
160: public boolean isOneTimeTask() {
161: return (this .period <= 0);
162: }
163:
164: /**
165: * Specify the time unit for the delay and period values.
166: * Default is milliseconds (<code>TimeUnit.MILLISECONDS</code>).
167: * @see java.util.concurrent.TimeUnit#MILLISECONDS
168: * @see java.util.concurrent.TimeUnit#SECONDS
169: */
170: public void setTimeUnit(TimeUnit timeUnit) {
171: this .timeUnit = (timeUnit != null ? timeUnit
172: : TimeUnit.MILLISECONDS);
173: }
174:
175: /**
176: * Return the time unit for the delay and period values.
177: */
178: public TimeUnit getTimeUnit() {
179: return this .timeUnit;
180: }
181:
182: /**
183: * Set whether to schedule as fixed-rate execution, rather than
184: * fixed-delay execution. Default is "false", that is, fixed delay.
185: * <p>See ScheduledExecutorService javadoc for details on those execution modes.
186: * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
187: * @see java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
188: */
189: public void setFixedRate(boolean fixedRate) {
190: this .fixedRate = fixedRate;
191: }
192:
193: /**
194: * Return whether to schedule as fixed-rate execution.
195: */
196: public boolean isFixedRate() {
197: return this.fixedRate;
198: }
199:
200: }
|