1 22 package org.jboss.ejb.txtimer; 23 24 26 import java.lang.reflect.Constructor ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.ejb.TimerService ; 35 import javax.management.ObjectName ; 36 import javax.transaction.TransactionManager ; 37 38 import org.jboss.ejb.Container; 39 import org.jboss.ejb.ContainerMBean; 40 import org.jboss.logging.Logger; 41 import org.jboss.mx.util.MBeanProxyExt; 42 import org.jboss.system.ServiceMBeanSupport; 43 import org.jboss.tm.TransactionManagerFactory; 44 import org.jboss.tm.TransactionManagerLocator; 45 46 54 public class EJBTimerServiceImpl extends ServiceMBeanSupport 55 implements EJBTimerServiceImplMBean 56 { 57 private static Logger log = Logger.getLogger(EJBTimerServiceImpl.class); 59 60 62 private ObjectName retryPolicyName; 64 private ObjectName persistencePolicyName; 66 private String timerIdGeneratorClassName; 68 private String timedObjectInvokerClassName; 70 private TransactionManagerFactory transactionManagerFactory; 72 73 75 private TransactionManager transactionManager; 77 private RetryPolicy retryPolicy; 79 private PersistencePolicy persistencePolicy; 81 private TimerIdGenerator timerIdGenerator; 83 84 private Map timerServiceMap = Collections.synchronizedMap(new HashMap ()); 86 87 89 94 public ObjectName getRetryPolicy() 95 { 96 return retryPolicyName; 97 } 98 99 104 public void setRetryPolicy(ObjectName retryPolicyName) 105 { 106 this.retryPolicyName = retryPolicyName; 107 } 108 109 114 public ObjectName getPersistencePolicy() 115 { 116 return persistencePolicyName; 117 } 118 119 124 public void setPersistencePolicy(ObjectName persistencePolicyName) 125 { 126 this.persistencePolicyName = persistencePolicyName; 127 } 128 129 134 public String getTimerIdGeneratorClassName() 135 { 136 return timerIdGeneratorClassName; 137 } 138 139 144 public void setTimerIdGeneratorClassName(String timerIdGeneratorClassName) 145 { 146 this.timerIdGeneratorClassName = timerIdGeneratorClassName; 147 } 148 149 154 public String getTimedObjectInvokerClassName() 155 { 156 return timedObjectInvokerClassName; 157 } 158 159 164 public void setTimedObjectInvokerClassName(String timedObjectInvokerClassName) 165 { 166 this.timedObjectInvokerClassName = timedObjectInvokerClassName; 167 } 168 169 172 public void setTransactionManagerFactory(TransactionManagerFactory factory) 173 { 174 this.transactionManagerFactory = factory; 175 } 176 177 179 protected void startService() throws Exception 180 { 181 183 if (transactionManagerFactory != null) 185 transactionManager = transactionManagerFactory.getTransactionManager(); 186 else 187 transactionManager = TransactionManagerLocator.getInstance().locate(); 188 189 try 191 { 192 retryPolicy = (RetryPolicy)MBeanProxyExt.create(RetryPolicy.class, getRetryPolicy(), server); 193 } 194 catch (Exception e) 195 { 196 log.error("Cannot obtain the implementation of a RetryPolicy", e); 197 } 198 199 try 201 { 202 persistencePolicy = (PersistencePolicy)MBeanProxyExt.create(PersistencePolicy.class, persistencePolicyName, server); 203 } 204 catch (Exception e) 205 { 206 log.warn("Cannot obtain the implementation of a PersistencePolicy, using NoopPersistencePolicy: " + e.toString()); 207 persistencePolicy = new NoopPersistencePolicy(); 208 } 209 210 try 212 { 213 Class timerIdGeneratorClass = getClass().getClassLoader().loadClass(timerIdGeneratorClassName); 214 timerIdGenerator = (TimerIdGenerator)timerIdGeneratorClass.newInstance(); 215 } 216 catch (Exception e) 217 { 218 log.warn("Cannot obtain the implementation of a TimerIdGenerator, using BigIntegerTimerIdGenerator: " + e.toString()); 219 timerIdGenerator = new BigIntegerTimerIdGenerator(); 220 } 221 } 222 223 protected void stopService() 224 { 225 transactionManager = null; 227 retryPolicy = null; 228 persistencePolicy = null; 229 timerIdGenerator = null; 230 } 231 232 234 243 public TimerService createTimerService(ObjectName containerId, Object instancePk, Container container) 244 { 245 TimedObjectInvoker invoker = null; 246 try 247 { 248 TimedObjectId timedObjectId = new TimedObjectId(containerId, instancePk); 249 Class invokerClass = getClass().getClassLoader().loadClass(timedObjectInvokerClassName); 250 Constructor constr = invokerClass.getConstructor(new Class []{TimedObjectId.class, Container.class}); 251 invoker = (TimedObjectInvoker)constr.newInstance(new Object []{timedObjectId, container}); 252 } 253 catch (Exception e) 254 { 255 log.error("Cannot create TimedObjectInvoker: " + timedObjectInvokerClassName, e); 256 return null; 257 } 258 259 return createTimerService(containerId, instancePk, invoker); 260 } 261 262 270 public TimerService createTimerService(ObjectName containerId, Object instancePk, TimedObjectInvoker invoker) 271 { 272 TimedObjectId timedObjectId = new TimedObjectId(containerId, instancePk); 273 TimerServiceImpl timerService = (TimerServiceImpl)timerServiceMap.get(timedObjectId); 274 if (timerService == null) 275 { 276 timerService = new TimerServiceImpl(timedObjectId, invoker, 277 transactionManager, persistencePolicy, retryPolicy, timerIdGenerator); 278 log.debug("createTimerService: " + timerService); 279 timerServiceMap.put(timedObjectId, timerService); 280 } 281 return timerService; 282 } 283 284 291 public TimerService getTimerService(ObjectName containerId, Object instancePk) 292 { 293 TimedObjectId timedObjectId = new TimedObjectId(containerId, instancePk); 294 return (TimerServiceImpl)timerServiceMap.get(timedObjectId); 295 } 296 297 307 public void removeTimerService(ObjectName containerId, Object instancePk) 308 { 309 TimedObjectId timedObjectId = new TimedObjectId(containerId, instancePk); 310 if (timedObjectId.getInstancePk() != null) 312 { 313 TimerServiceImpl timerService = (TimerServiceImpl)getTimerService(containerId, instancePk); 314 if (timerService != null) 315 { 316 log.debug("removeTimerService: " + timerService); 317 timerService.shutdown(false); 320 timerServiceMap.remove(timedObjectId); 321 } 322 } 323 else 324 { 325 removeTimerService(containerId, false); 328 } 329 } 330 331 341 public void removeTimerService(ObjectName containerId, boolean keepState) throws IllegalStateException 342 { 343 Iterator it = timerServiceMap.entrySet().iterator(); 345 while (it.hasNext()) 346 { 347 Map.Entry entry = (Map.Entry )it.next(); 348 TimedObjectId key = (TimedObjectId)entry.getKey(); 349 TimerServiceImpl timerService = (TimerServiceImpl)entry.getValue(); 350 if (containerId.equals(key.getContainerId())) 351 { 352 log.debug("removeTimerService: " + timerService); 353 timerService.shutdown(keepState); 354 it.remove(); 355 } 356 } 357 } 358 359 366 public void removeTimerService(ObjectName containerId, Object instancePk, boolean keepState) throws IllegalStateException 367 { 368 TimedObjectId timedObjectId = new TimedObjectId(containerId, instancePk); 370 if (timedObjectId.getInstancePk() != null) 371 { 372 TimerServiceImpl timerService = (TimerServiceImpl)getTimerService(containerId, instancePk); 373 if (timerService != null) 374 { 375 log.debug("removeTimerService: " + timerService); 376 timerService.shutdown(false); 377 timerServiceMap.remove(timedObjectId); 378 } 379 } 380 else 382 { 383 Iterator it = timerServiceMap.entrySet().iterator(); 384 while (it.hasNext()) 385 { 386 Map.Entry entry = (Map.Entry )it.next(); 387 TimedObjectId key = (TimedObjectId)entry.getKey(); 388 TimerServiceImpl timerService = (TimerServiceImpl)entry.getValue(); 389 if (containerId.equals(key.getContainerId())) 390 { 391 log.debug("removeTimerService: " + timerService); 392 timerService.shutdown(keepState); 393 it.remove(); 394 } 395 } 396 } 397 } 398 399 405 public void restoreTimers(ObjectName containerId, ClassLoader loader) throws IllegalStateException 406 { 407 List handles = persistencePolicy.listTimerHandles(containerId, loader); 409 410 if (handles.isEmpty() == false) 411 { 412 for (Iterator i = handles.iterator(); i.hasNext(); ) 414 { 415 TimerHandleImpl handle = (TimerHandleImpl)i.next(); 416 persistencePolicy.deleteTimer(handle.getTimerId(), handle.getTimedObjectId()); 417 } 418 419 for (Iterator i = handles.iterator(); i.hasNext(); ) 423 { 424 TimerHandleImpl handle = (TimerHandleImpl)i.next(); 425 try 426 { 427 TimedObjectId targetId = handle.getTimedObjectId(); 428 ContainerMBean container = (ContainerMBean)MBeanProxyExt.create(ContainerMBean.class, containerId, server); 429 TimerService timerService = container.getTimerService(targetId.getInstancePk()); 430 timerService.createTimer(handle.getFirstTime(), handle.getPeriode(), handle.getInfo()); 431 } 432 catch (Exception e) 433 { 434 log.warn("Unable to restore timer record: " + handle, e); 435 } 436 } 437 } 438 } 439 440 442 447 public String listTimers() 448 { 449 StringBuffer retBuffer = new StringBuffer (); 450 Iterator it = timerServiceMap.entrySet().iterator(); 451 while (it.hasNext()) 452 { 453 Map.Entry entry = (Map.Entry )it.next(); 454 TimedObjectId timedObjectId = (TimedObjectId)entry.getKey(); 455 retBuffer.append(timedObjectId + "\n"); 456 457 TimerServiceImpl timerService = (TimerServiceImpl)entry.getValue(); 458 Collection col = timerService.getAllTimers(); 459 for (Iterator iterator = col.iterator(); iterator.hasNext();) 460 { 461 TimerImpl timer = (TimerImpl)iterator.next(); 462 TimerHandleImpl handle = new TimerHandleImpl(timer); 463 retBuffer.append(" handle: " + handle + "\n"); 464 retBuffer.append(" " + timer + "\n"); 465 } 466 } 467 return retBuffer.toString(); 468 } 469 470 } 471 | Popular Tags |