KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > timer > TimerTaskExecutor


1 /*
2  * Copyright 2002-2006 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.timer;
18
19 import java.util.Timer JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.scheduling.SchedulingTaskExecutor;
27 import org.springframework.util.Assert;
28
29 /**
30  * {@link org.springframework.core.task.TaskExecutor} implementation that uses a
31  * single {@link Timer} for executing all tasks, effectively resulting in
32  * serialized asynchronous execution on a single thread.
33  *
34  * @author Juergen Hoeller
35  * @since 2.0
36  * @see java.util.Timer
37  */

38 public class TimerTaskExecutor implements SchedulingTaskExecutor, InitializingBean, DisposableBean {
39
40     /**
41      * The shared {@link Log} instance.
42      */

43     protected final Log logger = LogFactory.getLog(getClass());
44
45     private Timer JavaDoc timer;
46
47     private int delay = 0;
48
49     private boolean usingAnInternalTimer = false;
50
51
52     /**
53      * Create a new TimerTaskExecutor that needs to be further
54      * configured and initialized.
55      * @see #setTimer
56      * @see #afterPropertiesSet
57      */

58     public TimerTaskExecutor() {
59     }
60
61     /**
62      * Create a new TimerTaskExecutor for the given {@link Timer}.
63      * @param timer the {@link Timer} to wrap
64      * @throws IllegalArgumentException if the supplied {@link Timer} is <code>null</code>
65      */

66     public TimerTaskExecutor(Timer JavaDoc timer) {
67         Assert.notNull(timer, "Timer must not be null");
68         this.timer = timer;
69     }
70
71     /**
72      * Set the {@link Timer} to use for this {@link TimerTaskExecutor},
73      * for example a shared {@link Timer} instance defined by a
74      * {@link TimerFactoryBean}.
75      * <p>If not specified, a default {@link Timer} instance will be used.
76      * @param timer the {@link Timer} to use for this {@link TimerTaskExecutor} (may be <code>null</code>)
77      * @see TimerFactoryBean
78      */

79     public void setTimer(Timer JavaDoc timer) {
80         this.timer = timer;
81     }
82
83     /**
84      * Set the delay to use for scheduling tasks passed into the
85      * <code>execute</code> method. Default is 0.
86      * @param delay the delay in milliseconds before the task is to be executed
87      */

88     public void setDelay(int delay) {
89         this.delay = delay;
90     }
91
92
93     public void afterPropertiesSet() {
94         if (this.timer == null) {
95             logger.info("Initializing Timer");
96             this.timer = createTimer();
97             this.usingAnInternalTimer = true;
98         }
99     }
100
101     /**
102      * Create a new {@link Timer} instance. Called by <code>afterPropertiesSet</code>
103      * if no {@link Timer} has been specified explicitly.
104      * <p>Default implementation creates a plain daemon {@link Timer}.
105      * <p>If overridden, subclasses must take care to ensure that a non-null
106      * {@link Timer} is returned from the execution of this method.
107      * @return a new {@link Timer} instance
108      * @see #afterPropertiesSet
109      * @see java.util.Timer#Timer(boolean)
110      */

111     protected Timer JavaDoc createTimer() {
112         return new Timer JavaDoc(true);
113     }
114
115
116     /**
117      * Schedules the given {@link Runnable} on this executor's {@link Timer} instance,
118      * wrapping it in a {@link DelegatingTimerTask}.
119      * @param task the task to be executed
120      * @throws IllegalArgumentException if the supplied {@link Runnable task} is <code>null</code>,
121      * or if the {@link Timer} encapsulated by this instance has not been set, or
122      * if the {@link #setDelay(int) configured delay} is negative.
123      */

124     public void execute(Runnable JavaDoc task) {
125         Assert.notNull(this.timer, "timer is required");
126         this.timer.schedule(new DelegatingTimerTask(task), this.delay);
127     }
128
129     /**
130      * This task executor prefers short-lived work units.
131      */

132     public boolean prefersShortLivedTasks() {
133         return true;
134     }
135
136
137     /**
138      * Cancel the {@link Timer} on bean factory shutdown, stopping all scheduled tasks.
139      * @see java.util.Timer#cancel()
140      */

141     public void destroy() {
142         if (this.usingAnInternalTimer) {
143             logger.info("Cancelling Timer");
144             this.timer.cancel();
145         }
146     }
147
148 }
149
Popular Tags