1
package org.jboss.remoting.util;
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.Timer;
6
import java.util.TimerTask;
8
import org.apache.log4j.Logger;
12
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
14
public class TimerUtil
16
private static Timer timer = null;
17
private static ArrayList stoppableTasks = new ArrayList();
18
private static Logger log = Logger.getLogger(TimerUtil.class);
20
private static synchronized void init()
22
TimerUtil.timer = new Timer(true);
25
public static synchronized void schedule(TimerTask task, long period)
27
if (TimerUtil.timer == null)
32
if (task instanceof StoppableTimerTask)
34
stoppableTasks.add(task);
37
//schedule at fixed delay (not rate)
40
TimerUtil.timer.schedule(task, period, period);
42
catch (IllegalStateException e)
44
log.debug("Unable to schedule TimerTask on existing Timer", e);
45
timer = new Timer(true);
46
timer.schedule(task, period, period);
50
public static synchronized void unschedule(TimerTask task)
52
if (!(task instanceof StoppableTimerTask))
54
log.warn("TimerUtil only remembers StoppableTimerTasks");
58
StoppableTimerTask stoppableTask = (StoppableTimerTask) task;
59
if (!stoppableTasks.remove(stoppableTask))
60
log.warn("unrecognized StoppableTimerTask: " + task);
68
log.warn("error calling stop() on: " + stoppableTask, e);
72
public static synchronized void destroy()
74
if (TimerUtil.timer != null)
76
TimerUtil.timer.cancel();
77
TimerUtil.timer = null;
80
Iterator it = new ArrayList(stoppableTasks).iterator();
83
StoppableTimerTask task = (StoppableTimerTask) it.next();
90
log.warn("unable to stop TimerTask: " + task);