1 package org.jfox.ejb.timer; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.Collections ; 7 import java.util.Date ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.WeakHashMap ; 12 import javax.ejb.EJBException ; 13 import javax.ejb.Timer ; 14 import javax.ejb.TimerService ; 15 16 21 22 public class EJBTimerService implements TimerService { 23 24 29 private Map timerTasks = new WeakHashMap (); 30 private static java.util.Timer timer = new java.util.Timer (); 32 33 private String ejbName; 34 35 public EJBTimerService(String ejbName) { 36 this.ejbName = ejbName; 37 } 38 39 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException { 40 EJBTimerTask ejbTimerTask = new EJBTimerTask(this, info); 41 timer.schedule(ejbTimerTask, duration); 42 timerTasks.put(ejbTimerTask, "" + System.identityHashCode(ejbTimerTask.getTimer())); 43 return ejbTimerTask.getTimer(); 44 } 45 46 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException { 47 EJBTimerTask ejbTimerTask = new EJBTimerTask(this, info); 48 timer.scheduleAtFixedRate(ejbTimerTask, initialDuration, intervalDuration); 49 timerTasks.put(ejbTimerTask, "" + System.identityHashCode(ejbTimerTask.getTimer())); 50 return ejbTimerTask.getTimer(); 51 } 52 53 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException { 54 EJBTimerTask ejbTimerTask = new EJBTimerTask(this, info); 55 timer.schedule(ejbTimerTask, expiration); 56 timerTasks.put(ejbTimerTask, "" + System.identityHashCode(ejbTimerTask.getTimer())); 57 return ejbTimerTask.getTimer(); 58 } 59 60 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException { 61 EJBTimerTask ejbTimerTask = new EJBTimerTask(this, info); 62 timer.scheduleAtFixedRate(ejbTimerTask, initialExpiration, intervalDuration); 63 timerTasks.put(ejbTimerTask, "" + System.identityHashCode(ejbTimerTask.getTimer())); 64 return ejbTimerTask.getTimer(); 65 } 66 67 public synchronized Collection getTimers() throws IllegalStateException , EJBException { 68 List timers = new ArrayList (timerTasks.size()); 69 for(Iterator it = timerTasks.keySet().iterator(); it.hasNext();) { 70 timers.add(((EJBTimerTask) it.next()).getTimer()); 71 } 72 return Collections.unmodifiableList(timers); 73 } 74 75 80 public String getEjbName() { 81 return ejbName; 82 } 83 84 public boolean cancel(EJBTimerTask ejbTimerTask) { 85 return timerTasks.remove(ejbTimerTask) != null; 86 } 87 88 94 public Timer getTimer(String timerIdentity) { 95 for(Iterator it = timerTasks.entrySet().iterator(); it.hasNext();) { 96 Map.Entry entry = (Map.Entry ) it.next(); 97 if(entry.getValue().equals(timerIdentity)) { 98 return ((EJBTimerTask) entry.getKey()).getTimer(); 99 } 100 } 101 return null; 102 } 103 104 public static void main(String [] args) { 105 106 } 107 108 } 109 110 | Popular Tags |