KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > remote > hessian > 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.tools.remote.hessian;
10
11 import javax.management.MBeanServerConnection JavaDoc;
12 import javax.management.MBeanServerDelegateMBean JavaDoc;
13 import javax.management.MBeanServerInvocationHandler JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.management.remote.JMXConnector JavaDoc;
18 import javax.management.remote.JMXConnectorFactory JavaDoc;
19 import javax.management.remote.JMXServiceURL JavaDoc;
20 import javax.management.timer.Timer JavaDoc;
21
22 /**
23  * This example shows how to connect to a Hessian JMXConnectorServer.
24  * To run this example, you need the following jars:
25  * <ul>
26  * <li>MX4J 3.x</li>
27  * <ul>
28  * <li>mx4j.jar</li>
29  * <li>mx4j-remote.jar</li>
30  * <li>mx4j-tools.jar</li>
31  * <li>mx4j-examples.jar</li>
32  * </ul>
33  * <li>Hessian 3.0.8</li>
34  * <ul>
35  * <li>hessian-3.0.8.jar</li>
36  * </ul>
37  * </ul>
38  *
39  * @version $Revision: 1.1 $
40  */

41 public class Client
42 {
43    public static void main(String JavaDoc[] args) throws Exception JavaDoc
44    {
45       // This JMXServiceURL works only if the connector server is on the same host of
46
// the connector. If this is not the case, set the correct host name.
47
JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("hessian", null, 8080, "/hessian");
48
49       // Connect a JSR 160 JMXConnector to the server side
50
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(address);
51
52       // Retrieve an MBeanServerConnection that represent the MBeanServer
53
// the remote connector server is bound to
54
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
55
56       // Call the server side as if it is a local MBeanServer
57
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
58       Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean JavaDoc.class, true);
59       MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc)proxy;
60
61       System.out.println(delegate.getImplementationVendor() + " is cool !");
62
63       // Register an MBean, and get notifications via the Hessian protocol
64
connection.addNotificationListener(delegateName, new NotificationListener JavaDoc()
65       {
66          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
67          {
68             System.out.println("Got the following notification: " + notification);
69          }
70       }, null, null);
71
72       ObjectName JavaDoc timerName = ObjectName.getInstance("services:type=Timer");
73       connection.createMBean(Timer JavaDoc.class.getName(), timerName, null);
74
75       // Unregistering the MBean to get another notification
76
connection.unregisterMBean(timerName);
77
78       // Allow the unregistration notification to arrive before killing this JVM
79
Thread.sleep(1000);
80
81       connector.close();
82    }
83 }
84
Popular Tags