1 //
2 // ========================================================================
3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
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 * MonitorTask
35 *
36 * Invokes polling of the JMX server for the MBean attribute values
37 * through executing timer task scheduled using java.util.Timer
38 * at specified poll interval following a specified delay.
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 * Creates new instance of MonitorTask
53 *
54 * @param action instance of MonitorAction to use
55 */
56 private MonitorTask(MonitorAction action)
57 {
58 _action = action;
59 }
60
61 /* ------------------------------------------------------------ */
62 /**
63 * Schedule new timer task for specified monitor action
64 *
65 * @param action monitor action
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 * Cancel timer task for specified monitor action
80 *
81 * @param action monitor action
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 * This method is invoked when poll interval has elapsed
93 * to check if the event trigger conditions are satisfied
94 * in order to fire event.
95 *
96 * @see java.util.TimerTask#run()
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 }