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.simple;
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 the simplest way to connect to a JSR 160 connector server. <br />
21: * To do so, the most important information is the JMXServiceURL, which is the address
22: * of the remote connector server. This url is generated by the server, and
23: * must be known to the client.
24: * When using JSR 160's RMI connector server, this information is often in form of a
25: * JNDI name where the RMI stub has been registered; in this case the client needs
26: * to know the host and port of the JNDI server and the JNDI path where the stub is
27: * registered.
28: *
29: * @version $Revision: 1.1 $
30: */
31: public class Client {
32: public static void main(String[] args) throws Exception {
33: // The JMXConnectorServer protocol, in this case is RMI.
34: String serverProtocol = "rmi";
35:
36: // The RMI server's host: this is actually ignored by JSR 160
37: // since this information is stored in the RMI stub.
38: String serverHost = "host";
39:
40: // The host, port and path where the rmiregistry runs.
41: String namingHost = "localhost";
42: int namingPort = 1099;
43: String jndiPath = "/jmxconnector";
44:
45: // The address of the connector server
46: JMXServiceURL url = new JMXServiceURL("service:jmx:"
47: + serverProtocol + "://" + serverHost + "/jndi/rmi://"
48: + namingHost + ":" + namingPort + jndiPath);
49:
50: // Connect a JSR 160 JMXConnector to the server side
51: JMXConnector connector = JMXConnectorFactory.connect(url);
52:
53: // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
54: // connector server is bound to
55: MBeanServerConnection connection = connector
56: .getMBeanServerConnection();
57:
58: // Call the server side as if it is a local MBeanServer
59: ObjectName delegateName = ObjectName
60: .getInstance("JMImplementation:type=MBeanServerDelegate");
61: Object proxy = MBeanServerInvocationHandler.newProxyInstance(
62: connection, delegateName,
63: MBeanServerDelegateMBean.class, true);
64: MBeanServerDelegateMBean delegate = (MBeanServerDelegateMBean) proxy;
65:
66: // The magic of JDK 1.3 dynamic proxy and JSR 160:
67: // delegate.getImplementationVendor() is actually a remote JMX call,
68: // but it looks like a local, old-style, java call.
69: System.out.println(delegate.getImplementationVendor()
70: + " is cool !");
71: }
72: }
|