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.rmi.iiop;
10:
11: import javax.management.MBeanServerConnection;
12: import javax.management.MBeanServerDelegateMBean;
13: import javax.management.MBeanServerInvocationHandler;
14: import javax.management.ObjectName;
15: import javax.management.remote.JMXConnector;
16: import javax.management.remote.JMXConnectorFactory;
17: import javax.management.remote.JMXServiceURL;
18:
19: /**
20: * This example shows how to connect to a JSR 160 connector server over IIOP.
21: * It is very similar to the simple example also present in these examples, except
22: * that it uses the IIOP protocol instead of native RMI's one, called JRMP.
23: *
24: * @version $Revision: 1.1 $
25: */
26: public class Client {
27: public static void main(String[] args) throws Exception {
28: // The JMXConnectorServer protocol, in this case is IIOP
29: String serverProtocol = "iiop";
30:
31: // The RMI server's host: this is actually ignored by JSR 160
32: // since this information is stored in the RMI stub.
33: String serverHost = "host";
34:
35: // The host, port and path where the rmiregistry runs.
36: String namingHost = "localhost";
37: int namingPort = 1099;
38: String jndiPath = "/jmxconnector";
39:
40: // The address of the connector server
41: JMXServiceURL url = new JMXServiceURL("service:jmx:"
42: + serverProtocol + "://" + serverHost + "/jndi/iiop://"
43: + namingHost + ":" + namingPort + jndiPath);
44:
45: // Connect a JSR 160 JMXConnector to the server side
46: JMXConnector connector = JMXConnectorFactory.connect(url);
47:
48: // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
49: // connector server is bound to
50: MBeanServerConnection connection = connector
51: .getMBeanServerConnection();
52:
53: // Call the server side as if it is a local MBeanServer
54: ObjectName delegateName = ObjectName
55: .getInstance("JMImplementation:type=MBeanServerDelegate");
56: Object proxy = MBeanServerInvocationHandler.newProxyInstance(
57: connection, delegateName,
58: MBeanServerDelegateMBean.class, true);
59: MBeanServerDelegateMBean delegate = (MBeanServerDelegateMBean) proxy;
60:
61: // The magic of JDK 1.3 dynamic proxy and JSR 160:
62: // delegate.getImplementationVendor() is actually a remote JMX call,
63: // but it looks like a local, old-style, java call.
64: System.out.println(delegate.getImplementationVendor()
65: + " is cool !");
66: }
67: }
|