1 17 package org.apache.servicemix.timers.impl; 18 19 import java.util.Date ; 20 import java.util.TimerTask ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.servicemix.timers.Timer; 25 import org.apache.servicemix.timers.TimerListener; 26 import org.apache.servicemix.timers.TimerManager; 27 28 public class TimerManagerImpl implements TimerManager { 29 30 private static final Log log = LogFactory.getLog(TimerManagerImpl.class); 31 private java.util.Timer timer; 32 33 public Timer schedule(TimerListener listener, long delay) { 34 if (log.isDebugEnabled()) { 35 log.debug("Schedule timer " + listener + " for " + delay); 36 } 37 TimerImpl tt = new TimerImpl(listener); 38 timer.schedule(tt, delay); 39 return tt; 40 } 41 42 public Timer schedule(TimerListener listener, Date date) { 43 if (log.isDebugEnabled()) { 44 log.debug("Schedule timer " + listener + " at " + date); 45 } 46 TimerImpl tt = new TimerImpl(listener); 47 timer.schedule(tt, date); 48 return tt; 49 } 50 51 public void start() { 52 timer = new java.util.Timer (); 53 } 54 55 public void stop() { 56 timer.cancel(); 57 } 58 59 protected static class TimerImpl extends TimerTask implements Timer { 60 61 private TimerListener timerListener; 62 63 public TimerImpl(TimerListener timerListener) { 64 this.timerListener = timerListener; 65 } 66 67 public boolean cancel() { 68 if (log.isDebugEnabled()) { 69 log.debug("Timer " + timerListener + " cancelled"); 70 } 71 return super.cancel(); 72 } 73 74 public TimerListener getTimerListener() { 75 return this.timerListener; 76 } 77 78 public void run() { 79 if (log.isDebugEnabled()) { 80 log.debug("Timer " + timerListener + " expired"); 81 } 82 this.timerListener.timerExpired(this); 83 } 84 85 } 86 87 } 88 | Popular Tags |