1 Reply Latest reply on Mar 10, 2009 5:39 AM by John smith

    How can i stop ejb timer ???

    John smith Newbie


      i have the following problem:

      I can not kill an ejb timer after the call unregisterTimer method, and the timer is still active even after the ejb is undeployed.

      the source code of my bean is here under :


      package com.test.timer;

      import javax.annotation.PreDestroy;
      import javax.annotation.Resource;
      import javax.ejb.EJBException;
      import javax.ejb.Local;
      import javax.ejb.NoSuchObjectLocalException;
      import javax.ejb.Remote;
      import javax.ejb.SessionContext;
      import javax.ejb.Stateless;
      import javax.ejb.Timeout;
      import javax.ejb.Timer;
      import javax.ejb.TimerHandle;
      import javax.ejb.TimerService;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.TransactionAttributeType;
      import javax.ejb.TransactionManagement;
      import javax.ejb.TransactionManagementType;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      import org.apache.log4j.Logger;

      @Local( ITimerManagerLocal.class)
      @Stateless( name = "NotifyExpiration", mappedName="NotifyExpirationRemote")
      public class TimerManagerBean implements ITimerManagerLocal, ITimerManagerRemote {

      private static final Logger logger = Logger.getLogger ( "EXPIRATION_SERVICE");

      private SessionContext sessionContext;

      private TimerService timerService;

      // Methods
      public void unregisterTimer() throws NamingException {

      logger.info( "NOTIFY TIMEOUT UNREGISTER");

      Context ctx = new InitialContext();

      try {
      TimerHandle handle = (TimerHandle) ctx.lookup( "TIMER_JNDI_NAME");
      if (handle != null){
      try {
      Timer expirationTimer = handle.getTimer();
      if (expirationTimer != null){
      } catch (NoSuchObjectLocalException e) {
      logger.warn( "UNABLE TO UNREGISTER TIME OUT", e);
      } catch (IllegalStateException e) {
      logger.warn( "UNABLE TO UNREGISTER TIME OUT", e);
      } catch (EJBException e) {
      logger.warn( "UNABLE TO UNREGISTER TIME OUT" , e);
      } catch (Throwable thre){
      logger.warn( "UNABLE TO UNREGISTER TIME OUT", thre);

      public void registerTimer() throws NamingException {
      int delai = 10 * 1000;
      Timer expirationTimer = createTimer( delai);
      Context ctx = new InitialContext();
      ctx.bind("TIMER_JNDI_NAME", expirationTimer.getHandle());
      if (logger.isInfoEnabled()){
      logger.info("TIMER REGISTERED " + expirationTimer.getNextTimeout().getTime());


      private Timer createTimer( int delai)
      try {
      return sessionContext.getTimerService().createTimer(delai, delai, null);
      } catch( Throwable thr) {
      return timerService.createTimer (delai, delai, null);

      public void timedOut(Timer obj)
      try {
      if (logger.isDebugEnabled()){
      String.format("TIMER WAKEUP at: " + System.currentTimeMillis()));

      logger.debug("do something...");
      }catch (RuntimeException e) {
      logger.debug( "TIMER WAKEUP FAILS");


      so first i register the timer by calling registerTimer() method, and i wont to kill it by calling unregisterTimer() method. but this does not work, and the timer remains active even after the bean is undeployed.

      If somme one has an idea or workaround to solve this problem ?

      Thank's a lot.