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.commonj;
018:
019: import commonj.timers.TimerListener;
020:
021: /**
022: * JavaBean that describes a scheduled TimerListener, consisting of
023: * the TimerListener itself (or a Runnable to create a TimerListener for)
024: * and a delay plus period. Period needs to be specified;
025: * there is no point in a default for it.
026: *
027: * <p>The CommonJ TimerManager does not offer more sophisticated scheduling
028: * options such as cron expressions. Consider using Quartz for such
029: * advanced needs.
030: *
031: * <p>Note that the TimerManager uses a TimerListener instance that is
032: * shared between repeated executions, in contrast to Quartz which
033: * instantiates a new Job for each execution.
034: *
035: * @author Juergen Hoeller
036: * @since 2.0
037: * @see commonj.timers.TimerListener
038: * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long, long)
039: * @see commonj.timers.TimerManager#scheduleAtFixedRate(commonj.timers.TimerListener, long, long)
040: */
041: public class ScheduledTimerListener {
042:
043: private TimerListener timerListener;
044:
045: private long delay = 0;
046:
047: private long period = -1;
048:
049: private boolean fixedRate = false;
050:
051: /**
052: * Create a new ScheduledTimerListener,
053: * to be populated via bean properties.
054: * @see #setTimerListener
055: * @see #setDelay
056: * @see #setPeriod
057: * @see #setFixedRate
058: */
059: public ScheduledTimerListener() {
060: }
061:
062: /**
063: * Create a new ScheduledTimerListener, with default
064: * one-time execution without delay.
065: * @param timerListener the TimerListener to schedule
066: */
067: public ScheduledTimerListener(TimerListener timerListener) {
068: this .timerListener = timerListener;
069: }
070:
071: /**
072: * Create a new ScheduledTimerListener, with default
073: * one-time execution with the given delay.
074: * @param timerListener the TimerListener to schedule
075: * @param delay the delay before starting the task for the first time (ms)
076: */
077: public ScheduledTimerListener(TimerListener timerListener,
078: long delay) {
079: this .timerListener = timerListener;
080: this .delay = delay;
081: }
082:
083: /**
084: * Create a new ScheduledTimerListener.
085: * @param timerListener the TimerListener to schedule
086: * @param delay the delay before starting the task for the first time (ms)
087: * @param period the period between repeated task executions (ms)
088: * @param fixedRate whether to schedule as fixed-rate execution
089: */
090: public ScheduledTimerListener(TimerListener timerListener,
091: long delay, long period, boolean fixedRate) {
092: this .timerListener = timerListener;
093: this .delay = delay;
094: this .period = period;
095: this .fixedRate = fixedRate;
096: }
097:
098: /**
099: * Create a new ScheduledTimerListener, with default
100: * one-time execution without delay.
101: * @param timerTask the Runnable to schedule as TimerListener
102: */
103: public ScheduledTimerListener(Runnable timerTask) {
104: setRunnable(timerTask);
105: }
106:
107: /**
108: * Create a new ScheduledTimerListener, with default
109: * one-time execution with the given delay.
110: * @param timerTask the Runnable to schedule as TimerListener
111: * @param delay the delay before starting the task for the first time (ms)
112: */
113: public ScheduledTimerListener(Runnable timerTask, long delay) {
114: setRunnable(timerTask);
115: this .delay = delay;
116: }
117:
118: /**
119: * Create a new ScheduledTimerListener.
120: * @param timerTask the Runnable to schedule as TimerListener
121: * @param delay the delay before starting the task for the first time (ms)
122: * @param period the period between repeated task executions (ms)
123: * @param fixedRate whether to schedule as fixed-rate execution
124: */
125: public ScheduledTimerListener(Runnable timerTask, long delay,
126: long period, boolean fixedRate) {
127: setRunnable(timerTask);
128: this .delay = delay;
129: this .period = period;
130: this .fixedRate = fixedRate;
131: }
132:
133: /**
134: * Set the Runnable to schedule as TimerListener.
135: * @see DelegatingTimerListener
136: */
137: public void setRunnable(Runnable timerTask) {
138: this .timerListener = new DelegatingTimerListener(timerTask);
139: }
140:
141: /**
142: * Set the TimerListener to schedule.
143: */
144: public void setTimerListener(TimerListener timerListener) {
145: this .timerListener = timerListener;
146: }
147:
148: /**
149: * Return the TimerListener to schedule.
150: */
151: public TimerListener getTimerListener() {
152: return this .timerListener;
153: }
154:
155: /**
156: * Set the delay before starting the task for the first time,
157: * in milliseconds. Default is 0, immediately starting the
158: * task after successful scheduling.
159: * <p>If the "firstTime" property is specified, this property will be ignored.
160: * Specify one or the other, not both.
161: */
162: public void setDelay(long delay) {
163: this .delay = delay;
164: }
165:
166: /**
167: * Return the delay before starting the job for the first time.
168: */
169: public long getDelay() {
170: return this .delay;
171: }
172:
173: /**
174: * Set the period between repeated task executions, in milliseconds.
175: * <p>Default is -1, leading to one-time execution. In case of zero or a
176: * positive value, the task will be executed repeatedly, with the given
177: * interval inbetween executions.
178: * <p>Note that the semantics of the period value vary between fixed-rate
179: * and fixed-delay execution.
180: * <p><b>Note:</b> A period of 0 (for example as fixed delay) <i>is</i>
181: * supported, because the CommonJ specification defines this as a legal value.
182: * Hence a value of 0 will result in immediate re-execution after a job has
183: * finished (not in one-time execution like with <code>java.util.Timer</code>).
184: * @see #setFixedRate
185: * @see #isOneTimeTask()
186: * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long, long)
187: */
188: public void setPeriod(long period) {
189: this .period = period;
190: }
191:
192: /**
193: * Return the period between repeated task executions.
194: */
195: public long getPeriod() {
196: return this .period;
197: }
198:
199: /**
200: * Is this task only ever going to execute once?
201: * @return <code>true</code> if this task is only ever going to execute once
202: * @see #getPeriod()
203: */
204: public boolean isOneTimeTask() {
205: return (this .period < 0);
206: }
207:
208: /**
209: * Set whether to schedule as fixed-rate execution, rather than
210: * fixed-delay execution. Default is "false", i.e. fixed delay.
211: * <p>See TimerManager javadoc for details on those execution modes.
212: * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long, long)
213: * @see commonj.timers.TimerManager#scheduleAtFixedRate(commonj.timers.TimerListener, long, long)
214: */
215: public void setFixedRate(boolean fixedRate) {
216: this .fixedRate = fixedRate;
217: }
218:
219: /**
220: * Return whether to schedule as fixed-rate execution.
221: */
222: public boolean isFixedRate() {
223: return this.fixedRate;
224: }
225:
226: }
|