--- miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java 2011/04/18 12:28:14 1298 +++ miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java 2011/04/18 14:05:29 1299 @@ -23,17 +23,21 @@ @Stateless public class TimedEjb implements TimedObject { + class TimerInfo implements Serializable { + public int count; + } + @Resource private SessionContext sessionCtx; - int count; public void startTimer() { TimerService timerService = sessionCtx.getTimerService(); + TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer - //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer - timerService.createTimer(10*1000, 5000, null); //repeating timer + //Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer + timerService.createTimer(10*1000, 5000, info ); //repeating timer // otherwise use schedule expression to create more complex schedules // in this case fire once every minute when the seconds is == 10 @@ -41,18 +45,19 @@ //System.out.println(se.toString()); //timerService.createCalendarTimer( se ); - count = 0; } @Override public void ejbTimeout(Timer timer) { + + TimerInfo info = (TimerInfo) timer.getInfo(); - count++; + info.count++; - System.out.print("timeout .... " + count); + System.out.print("timeout .... " + info.count); - if (count >= 10) { + if (info.count >= 10) { timer.cancel(); } }