KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > concurrent > ScheduledExecutorTask


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.concurrent;
18
19 import java.util.concurrent.TimeUnit JavaDoc;
20
21 /**
22  * JavaBean that describes a scheduled executor task, consisting of the
23  * {@link Runnable} and a delay plus period. The period needs to be specified;
24  * there is no point in a default for it.
25  *
26  * <p>The JDK 1.5 {@link java.util.concurrent.ScheduledExecutorService} does
27  * not offer more sophisticated scheduling options such as cron expressions.
28  * Consider using Quartz for such advanced needs.
29  *
30  * <p>Note that the {@link java.util.concurrent.ScheduledExecutorService} mechanism
31  * uses a {@link Runnable} instance that is shared between repeated executions,
32  * in contrast to Quartz which creates a new Job instance for each execution.
33  *
34  * <p>This class is analogous to the {@link org.springframework.scheduling.timer.ScheduledTimerTask}
35  * class for the JDK 1.3 {@link java.util.Timer} facility.
36  *
37  * @author Juergen Hoeller
38  * @since 2.0
39  * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
40  * @see java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
41  * @see org.springframework.scheduling.timer.ScheduledTimerTask
42  */

43 public class ScheduledExecutorTask {
44
45     private Runnable JavaDoc runnable;
46
47     private long delay = 0;
48
49     private long period = -1;
50
51     private TimeUnit JavaDoc timeUnit = TimeUnit.MILLISECONDS;
52
53     private boolean fixedRate = false;
54
55
56     /**
57      * Create a new ScheduledExecutorTask,
58      * to be populated via bean properties.
59      * @see #setDelay
60      * @see #setPeriod
61      * @see #setFixedRate
62      */

63     public ScheduledExecutorTask() {
64     }
65
66     /**
67      * Create a new ScheduledExecutorTask, with default
68      * one-time execution without delay.
69      * @param executorTask the Runnable to schedule
70      */

71     public ScheduledExecutorTask(Runnable JavaDoc executorTask) {
72         this.runnable = executorTask;
73     }
74
75     /**
76      * Create a new ScheduledExecutorTask, with default
77      * one-time execution with the given delay.
78      * @param executorTask the Runnable to schedule
79      * @param delay the delay before starting the task for the first time (ms)
80      */

81     public ScheduledExecutorTask(Runnable JavaDoc executorTask, long delay) {
82         this.runnable = executorTask;
83         this.delay = delay;
84     }
85
86     /**
87      * Create a new ScheduledExecutorTask.
88      * @param executorTask the Runnable to schedule
89      * @param delay the delay before starting the task for the first time (ms)
90      * @param period the period between repeated task executions (ms)
91      * @param fixedRate whether to schedule as fixed-rate execution
92      */

93     public ScheduledExecutorTask(Runnable JavaDoc executorTask, long delay, long period, boolean fixedRate) {
94         this.runnable = executorTask;
95         this.delay = delay;
96         this.period = period;
97         this.fixedRate = fixedRate;
98     }
99
100
101     /**
102      * Set the Runnable to schedule as executor task.
103      */

104     public void setRunnable(Runnable JavaDoc executorTask) {
105         this.runnable = executorTask;
106     }
107
108     /**
109      * Return the Runnable to schedule as executor task.
110      */

111     public Runnable JavaDoc getRunnable() {
112         return this.runnable;
113     }
114
115     /**
116      * Set the delay before starting the task for the first time,
117      * in milliseconds. Default is 0, immediately starting the
118      * task after successful scheduling.
119      */

120     public void setDelay(long delay) {
121         this.delay = delay;
122     }
123
124     /**
125      * Return the delay before starting the job for the first time.
126      */

127     public long getDelay() {
128         return this.delay;
129     }
130
131     /**
132      * Set the period between repeated task executions, in milliseconds.
133      * <p>Default is -1, leading to one-time execution. In case of a positive value,
134      * the task will be executed repeatedly, with the given interval inbetween executions.
135      * <p>Note that the semantics of the period value vary between fixed-rate and
136      * fixed-delay execution.
137      * <p><b>Note:</b> A period of 0 (for example as fixed delay) is <i>not</i> supported,
138      * simply because <code>java.util.concurrent.ScheduledExecutorService</code> itself
139      * does not support it. Hence a value of 0 will be treated as one-time execution;
140      * however, that value should never be specified explicitly in the first place!
141      * @see #setFixedRate
142      * @see #isOneTimeTask()
143      * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
144      */

145     public void setPeriod(long period) {
146         this.period = period;
147     }
148
149     /**
150      * Return the period between repeated task executions.
151      */

152     public long getPeriod() {
153         return this.period;
154     }
155
156     /**
157      * Is this task only ever going to execute once?
158      * @return <code>true</code> if this task is only ever going to execute once
159      * @see #getPeriod()
160      */

161     public boolean isOneTimeTask() {
162         return (this.period <= 0);
163     }
164
165     /**
166      * Specify the time unit for the delay and period values.
167      * Default is milliseconds (<code>TimeUnit.MILLISECONDS</code>).
168      * @see java.util.concurrent.TimeUnit#MILLISECONDS
169      * @see java.util.concurrent.TimeUnit#SECONDS
170      */

171     public void setTimeUnit(TimeUnit JavaDoc timeUnit) {
172         this.timeUnit = (timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS);
173     }
174
175     /**
176      * Return the time unit for the delay and period values.
177      */

178     public TimeUnit JavaDoc 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 }
201
Popular Tags