KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > dynamic > DynamicMBeanExample


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.mbeans.dynamic;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.Notification JavaDoc;
14 import javax.management.NotificationListener JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.management.monitor.GaugeMonitor JavaDoc;
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 {
28    public static void main(String JavaDoc[] args) throws Exception JavaDoc
29    {
30       // Let's create the MBeanServer
31
MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
32
33       // Let's create a dynamic MBean and register it
34
DynamicService serviceMBean = new DynamicService();
35       ObjectName JavaDoc serviceName = new ObjectName JavaDoc("examples", "mbean", "dynamic");
36       server.registerMBean(serviceMBean, serviceName);
37
38       // Now let's register a Monitor
39
// We would like to know if we have peaks in activity, so we can use JMX's
40
// GaugeMonitor
41
GaugeMonitor JavaDoc monitorMBean = new GaugeMonitor JavaDoc();
42       ObjectName JavaDoc monitorName = new ObjectName JavaDoc("examples", "monitor", "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 JavaDoc(8), new Integer JavaDoc(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(new NotificationListener JavaDoc()
59       {
60          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
61          {
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 }
72
Popular Tags