--- miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java 2011/04/18 14:07:49 1300 +++ miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java 2011/04/18 14:39:01 1301 @@ -10,6 +10,7 @@ import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.ejb.TimedObject; +import javax.ejb.Timeout; import javax.ejb.Timer; import javax.ejb.TimerService; @@ -23,12 +24,21 @@ * */ @Stateless -public class TimedEjb implements TimedObject { +public class TimedEjb /*implements TimedObject*/ { + + public static class TimerInfo implements Serializable { + static final long serialVersionUID = 1L; + + private long start = System.currentTimeMillis(); + + public long getStart() { + return start; + } - class TimerInfo implements Serializable { - public int count; } + private int count; //storing shared data like this is not recommended !!! + @Resource private SessionContext sessionCtx; @@ -46,20 +56,25 @@ //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*"); //System.out.println(se.toString()); //timerService.createCalendarTimer( se ); + + count = 0; } - @Override - public void ejbTimeout(Timer timer) { + //@Override if we used TimedObject interface + //public void ejbTimeout(Timer timer) { + + @Timeout + public void myEjbTimeout(Timer timer) { TimerInfo info = (TimerInfo) timer.getInfo(); - info.count++; - - System.out.print("timeout .... " + info.count); + //any updates to the info object is not pushed back to the timer services + System.out.print("timeout .... " + count + " / " + info.getStart() ); + count++; - if (info.count >= 10) { + if (count >= 10) { timer.cancel(); } }