[jboss-cvs] JBossRemoting/src/main/org/jboss/remoting/util ...

Ron Sigal ron_sigal at yahoo.com
Fri Jan 19 03:39:21 EST 2007


  User: rsigal  
  Date: 07/01/19 03:39:21

  Modified:    src/main/org/jboss/remoting/util  Tag: remoting_2_x
                        TimerUtil.java
  Log:
  JBREM-676:  Added handling for StoppableTimerTasks.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.4.4.1   +47 -0     JBossRemoting/src/main/org/jboss/remoting/util/TimerUtil.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TimerUtil.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/src/main/org/jboss/remoting/util/TimerUtil.java,v
  retrieving revision 1.4
  retrieving revision 1.4.4.1
  diff -u -b -r1.4 -r1.4.4.1
  --- TimerUtil.java	22 Jun 2006 17:27:10 -0000	1.4
  +++ TimerUtil.java	19 Jan 2007 08:39:21 -0000	1.4.4.1
  @@ -1,8 +1,12 @@
   package org.jboss.remoting.util;
   
  +import java.util.ArrayList;
  +import java.util.Iterator;
   import java.util.Timer;
   import java.util.TimerTask;
   
  +import org.apache.log4j.Logger;
  +
   
   /**
    * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
  @@ -10,6 +14,8 @@
   public class TimerUtil
   {
      private static Timer timer = null;
  +   private static ArrayList stoppableTasks = new ArrayList();
  +   private static Logger log = Logger.getLogger(TimerUtil.class);
      
      private static synchronized void init()
      {
  @@ -23,10 +29,37 @@
            TimerUtil.init();
         }
   
  +      if (task instanceof StoppableTimerTask)
  +      {
  +         stoppableTasks.add(task);
  +      }
  +
         //schedule at fixed delay (not rate)
         TimerUtil.timer.schedule(task, period, period);
      }
   
  +   public static synchronized void unschedule(TimerTask task)
  +   {
  +      if (!(task instanceof StoppableTimerTask))
  +      {
  +         log.warn("TimerUtil only remembers StoppableTimerTasks");
  +         return;
  +      }
  +      
  +      StoppableTimerTask stoppableTask = (StoppableTimerTask) task;
  +      if (!stoppableTasks.remove(stoppableTask))
  +         log.warn("unrecognized StoppableTimerTask: " + task);
  +      
  +      try
  +      {
  +         stoppableTask.stop();
  +      }
  +      catch (Exception e)
  +      {
  +         log.warn("error calling stop() on: " + stoppableTask, e);
  +      }
  +   }
  +
      public static synchronized void destroy()
      {
         if (TimerUtil.timer != null)
  @@ -34,5 +67,19 @@
            TimerUtil.timer.cancel();
            TimerUtil.timer = null;
         }
  +      
  +      Iterator it = new ArrayList(stoppableTasks).iterator();
  +      while (it.hasNext())
  +      {
  +         StoppableTimerTask task = (StoppableTimerTask) it.next();
  +         try
  +         {
  +            task.stop();
  +         }
  +         catch (Exception e)
  +         {
  +            log.warn("unable to stop TimerTask: " + task);
  +         }
  +      }
      }
   }
  
  
  



More information about the jboss-cvs-commits mailing list