1 22 package org.jboss.test.txtimer.ejb; 23 24 import java.io.Serializable ; 25 import java.rmi.RemoteException ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import javax.ejb.CreateException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.SessionBean ; 32 import javax.ejb.SessionContext ; 33 import javax.ejb.TimedObject ; 34 import javax.ejb.Timer ; 35 36 import org.jboss.logging.Logger; 37 38 44 public class TimerTestBean implements SessionBean , TimedObject 45 { 46 private Logger log = Logger.getLogger(TimerTestBean.class.getName()); 47 private SessionContext context; 48 49 public TimerTestBean() 50 { 51 } 53 54 public void setSessionContext(SessionContext newContext) throws EJBException 55 { 56 context = newContext; 57 } 58 59 public void ejbCreate() throws CreateException {} 60 public void ejbRemove() throws EJBException , RemoteException {} 61 public void ejbActivate() throws EJBException , RemoteException {} 62 public void ejbPassivate() throws EJBException , RemoteException {} 63 64 public void ejbTimeout(Timer timer) 65 { 66 log.info("ejbTimeout: " + timer); 67 } 68 69 71 76 public void startTimerInTxRequired() throws EJBException 77 { 78 startTimer("Required"); 79 } 80 81 86 public void startTimerInTxRequiresNew() throws EJBException 87 { 88 startTimer("RequiresNew"); 89 } 90 91 96 public void startTimerInTxNever() throws EJBException 97 { 98 startTimer("Never"); 99 } 100 101 106 public void startTimerInTxNotSupported() throws EJBException 107 { 108 startTimer("NotSupported"); 109 } 110 111 114 public int listAllTimers() 115 { 116 Collection timers = context.getTimerService().getTimers(); 117 String s = "Timers: "; 118 for (Iterator it = timers.iterator();it.hasNext();) 119 { 120 Timer t = (Timer )it.next(); 121 s = s + t.toString() + " "; 122 try 123 { 124 s += t.getInfo(); 125 } 126 catch (Exception ignore) 127 { 128 } 130 s += "\n"; 131 } 132 log.info(s); 133 134 return timers.size(); 135 } 136 137 142 public void cancelTimerInTxRequired() throws EJBException 143 { 144 cancelTimers(); 145 } 146 147 152 public void cancelTimerInTxRequiresNew() throws EJBException 153 { 154 cancelTimers(); 155 } 156 157 162 public void cancelTimerInTxNever() throws EJBException 163 { 164 cancelTimers(); 165 } 166 167 172 public void cancelTimerInTxNotSupported() throws EJBException 173 { 174 cancelTimers(); 175 } 176 177 179 private void startTimer(Serializable info) 180 { 181 log.info("Starting timer, info=" + info); 182 context.getTimerService().createTimer(10000, info); 183 } 184 185 private void cancelTimers() 186 { 187 Collection timers = context.getTimerService().getTimers(); 188 for (Iterator it = timers.iterator(); it.hasNext(); ) 189 { 190 Timer t = (Timer )it.next(); 191 log.info("Cancelling timer " + t + " " + t.getInfo()); 192 t.cancel(); 193 log.info("Timer is now " + t); 194 } 195 } 196 197 } 198 | Popular Tags |