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.remote.notification;
10:
11: import javax.management.MBeanServerConnection;
12: import javax.management.Notification;
13: import javax.management.NotificationListener;
14: import javax.management.ObjectName;
15: import javax.management.loading.MLet;
16: import javax.management.remote.JMXConnector;
17: import javax.management.remote.JMXConnectorFactory;
18: import javax.management.remote.JMXServiceURL;
19:
20: /**
21: * This example shows how to setup a JSR 160 connector client, and how it is
22: * possible to receive notifications emitted by a remote connector server.
23: *
24: * @version $Revision: 1.1 $
25: * @see Server
26: */
27: public class Client {
28: public static void main(String[] args) throws Exception {
29: // The address of the connector server
30: JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0,
31: "/jndi/jmx");
32:
33: // Create and connect the connector client
34: JMXConnector cntor = JMXConnectorFactory.connect(url, null);
35:
36: // The connection represent, on client-side, the remote MBeanServer
37: MBeanServerConnection connection = cntor
38: .getMBeanServerConnection();
39:
40: // The listener that will receive notifications from a remote MBean
41: NotificationListener listener = new NotificationListener() {
42: public void handleNotification(Notification notification,
43: Object handback) {
44: System.out.println(notification);
45: }
46: };
47:
48: // The MBeanServerDelegate emits notifications about registration/unregistration of MBeans
49: ObjectName delegateName = ObjectName
50: .getInstance("JMImplementation:type=MBeanServerDelegate");
51:
52: connection.addNotificationListener(delegateName, listener,
53: null, null);
54:
55: // Give chance to the notification machinery to setup
56: Thread.sleep(1000);
57:
58: // Now register a remote MBean, for example an MLet, so that the MBeanServerDelegate
59: // will emit notifications for its registration
60: ObjectName name = ObjectName.getInstance("examples:mbean=mlet");
61: // First notification
62: connection.createMBean(MLet.class.getName(), name, null);
63: // Second notification
64: connection.unregisterMBean(name);
65: }
66: }
|