~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/util/TimerUtil.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.remoting.util;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Iterator;
 
5
import java.util.Timer;
 
6
import java.util.TimerTask;
 
7
 
 
8
import org.apache.log4j.Logger;
 
9
 
 
10
 
 
11
/**
 
12
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
13
 */
 
14
public class TimerUtil
 
15
{
 
16
   private static Timer timer = null;
 
17
   private static ArrayList stoppableTasks = new ArrayList();
 
18
   private static Logger log = Logger.getLogger(TimerUtil.class);
 
19
   
 
20
   private static synchronized void init()
 
21
   {
 
22
      TimerUtil.timer = new Timer(true);
 
23
   }
 
24
 
 
25
   public static synchronized void schedule(TimerTask task, long period)
 
26
   {
 
27
      if (TimerUtil.timer == null)
 
28
      {
 
29
         TimerUtil.init();
 
30
      }
 
31
      
 
32
      if (task instanceof StoppableTimerTask)
 
33
      {
 
34
         stoppableTasks.add(task);
 
35
      }
 
36
 
 
37
      //schedule at fixed delay (not rate)
 
38
      try
 
39
      {
 
40
         TimerUtil.timer.schedule(task, period, period);
 
41
      }
 
42
      catch (IllegalStateException e)
 
43
      {
 
44
         log.debug("Unable to schedule TimerTask on existing Timer", e);
 
45
         timer = new Timer(true);
 
46
         timer.schedule(task, period, period);
 
47
      }
 
48
   }
 
49
 
 
50
   public static synchronized void unschedule(TimerTask task)
 
51
   {
 
52
      if (!(task instanceof StoppableTimerTask))
 
53
      {
 
54
         log.warn("TimerUtil only remembers StoppableTimerTasks");
 
55
         return;
 
56
      }
 
57
      
 
58
      StoppableTimerTask stoppableTask = (StoppableTimerTask) task;
 
59
      if (!stoppableTasks.remove(stoppableTask))
 
60
         log.warn("unrecognized StoppableTimerTask: " + task);
 
61
      
 
62
      try
 
63
      {
 
64
         stoppableTask.stop();
 
65
      }
 
66
      catch (Exception e)
 
67
      {
 
68
         log.warn("error calling stop() on: " + stoppableTask, e);
 
69
      }
 
70
   }
 
71
 
 
72
   public static synchronized void destroy()
 
73
   {
 
74
      if (TimerUtil.timer != null)
 
75
      {
 
76
         TimerUtil.timer.cancel();
 
77
         TimerUtil.timer = null;
 
78
      }
 
79
      
 
80
      Iterator it = new ArrayList(stoppableTasks).iterator();
 
81
      while (it.hasNext())
 
82
      {
 
83
         StoppableTimerTask task = (StoppableTimerTask) it.next();
 
84
         try
 
85
         {
 
86
            task.stop();
 
87
         }
 
88
         catch (Exception e)
 
89
         {
 
90
            log.warn("unable to stop TimerTask: " + task);
 
91
         }
 
92
      }
 
93
   }
 
94
}