KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > notification > Client


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.remote.notification;
10
11 import javax.management.MBeanServerConnection JavaDoc;
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationListener JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.loading.MLet JavaDoc;
16 import javax.management.remote.JMXConnector JavaDoc;
17 import javax.management.remote.JMXConnectorFactory JavaDoc;
18 import javax.management.remote.JMXServiceURL JavaDoc;
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.4 $
25  * @see Server
26  */

27 public class Client
28 {
29    public static void main(String JavaDoc[] args) throws Exception JavaDoc
30    {
31       // The address of the connector server
32
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("rmi", "localhost", 0, "/jndi/jmx");
33
34       // Create and connect the connector client
35
JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(url, null);
36
37       // The connection represent, on client-side, the remote MBeanServer
38
MBeanServerConnection JavaDoc connection = cntor.getMBeanServerConnection();
39
40       // The listener that will receive notifications from a remote MBean
41
NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
42       {
43          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
44          {
45             System.out.println(notification);
46          }
47       };
48
49       // The MBeanServerDelegate emits notifications about registration/unregistration of MBeans
50
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
51
52       connection.addNotificationListener(delegateName, listener, null, null);
53
54       // Give chance to the notification machinery to setup
55
Thread.sleep(1000);
56
57       // Now register a remote MBean, for example an MLet, so that the MBeanServerDelegate
58
// will emit notifications for its registration
59
ObjectName JavaDoc name = ObjectName.getInstance("examples:mbean=mlet");
60       // First notification
61
connection.createMBean(MLet JavaDoc.class.getName(), name, null);
62       // Second notification
63
connection.unregisterMBean(name);
64    }
65 }
66
Popular Tags