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.notification;
09:
10: import javax.management.NotificationListener;
11: import javax.management.Notification;
12: import javax.management.AttributeChangeNotification;
13: import javax.management.MBeanServer;
14: import javax.management.MBeanServerFactory;
15: import javax.management.ObjectName;
16: import javax.management.Attribute;
17:
18: /**
19: *
20: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
21: */
22:
23: public class SimpleNotificationAgent implements NotificationListener {
24: public void handleNotification(Notification notification, Object o) {
25: AttributeChangeNotification acn = (AttributeChangeNotification) notification;
26: System.out.println("Recieving a new notifation: "
27: + acn.getType() + "," + acn.getMessage() + " "
28: + acn.getOldValue() + " => " + acn.getNewValue());
29: }
30:
31: public static void main(String[] args) throws Exception {
32: System.out.println(System.currentTimeMillis());
33: MBeanServer server = MBeanServerFactory.createMBeanServer();
34: String className = "example.jmx.notification.SimpleNotification";
35: ObjectName objectName = new ObjectName(":name=" + className);
36: server.createMBean(className, objectName,
37: new Object[] { "Default Word" },
38: new String[] { "java.lang.String" });
39:
40: // add notificationListener
41: server.addNotificationListener(objectName,
42: new SimpleNotificationAgent(), null, null);
43:
44: // will cause send notification
45: server.setAttribute(objectName, new Attribute("Word",
46: "New Word!")); // setWord("New Word");
47:
48: System.out.println(System.currentTimeMillis());
49: }
50: }
|