1 22 package org.jboss.test.timer.ejb; 23 24 import java.util.Date ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.List ; 28 import java.io.*; 29 30 import javax.ejb.SessionBean ; 31 import javax.ejb.SessionContext ; 32 import javax.ejb.TimedObject ; 33 import javax.ejb.Timer ; 34 import javax.ejb.TimerHandle ; 35 import javax.ejb.TimerService ; 36 import javax.ejb.EJBException ; 37 import javax.ejb.NoSuchObjectLocalException ; 38 import javax.management.MBeanServerFactory ; 39 import javax.management.MBeanServer ; 40 41 import org.jboss.logging.Logger; 42 import org.jboss.test.timer.interfaces.TimerSLSB; 43 import org.jboss.ejb.txtimer.FixedDelayRetryPolicyMBean; 44 45 60 public class TimerSLSBean 61 implements SessionBean , TimedObject 62 { 63 private static HashMap timeoutCounts = new HashMap (); 67 private static Logger log = Logger.getLogger(TimerSLSBean.class); 68 69 private SessionContext context; 73 74 78 86 public byte[] startSingleTimer(long pPeriod) 87 { 88 return startSingleTimer(pPeriod,"TimerSLSBean.startSingleTimer"); 89 } 90 91 99 public byte[] startSingleTimer(long pPeriod, Serializable info) 100 { 101 log.info("TimerSLSBean.startSingleTimer(), try to get a Timer Service from the Session Context"); 102 TimerService ts = context.getTimerService(); 103 long exp = System.currentTimeMillis() + pPeriod; 104 Timer timer = ts.createTimer(new Date (exp), info); 105 log.info("TimerSLSBean.startSingleTimer(), create a timer: "+timer); 106 byte[] handle = getHandle(timer); 107 return handle; 108 } 109 110 119 public byte[] startTimer(long pPeriod) 120 { 121 return startTimer(pPeriod, "TimerSLSBean.startTimer"); 122 } 123 124 133 public byte[] startTimer(long pPeriod, Serializable info) 134 { 135 log.info("TimerSLSBean.startTimer(), try to get a Timer Service from the Session Context"); 136 TimerService ts = context.getTimerService(); 137 long exp = System.currentTimeMillis() + pPeriod; 138 Timer timer = ts.createTimer(new Date (exp), pPeriod, info); 139 log.info("TimerSLSBean.startTimer(), create a timer: "+timer); 140 byte[] handle = getHandle(timer); 141 return handle; 142 } 143 144 147 public void stopTimer(byte[] handle) 148 { 149 Timer timer = getTimer(handle); 150 timer.cancel(); 151 log.info("TimerSLSBean.stopTimer(), create a timer: "+timer); 152 synchronized( TimerSLSBean.class ) 153 { 154 Long key = getKey(handle); 155 timeoutCounts.remove(key); 156 } 157 } 158 159 162 public int getTimeoutCount(byte[] handle) 163 { 164 Integer count = null; 165 try 166 { 167 Long key = getKey(handle); 168 count = (Integer ) timeoutCounts.get(key); 169 } 170 catch(NoSuchObjectLocalException e) 171 { 172 } 174 log.info("TimerSLSBean.getTimeoutCount(): " + count); 175 return count != null ? count.intValue() : 0; 176 } 177 178 183 public Date getNextTimeout(byte[] handle) 184 { 185 Timer timer = getTimer(handle); 186 return timer.getNextTimeout(); 187 } 188 189 194 public long getTimeRemaining(byte[] handle) 195 { 196 Timer timer = getTimer(handle); 197 return timer.getTimeRemaining(); 198 } 199 200 205 public Object getInfo(byte[] handle) 206 { 207 Timer timer = getTimer(handle); 208 return timer.getInfo(); 209 } 210 211 216 public void ejbCreate() 217 { 218 log.info("TimerSLSBean.ejbCreate()"); 219 } 220 221 public void ejbTimeout(Timer timer) 222 { 223 Integer count = null; 224 Long key = null; 225 synchronized( TimerSLSBean.class ) 226 { 227 log.debug("ejbTimeout(): Timer State:" + timer); 228 byte[] handle = getHandle(timer); 229 key = getKey(handle); 230 count = (Integer ) timeoutCounts.get(key); 231 if( count == null ) 232 count = new Integer (1); 233 else 234 count = new Integer (1 + count.intValue()); 235 timeoutCounts.put(key, count); 236 log.info("ejbTimeout(): count for timer handle " + key + " is " + count); 237 } 238 239 log.info("ejbTimeout(), timer: " + timer+", key: "+key+", count: "+count); 240 241 Object info = timer.getInfo(); 242 if(info instanceof Map ) { 243 Map mInfo = ((Map )info); 244 Integer failCount = (Integer ) mInfo.get(TimerSLSB.INFO_EXEC_FAIL_COUNT); 245 Integer taskTime = (Integer ) mInfo.get(TimerSLSB.INFO_TASK_RUNTIME); 246 247 if(failCount != null && count.compareTo(failCount) <= 0) { 251 log.info("ejbTimeout(): Failing timeout because '" + TimerSLSB.INFO_EXEC_FAIL_COUNT 252 + "' is set to " + failCount + " and count is " + count); 253 context.setRollbackOnly(); 254 return; 255 } 256 257 if(taskTime != null) { 260 try 261 { 262 log.info("ejbTimeout(): Simulating long task ("+ taskTime +"ms)"); 263 Thread.sleep(taskTime.intValue()); 264 } 265 catch (InterruptedException e) {} 266 } 267 } 268 } 269 270 275 public String toString() 276 { 277 return "TimerSLSBean [ " + " ]"; 278 } 279 280 284 public void setSessionContext(SessionContext aContext) 285 { 286 context = aContext; 287 } 288 289 public void ejbActivate() 290 { 291 } 292 293 public void ejbPassivate() 294 { 295 } 296 297 public void ejbRemove() 298 { 299 } 300 301 private Long getKey(byte[] handle) 302 { 303 long key = 0; 304 for(int n = 0; n < handle.length; n ++) 305 key += handle[n]; 306 log.info("HandleKey: "+key); 307 return new Long (key); 308 } 309 private byte[] getHandle(Timer timer) 310 throws EJBException 311 { 312 try 313 { 314 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 315 ObjectOutputStream oos = new ObjectOutputStream(baos); 316 oos.writeObject(timer.getHandle()); 317 oos.close(); 318 byte[] handle = baos.toByteArray(); 319 return handle; 320 } 321 catch (Exception e) 322 { 323 throw new EJBException ("Failed to get timer from handle", e); 324 } 325 } 326 private Timer getTimer(byte[] handle) 327 throws NoSuchObjectLocalException , EJBException 328 { 329 try 330 { 331 ByteArrayInputStream bais = new ByteArrayInputStream(handle); 332 ObjectInputStream ois = new ObjectInputStream(bais); 333 TimerHandle th = null; 334 th = (TimerHandle ) ois.readObject(); 335 ois.close(); 336 Timer timer = th.getTimer(); 337 return timer; 338 } 339 catch(NoSuchObjectLocalException e) 340 { 341 throw e; 342 } 343 catch (Exception e) 344 { 345 throw new EJBException ("Failed to get timer from handle", e); 346 } 347 } 348 349 353 public long getRetryTimeoutPeriod() { 354 List lServers = MBeanServerFactory.findMBeanServer( null ); 355 MBeanServer lServer = (MBeanServer ) lServers.get( 0 ); 356 try 357 { 358 Long val = (Long ) lServer.getAttribute(FixedDelayRetryPolicyMBean.OBJECT_NAME, "Delay"); 359 return val.longValue(); 360 } 361 catch (Exception e) 362 { 363 log.error(e); 364 e.printStackTrace(); 365 366 return -1; 367 } 368 } 369 } 370 | Popular Tags |