1 22 package org.jboss.test.txtimer.test; 23 24 import javax.ejb.EJBException ; 25 import javax.ejb.TimedObject ; 26 import javax.ejb.Timer ; 27 import javax.transaction.Transaction ; 28 import javax.transaction.TransactionManager ; 29 30 import org.jboss.ejb.txtimer.TimerImpl; 31 import org.jboss.logging.Logger; 32 import org.jboss.tm.TransactionManagerLocator; 33 34 40 public class TimedMockObject implements TimedObject 41 { 42 protected static Logger log = Logger.getLogger(TimedMockObject.class); 44 45 protected static TransactionManager transactionManager = TransactionManagerLocator.getInstance().locate(); 46 47 private int callCount; 48 49 public int getCallCount() 50 { 51 return callCount; 52 } 53 54 57 public void ejbTimeout(Timer timer) 58 { 59 registerTimerWithTx(timer); 61 62 callCount ++; 63 log.debug("ejbTimeout: " + callCount + ",txtimer=" + timer); 64 65 commitTx(); 67 } 68 69 72 private void registerTimerWithTx(Timer timer) 73 { 74 try 75 { 76 Transaction tx = transactionManager.getTransaction(); 77 if (tx == null) 78 { 79 transactionManager.begin(); 80 tx = transactionManager.getTransaction(); 81 } 82 83 TimerImpl txtimer = (TimerImpl) timer; 84 tx.registerSynchronization(txtimer); 85 } 86 catch (Exception e) 87 { 88 throw new EJBException ("Cannot register timer with Tx", e); 89 } 90 } 91 92 95 private void commitTx() 96 { 97 try 98 { 99 Transaction tx = transactionManager.getTransaction(); 100 tx.commit(); 101 } 102 catch (Exception e) 103 { 104 throw new EJBException ("Cannot commit Tx", e); 105 } 106 } 107 } 108 | Popular Tags |