01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.org
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package example.jmx.monitor;
09:
10: import javax.management.MBeanServer;
11: import javax.management.MBeanServerFactory;
12: import javax.management.ObjectName;
13: import javax.management.monitor.StringMonitor;
14: import javax.management.monitor.CounterMonitor;
15: import javax.management.monitor.GaugeMonitor;
16:
17: import org.huihoo.jfox.jmx.adaptor.http.HtmlAdaptorServer;
18:
19: /**
20: *
21: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
22: */
23:
24: public class MonitorAgent {
25: private MBeanServer server = null;
26: private StringMonitor sm = new StringMonitor();
27: private CounterMonitor cm = new CounterMonitor();
28: private GaugeMonitor gm = new GaugeMonitor();
29:
30: public MonitorAgent() throws Exception {
31: server = MBeanServerFactory.createMBeanServer();
32: ObjectName on = new ObjectName(":type=simple_standard");
33: server.createMBean("example.jmx.standard.SimpleStandard", on);
34:
35: // add StringMonitor
36: sm.setNotifyDiffer(true);
37: sm.setNotifyMatch(true);
38: sm.addObservedObject(on);
39: sm.setGranularityPeriod(5000L);
40: sm.setObservedAttribute("State");
41: sm.setStringToCompare("new state");
42: sm.addNotificationListener(new AgentListener(), null, null);
43: server.registerMBean(sm, null);
44:
45: // add CounterMonitor
46: cm.setGranularityPeriod(2000L);
47: cm.setModulus(new Integer(10));
48: cm.setNotify(true);
49: cm.addObservedObject(on);
50: cm.setObservedAttribute("Count");
51: cm.setOffset(new Integer(6));
52: cm.setInitThreshold(new Integer(20));
53: cm.addNotificationListener(new AgentListener(), null, null);
54: server.registerMBean(cm, null);
55:
56: // add GaugeMonitor
57: gm.setGranularityPeriod(2000L);
58: gm.setNotifyHigh(true);
59: gm.setNotifyLow(true);
60: gm.addObservedObject(on);
61: gm.setObservedAttribute("Floating");
62: gm.setThresholds(new Float(10.0), new Float(-10.0));
63: gm.addNotificationListener(new AgentListener(), null, null);
64: server.registerMBean(gm, null);
65:
66: }
67:
68: public void start() throws Exception {
69: sm.start();
70: cm.start();
71: gm.start();
72:
73: ObjectName haName = new ObjectName(
74: "Adaptor:type=HtmlAdaptor,port=8088");
75: HtmlAdaptorServer ha = new HtmlAdaptorServer();
76: server.registerMBean(ha, haName);
77: ha.start();
78:
79: System.out.println("Now,you can conncet http://" + ha.getHost()
80: + ":" + ha.getPort());
81: }
82:
83: public static void main(String[] args) throws Exception {
84: new MonitorAgent().start();
85: }
86: }
|