1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.monitor.jmx;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Timer;
24 import java.util.TimerTask;
25 import java.util.concurrent.TimeUnit;
26
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
29 import org.eclipse.jetty.util.thread.ExecutorThreadPool;
30 import org.eclipse.jetty.util.thread.ThreadPool;
31
32
33
34
35
36
37
38
39
40 public class MonitorTask extends TimerTask
41 {
42 private static final Logger LOG = Log.getLogger(MonitorTask.class);
43
44 private static Timer __timer = new Timer(true);
45 private static ThreadPool _callback = new ExecutorThreadPool(4,64,60,TimeUnit.SECONDS);;
46 private static Map<String,TimerTask> __tasks = new HashMap<String,TimerTask>();
47
48 private final MonitorAction _action;
49
50
51
52
53
54
55
56 private MonitorTask(MonitorAction action)
57 {
58 _action = action;
59 }
60
61
62
63
64
65
66
67 public static void schedule(MonitorAction action)
68 {
69 TimerTask task = new MonitorTask(action);
70 __timer.scheduleAtFixedRate(task,
71 action.getPollDelay(),
72 action.getPollInterval());
73
74 __tasks.put(action.getID(), task);
75 }
76
77
78
79
80
81
82
83 public static void cancel(MonitorAction action)
84 {
85 TimerTask task = __tasks.remove(action.getID());
86 if (task != null)
87 task.cancel();
88 }
89
90
91
92
93
94
95
96
97
98 @Override
99 public final void run()
100 {
101 final long timestamp = System.currentTimeMillis();
102 final EventTrigger trigger = _action.getTrigger();
103
104 _callback.execute(new Runnable() {
105 public void run()
106 {
107 try
108 {
109 if(trigger.match(timestamp))
110 _action.doExecute(timestamp);
111 }
112 catch (Exception ex)
113 {
114 LOG.debug(ex);
115 }
116 }
117 });
118 }
119 }