1 22 package org.jboss.ejb.txtimer; 23 24 26 import java.io.Serializable ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Date ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import javax.ejb.EJBException ; 35 import javax.ejb.Timer ; 36 import javax.ejb.TimerHandle ; 37 import javax.ejb.TimerService ; 38 import javax.transaction.SystemException ; 39 import javax.transaction.Transaction ; 40 import javax.transaction.TransactionManager ; 41 42 import org.jboss.logging.Logger; 43 44 55 public class TimerServiceImpl implements TimerService 56 { 57 private static Logger log = Logger.getLogger(TimerServiceImpl.class); 59 60 private TransactionManager transactionManager; 62 private PersistencePolicy persistencePolicy; 64 private TimerIdGenerator timerIdGenerator; 66 private RetryPolicy retryPolicy; 68 69 private TimedObjectId timedObjectId; 71 private TimedObjectInvoker timedObjectInvoker; 73 74 private Map timers = new HashMap (); 76 77 79 84 public TimerServiceImpl( 85 TimedObjectId timedObjectId, TimedObjectInvoker timedObjectInvoker, 86 TransactionManager transactionManager, PersistencePolicy persistencePolicy, 87 RetryPolicy retryPolicy, TimerIdGenerator timerIdGenerator) 88 { 89 this.timedObjectId = timedObjectId; 90 this.timedObjectInvoker = timedObjectInvoker; 91 this.transactionManager = transactionManager; 92 this.persistencePolicy = persistencePolicy; 93 this.timerIdGenerator = timerIdGenerator; 94 this.retryPolicy = retryPolicy; 95 } 96 97 99 102 public Collection getAllTimers() 103 { 104 synchronized (timers) 105 { 106 return new ArrayList (timers.values()); 107 } 108 } 109 110 113 public Timer getTimer(TimerHandle handle) 114 { 115 TimerImpl timer = (TimerImpl)timers.get(handle); 116 if (timer != null && timer.isActive()) 117 return timer; 118 else 119 return null; 120 } 121 122 127 public void shutdown(boolean keepState) 128 { 129 synchronized (timers) 130 { 131 Iterator it = timers.values().iterator(); 132 while (it.hasNext()) 133 { 134 TimerImpl timer = (TimerImpl)it.next(); 135 timer.stopTimer(); 136 137 if (keepState == false) 138 persistencePolicy.deleteTimer(timer.getTimerId(), timer.getTimedObjectId()); 139 } 140 timers.clear(); 141 } 142 } 143 144 147 public TimedObjectInvoker getTimedObjectInvoker() 148 { 149 return timedObjectInvoker; 150 } 151 152 154 166 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 167 { 168 if (duration < 0) 169 throw new IllegalArgumentException ("duration is negative"); 170 171 return createTimer(new Date (System.currentTimeMillis() + duration), 0, info); 172 } 173 174 195 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 196 { 197 if (initialDuration < 0) 198 throw new IllegalArgumentException ("initial duration is negative"); 199 if (intervalDuration < 0) 200 throw new IllegalArgumentException ("interval duration is negative"); 201 202 return createTimer(new Date (System.currentTimeMillis() + initialDuration), intervalDuration, info); 203 } 204 205 217 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 218 { 219 if (expiration == null) 220 throw new IllegalArgumentException ("expiration is null"); 221 222 return createTimer(expiration, 0, info); 223 } 224 225 245 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException , IllegalStateException , EJBException 246 { 247 if (initialExpiration == null) 248 throw new IllegalArgumentException ("initial expiration is null"); 249 if (intervalDuration < 0) 250 throw new IllegalArgumentException ("interval duration is negative"); 251 252 try 253 { 254 String timerId = timerIdGenerator.nextTimerId(); 255 TimerImpl timer = new TimerImpl(this, timerId, timedObjectId, timedObjectInvoker, info); 256 persistencePolicy.insertTimer(timerId, timedObjectId, initialExpiration, intervalDuration, info); 257 timer.startTimer(initialExpiration, intervalDuration); 258 return timer; 259 } 260 catch (Exception e) 261 { 262 log.error("Cannot create txtimer", e); 263 return null; 264 } 265 } 266 267 275 public Collection getTimers() throws IllegalStateException , EJBException 276 { 277 ArrayList activeTimers = new ArrayList (); 278 synchronized (timers) 279 { 280 Iterator it = timers.values().iterator(); 281 while (it.hasNext()) 282 { 283 TimerImpl timer = (TimerImpl)it.next(); 284 if (timer.isActive()) 285 activeTimers.add(timer); 286 } 287 } 288 return activeTimers; 289 } 290 291 293 296 Transaction getTransaction() 297 { 298 try 299 { 300 return transactionManager.getTransaction(); 301 } 302 catch (SystemException e) 303 { 304 return null; 305 } 306 } 307 308 311 void addTimer(TimerImpl txtimer) 312 { 313 synchronized (timers) 314 { 315 TimerHandle handle = new TimerHandleImpl(txtimer); 316 timers.put(handle, txtimer); 317 } 318 } 319 320 323 void removeTimer(TimerImpl txtimer) 324 { 325 synchronized (timers) 326 { 327 persistencePolicy.deleteTimer(txtimer.getTimerId(), txtimer.getTimedObjectId()); 328 timers.remove(new TimerHandleImpl(txtimer)); 329 } 330 } 331 332 void retryTimeout(TimerImpl txtimer) 333 { 334 try 335 { 336 retryPolicy.retryTimeout(timedObjectInvoker, txtimer); 337 } 338 catch (Exception e) 339 { 340 log.error("Retry timeout failed for timer: " + txtimer, e); 341 } 342 } 343 } 344 | Popular Tags |