We have to implement EJB 3.0 timers and we took the following approach:
- we created @Stateless beans and injected the TimerService with the @Resource annotation
- we have implemented a servlet which calls an initialization method during startup
The following snipets should be able to give you a more clear picture:
The timer implementation:
public class TimerFacade {
@Resource
protected TimerService timerService;
public void createTimer() {
timerService.createTimer(startTime, intervall, ident);
}
}
The init servlet:
public class InitServlet extends HttpServlet {
@EJB
private transient ITimerFacade timerFacade;
@Override
public void init(final ServletConfig config) throws ServletException {
timerFacade.createTimer();
}
}
After a (re-)deployment everything is fine. But after a restart it happens that the glassfish (2.1) gives us following message:
Rescheduling missed expiration for periodic timer ...
How could we avoid this behaviour and how could we guarantee that the timer is only started once?
We have to tried to make a stateless bean with a timer service injected to let us give the currrently available timers. But the timer which will be rescheduled from the container doesn't come up in this list cause the container reschedules the persisted timers at the end of initialization of the whole app.
I had similar problem.... to ensure ObjectTimer run one at time i used sort of semaphore, a static boolean variable ENABLE_TIMER wich i use in @Timeout method:
at the beginnig of timerMethod() I set variable ENABLE_TIMER to false, at the end ENABLE_TIMER is set to true