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