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