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.EntityBean ; 35 import javax.ejb.EntityContext ; 36 import javax.ejb.FinderException ; 37 import javax.ejb.RemoveException ; 38 import javax.ejb.TimedObject ; 39 import javax.ejb.Timer ; 40 import javax.ejb.TimerService ; 41 42 import org.jboss.logging.Logger; 43 44 47 public class TimerEntityBean 48 implements EntityBean , TimedObject 49 { 50 private static Logger log = Logger.getLogger(TimerEntityBean.class); 51 52 private EntityContext context; 53 54 private int callCount; 56 57 private Principal ejbTimeoutCaller; 59 60 63 public void createTimer(long duration, long periode, Serializable info) 64 { 65 TimerService timerService = context.getTimerService(); 66 if (periode > 0) 67 timerService.createTimer(duration, periode, info); 68 else 69 timerService.createTimer(duration, info); 70 } 71 72 75 public void cancelFirstTimer() 76 { 77 TimerService timerService = context.getTimerService(); 78 if (timerService.getTimers().isEmpty()) 79 throw new EJBException ("There are no timers"); 80 81 Timer timer = (Timer )timerService.getTimers().iterator().next(); 82 timer.cancel(); 83 } 84 85 89 public Object createTimerReturnHandle(long duration) 90 { 91 TimerService timerService = context.getTimerService(); 92 Timer timer = timerService.createTimer(duration, null); 93 return timer.getHandle(); 94 } 95 96 100 public String passTimerHandle(Object handle) 101 { 102 return handle.toString(); 103 } 104 105 108 public void resetCallCount() 109 { 110 callCount = 0; 111 } 112 113 116 public int getCallCount() 117 { 118 Object pk = context.getPrimaryKey(); 119 log.info("getCallCount [pk=" + pk + ",count=" + callCount + ",this=" + this + "]"); 120 return callCount; 121 } 122 123 126 public List getTimers() 127 { 128 TimerService timerService = context.getTimerService(); 129 130 ArrayList handles = new ArrayList (); 131 Iterator it = timerService.getTimers().iterator(); 132 while (it.hasNext()) 133 { 134 Timer timer = (Timer ) it.next(); 135 handles.add(timer.getHandle().toString()); 136 } 137 return handles; 138 } 139 140 143 public Principal getEjbTimeoutCaller() 144 { 145 return ejbTimeoutCaller; 146 } 147 148 public void ejbTimeout(Timer timer) 149 { 150 callCount++; 151 Object pk = context.getPrimaryKey(); 152 log.info("ejbTimeout [pk=" + pk + ",count=" + callCount + ",this=" + this + "] timer=" + timer); 153 154 ejbTimeoutCaller = context.getCallerPrincipal(); 155 156 if (timer.getInfo() != null) 157 { 158 Properties props = (Properties )timer.getInfo(); 159 if ("true".equals(props.getProperty("rollback"))) 160 { 161 props.setProperty("rollback", "false"); 162 throw new IllegalStateException ("rollback on ejbTimeout"); 163 } 164 } 165 } 166 167 171 174 public Integer ejbCreate(Integer pk) throws CreateException 175 { 176 log.info("ejbCreate [pk=" + pk + "]"); 177 return pk; 178 } 179 180 public void ejbPostCreate(Integer pk) throws CreateException 181 { 182 } 183 184 187 public Integer ejbFindByPrimaryKey(Integer pk) throws FinderException 188 { 189 return pk; 190 } 191 192 public void ejbActivate() throws EJBException , RemoteException 193 { 194 } 195 196 public void ejbLoad() throws EJBException , RemoteException 197 { 198 } 199 200 public void ejbPassivate() throws EJBException , RemoteException 201 { 202 } 203 204 public void ejbRemove() throws RemoveException , EJBException , RemoteException 205 { 206 } 207 208 public void ejbStore() throws EJBException , RemoteException 209 { 210 } 211 212 public void setEntityContext(EntityContext ctx) throws EJBException , RemoteException 213 { 214 this.context = ctx; 215 } 216 217 public void unsetEntityContext() throws EJBException , RemoteException 218 { 219 } 220 } 221 | Popular Tags |