01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.examples.mbeans.dynamic;
10:
11: import javax.management.MBeanServer;
12: import javax.management.MBeanServerFactory;
13: import javax.management.Notification;
14: import javax.management.NotificationListener;
15: import javax.management.ObjectName;
16: import javax.management.monitor.GaugeMonitor;
17:
18: /**
19: * Purpose of this example is to show how to use DynamicMBean in general, with the help
20: * of the {@link mx4j.AbstractDynamicMBean AbstractDynamicMBean} class, see
21: * {@link DynamicService}.
22: * It also shows usage of the Monitor classes.
23: *
24: * @version $Revision: 1.1 $
25: */
26: public class DynamicMBeanExample {
27: public static void main(String[] args) throws Exception {
28: // Let's create the MBeanServer
29: MBeanServer server = MBeanServerFactory.newMBeanServer();
30:
31: // Let's create a dynamic MBean and register it
32: DynamicService serviceMBean = new DynamicService();
33: ObjectName serviceName = new ObjectName("examples", "mbean",
34: "dynamic");
35: server.registerMBean(serviceMBean, serviceName);
36:
37: // Now let's register a Monitor
38: // We would like to know if we have peaks in activity, so we can use JMX's
39: // GaugeMonitor
40: GaugeMonitor monitorMBean = new GaugeMonitor();
41: ObjectName monitorName = new ObjectName("examples", "monitor",
42: "gauge");
43: server.registerMBean(monitorMBean, monitorName);
44:
45: // Setup the monitor: we want to be notified if we have too many clients or too less
46: monitorMBean.setThresholds(new Integer(8), new Integer(4));
47: // Setup the monitor: we want to know if a threshold is exceeded
48: monitorMBean.setNotifyHigh(true);
49: monitorMBean.setNotifyLow(true);
50: // Setup the monitor: we're interested in absolute values of the number of clients
51: monitorMBean.setDifferenceMode(false);
52: // Setup the monitor: link to the service MBean
53: monitorMBean.addObservedObject(serviceName);
54: monitorMBean.setObservedAttribute("ConcurrentClients");
55: // Setup the monitor: a short granularity period
56: monitorMBean.setGranularityPeriod(50L);
57: // Setup the monitor: register a listener
58: monitorMBean.addNotificationListener(
59: new NotificationListener() {
60: public void handleNotification(
61: Notification notification, Object handback) {
62: System.out.println(notification);
63: }
64: }, null, null);
65: // Setup the monitor: start it
66: monitorMBean.start();
67:
68: // Now start also the service
69: serviceMBean.start();
70: }
71: }
|