EJB 3.0 timer bean timeout called multiple times in glassfish

2.2k views Asked by At

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.

2

There are 2 answers

0
pikimota On

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:

@Timeout
public void timerMethod(Timer timer) {
    try{
    if (ObjectModelSessionBean.ENABLE_TIMER ) {
        timerMethod();
    } else {
        LOGGER.info("timer is temporary disabled - waiting....);
    }
    }catch (Exception ex){
         LOGGER.severe("- Eccezione.   "+ex.toString());
    }
}

at the beginnig of timerMethod() I set variable ENABLE_TIMER to false, at the end ENABLE_TIMER is set to true

6
Aviram Segal On

EJB Timers are persistent, which means if you did not cancel the timer it will continue working once the server is up.

You should cancel the timer either when the application is shutting down (not sure how you do that in glassfish) or before you create a new timer check if that timer already exists.