001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/TimerUtil.java,v 1.17 2007/06/15 10:59:19 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.17 $
005: * $Date: 2007/06/15 10:59:19 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: Minh Nguyen
032: * @author: Mai Nguyen
033: */
034: package net.myvietnam.mvncore.util;
035:
036: import java.util.Timer;
037: import java.util.TimerTask;
038: import java.util.Date;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: public final class TimerUtil {
043:
044: // static variable
045: private static Log log = LogFactory.getLog(TimerUtil.class);
046:
047: // static variable
048: private static TimerUtil instance = null;
049:
050: // static variable
051: private static boolean isCanceled = false;
052:
053: // instance variables
054: private Timer timer = null;
055:
056: // private constructor will prevent any instatiation
057: private TimerUtil() {
058: log.debug("TimerUtil is instantiated.");
059: timer = new Timer();
060: }
061:
062: private void reloadTimer() {
063: log.info("Reload Timer in TimerUtil.");
064: if (isCanceled == false) {
065: timer.cancel(); // Cancel the errored timer
066: timer = new Timer();
067: } else {
068: log.warn("Cannot reload a cancelled Timer.");
069: }
070: }
071:
072: /**
073: * This static method is used to get the Singleton instance of TimerUtil
074: * @return the singleton instance of TimerUtil
075: */
076: public static synchronized TimerUtil getInstance() {
077: if (instance == null) {
078: instance = new TimerUtil();
079: }
080: return instance;
081: }
082:
083: public void cancel() {
084: log.debug("TimerUtil.cancel() is called");
085:
086: isCanceled = true;
087: timer.cancel();
088: }
089:
090: public boolean isTimerCanceled() {
091: return isCanceled;
092: }
093:
094: public void schedule(TimerTask task, Date firstTime, long period) {
095: if (isCanceled == false) {
096: try {
097: timer.schedule(task, firstTime, period);
098: } catch (IllegalStateException ex) {
099: log.error("Cannot schedule task!", ex);
100: reloadTimer();
101: }
102: }
103: }
104:
105: public void schedule(TimerTask task, Date time) {
106: if (isCanceled == false) {
107: try {
108: timer.schedule(task, time);
109: } catch (IllegalStateException ex) {
110: log.error("Cannot schedule task!", ex);
111: reloadTimer();
112: }
113: }
114: }
115:
116: public void schedule(TimerTask task, long delay) {
117: if (isCanceled == false) {
118: try {
119: timer.schedule(task, delay);
120: } catch (IllegalStateException ex) {
121: log.error("Cannot schedule task!", ex);
122: reloadTimer();
123: }
124: }
125: }
126:
127: public void schedule(TimerTask task, long delay, long period) {
128: if (isCanceled == false) {
129: try {
130: timer.schedule(task, delay, period);
131: } catch (IllegalStateException ex) {
132: log.error("Cannot schedule task!", ex);
133: reloadTimer();
134: }
135: }
136: }
137:
138: public void scheduleAtFixedRate(TimerTask task, Date firstTime,
139: long period) {
140: if (isCanceled == false) {
141: try {
142: timer.schedule(task, firstTime, period);
143: } catch (IllegalStateException ex) {
144: log.error("Cannot schedule task!", ex);
145: reloadTimer();
146: }
147: }
148: }
149:
150: public void scheduleAtFixedRate(TimerTask task, long delay,
151: long period) {
152: if (isCanceled == false) {
153: try {
154: timer.schedule(task, delay, period);
155: } catch (IllegalStateException ex) {
156: log.error("Cannot schedule task!", ex);
157: reloadTimer();
158: }
159: }
160: }
161: }
|