1 // ========================================================================
2 // Copyright (c) Webtide LLC
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
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 * MonitorTask
30 *
31 * Invokes polling of the JMX server for the MBean attribute values
32 * through executing timer task scheduled using java.util.Timer
33 * at specified poll interval following a specified delay.
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 * Creates new instance of MonitorTask
48 *
49 * @param action instance of MonitorAction to use
50 */
51 private MonitorTask(MonitorAction action)
52 {
53 _action = action;
54 }
55
56 /* ------------------------------------------------------------ */
57 /**
58 * Schedule new timer task for specified monitor action
59 *
60 * @param action monitor action
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 * Cancel timer task for specified monitor action
75 *
76 * @param action monitor action
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 * This method is invoked when poll interval has elapsed
88 * to check if the event trigger conditions are satisfied
89 * in order to fire event.
90 *
91 * @see java.util.TimerTask#run()
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 }