KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > backportconcurrent > 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.backportconcurrent;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
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 JSR-166 backport
27  * {@link edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService}
28  * does not offer more sophisticated scheduling options such as cron expressions.
29  * Consider using Quartz for such advanced needs.
30  *
31  * <p>Note that the
32  * {@link edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService}
33  * mechanism uses a {@link Runnable} instance that is shared between repeated executions,
34  * in contrast to Quartz which creates a new Job instance for each execution.
35  *
36  * <p>This class is analogous to the {@link org.springframework.scheduling.timer.ScheduledTimerTask}
37  * class for the JDK 1.3 {@link java.util.Timer} facility.
38  *
39  * @author Juergen Hoeller
40  * @since 2.0.3
41  * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
42  * @see edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, edu.emory.mathcs.backport.java.util.concurrent.TimeUnit)
43  * @see org.springframework.scheduling.timer.ScheduledTimerTask
44  */

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

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

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

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

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

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

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

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

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

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

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

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

173     public void setTimeUnit(TimeUnit timeUnit) {
174         this.timeUnit = (timeUnit != null ? timeUnit : 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 }
203
Popular Tags