1 22 package org.jboss.test.txtimer.ejb; 23 24 import java.io.Serializable ; 25 import java.rmi.RemoteException ; 26 import java.security.Principal ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Properties ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.SessionBean ; 35 import javax.ejb.SessionContext ; 36 import javax.ejb.TimedObject ; 37 import javax.ejb.Timer ; 38 import javax.ejb.TimerService ; 39 40 import org.jboss.logging.Logger; 41 42 45 public class TimerSessionBean 46 implements SessionBean , TimedObject 47 { 48 private static Logger log = Logger.getLogger(TimerSessionBean.class); 49 50 private SessionContext context; 51 52 private int callCount; 54 55 private static int globalCallCount; 57 58 private Principal ejbTimeoutCaller; 60 61 62 65 public void createTimer(long duration, long periode, Serializable info) 66 { 67 TimerService timerService = context.getTimerService(); 68 if (periode > 0) 69 timerService.createTimer(duration, periode, info); 70 else 71 timerService.createTimer(duration, info); 72 } 73 74 77 public void cancelFirstTimer() 78 { 79 TimerService timerService = context.getTimerService(); 80 if (timerService.getTimers().isEmpty()) 81 throw new EJBException ("There are no timers"); 82 83 Timer timer = (Timer )timerService.getTimers().iterator().next(); 84 timer.cancel(); 85 } 86 87 91 public Object createTimerReturnHandle(long duration) 92 { 93 TimerService timerService = context.getTimerService(); 94 Timer timer = timerService.createTimer(duration, null); 95 return timer.getHandle(); 96 } 97 98 102 public String passTimerHandle(Object handle) 103 { 104 return handle.toString(); 105 } 106 107 110 public void resetCallCount() 111 { 112 callCount = 0; 113 globalCallCount = 0; 114 } 115 116 119 public int getCallCount() 120 { 121 log.info("getCallCount [count=" + callCount + "]"); 122 return callCount; 123 } 124 125 128 public int getGlobalCallCount() 129 { 130 log.info("getGlobalCallCount [count=" + globalCallCount + "]"); 131 return globalCallCount; 132 } 133 134 137 public List getTimers() 138 { 139 TimerService timerService = context.getTimerService(); 140 141 ArrayList handles = new ArrayList (); 142 Iterator it = timerService.getTimers().iterator(); 143 while (it.hasNext()) 144 { 145 Timer timer = (Timer ) it.next(); 146 handles.add(timer.getHandle().toString()); 147 } 148 return handles; 149 } 150 151 154 public Principal getEjbTimeoutCaller() 155 { 156 return ejbTimeoutCaller; 157 } 158 159 public void ejbTimeout(Timer timer) 160 { 161 callCount++; 162 globalCallCount++; 163 164 log.info("ejbTimeout [count=" + callCount + "] timer=" + timer); 165 166 ejbTimeoutCaller = context.getCallerPrincipal(); 167 log.info("ejbTimeout [callerPrincipal=" + ejbTimeoutCaller + "]"); 168 169 Serializable info = timer.getInfo(); 170 log.info("ejbTimeout [info=" + info + "]"); 171 172 if (info != null) 173 { 174 if (info instanceof Properties ) 175 { 176 Properties props = (Properties )timer.getInfo(); 177 if ("true".equals(props.getProperty("cancel"))) 178 timer.cancel(); 179 } 180 } 181 } 182 183 187 public void setSessionContext(SessionContext ctx) throws EJBException , RemoteException 188 { 189 this.context = ctx; 190 } 191 192 195 public void ejbCreate() throws CreateException 196 { 197 } 198 199 public void ejbRemove() throws EJBException , RemoteException 200 { 201 } 202 203 public void ejbActivate() throws EJBException , RemoteException 204 { 205 } 206 207 public void ejbPassivate() throws EJBException , RemoteException 208 { 209 } 210 } 211 | Popular Tags |