1 10 11 package org.mule.util.timer; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.TimerTask ; 16 17 25 public class EventTimerTask extends TimerTask 26 { 27 30 private List listeners = null; 31 32 35 private String name = null; 36 37 40 private boolean started = true; 41 42 47 public EventTimerTask(TimeEventListener listener) 48 { 49 super(); 50 addListener(listener); 51 this.name = "EventTimerTask." + hashCode(); 52 } 53 54 60 public EventTimerTask(TimeEventListener listener, String name) 61 { 62 super(); 63 addListener(listener); 64 this.name = name; 65 } 66 67 71 public void run() 72 { 73 74 TimeEvent event = new TimeEvent(this, getName(), scheduledExecutionTime()); 75 fireTimerEvent(event); 76 } 77 78 83 public String getName() 84 { 85 return name; 86 } 87 88 public void removeListener(TimeEventListener listener) 89 { 90 if (listeners != null && listeners.contains(listener)) 91 { 92 listeners.remove(listener); 93 } 94 } 95 96 public void removeAllListeners() 97 { 98 listeners = new ArrayList (); 99 } 100 101 public void addListener(TimeEventListener listener) 102 { 103 if (listeners == null) 104 { 105 listeners = new ArrayList (); 106 listeners.add(listener); 107 } 108 else if (!listeners.contains(listener)) 109 { 110 listeners.add(listener); 111 } 112 } 113 114 protected void fireTimerEvent(TimeEvent event) 115 { 116 if (listeners != null && started) 117 { 118 int count = listeners.size(); 119 for (int i = 0; i < count; i++) 120 { 121 ((TimeEventListener)listeners.get(i)).timeExpired(event); 122 } 123 } 124 } 125 126 public void stop() 127 { 128 started = false; 129 } 130 131 public void start() 132 { 133 started = true; 134 } 135 136 public boolean isStarted() 137 { 138 return started; 139 } 140 } 141 | Popular Tags |