KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > util > TimerUtil


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/TimerUtil.java,v 1.12 2006/04/15 02:59:20 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.12 $
5  * $Date: 2006/04/15 02:59:20 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding MyVietnam and MyVietnam CoreLib
12  * MUST remain intact in the scripts and source code.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  * Correspondence and Marketing Questions can be sent to:
29  * info at MyVietnam net
30  *
31  * @author: Minh Nguyen
32  * @author: Mai Nguyen
33  */

34 package net.myvietnam.mvncore.util;
35
36 import java.util.Timer JavaDoc;
37 import java.util.TimerTask JavaDoc;
38 import java.util.Date JavaDoc;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 public final class TimerUtil {
43
44     // static variable
45
private static Log log = LogFactory.getLog(TimerUtil.class);
46
47     // static variable
48
private static TimerUtil instance = null;
49
50     // static variable
51
private static boolean isCanceled = false;
52
53     // instance variables
54
private Timer JavaDoc timer = null;
55
56     // private constructor will prevent any instatiation
57
private TimerUtil() {
58         log.debug("TimerUtil is instantiated.");
59         timer = new Timer JavaDoc();
60     }
61
62     private void reloadTimer() {
63         log.info("Reload Timer in TimerUtil.");
64         if (!isCanceled) {
65             timer.cancel(); // Cancel the errored timer
66
timer = new Timer JavaDoc();
67         }
68     }
69
70     /**
71      * This static method is used to get the Singleton instance of TimerUtil
72      * @return the singleton instance of TimerUtil
73      */

74     public static synchronized TimerUtil getInstance() {
75         if (instance == null) {
76             instance = new TimerUtil();
77         }
78         return instance;
79     }
80
81     public void cancel() {
82         isCanceled = true;
83         timer.cancel();
84     }
85
86     public void schedule(TimerTask JavaDoc task, Date JavaDoc firstTime, long period) {
87         if (!isCanceled) {
88             try {
89                 timer.schedule(task, firstTime, period);
90             } catch (IllegalStateException JavaDoc ex) {
91                 log.error("Cannot schedule task!", ex);
92                 reloadTimer();
93             }
94         }
95     }
96
97     public void schedule(TimerTask JavaDoc task, Date JavaDoc time) {
98         if (!isCanceled) {
99             try {
100                 timer.schedule(task, time);
101             } catch (IllegalStateException JavaDoc ex) {
102                 log.error("Cannot schedule task!", ex);
103                 reloadTimer();
104             }
105         }
106     }
107
108     public void schedule(TimerTask JavaDoc task, long delay) {
109         if (!isCanceled) {
110             try {
111                 timer.schedule(task, delay);
112             } catch (IllegalStateException JavaDoc ex) {
113                 log.error("Cannot schedule task!", ex);
114                 reloadTimer();
115             }
116         }
117     }
118
119     public void schedule(TimerTask JavaDoc task, long delay, long period) {
120         if (!isCanceled) {
121             try {
122                 timer.schedule(task, delay, period);
123             } catch (IllegalStateException JavaDoc ex) {
124                 log.error("Cannot schedule task!", ex);
125                 reloadTimer();
126             }
127         }
128     }
129
130     public void scheduleAtFixedRate(TimerTask JavaDoc task, Date JavaDoc firstTime, long period) {
131         if (!isCanceled) {
132             try {
133                 timer.schedule(task, firstTime, period);
134             } catch (IllegalStateException JavaDoc ex) {
135                 log.error("Cannot schedule task!", ex);
136                 reloadTimer();
137             }
138         }
139     }
140
141     public void scheduleAtFixedRate(TimerTask JavaDoc task, long delay, long period) {
142         if (!isCanceled) {
143             try {
144                 timer.schedule(task, delay, period);
145             } catch (IllegalStateException JavaDoc ex) {
146                 log.error("Cannot schedule task!", ex);
147                 reloadTimer();
148             }
149         }
150     }
151 }
152
Popular Tags