KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > simple > 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.simple;
10
11 import javax.management.MBeanServerConnection JavaDoc;
12 import javax.management.MBeanServerDelegateMBean JavaDoc;
13 import javax.management.MBeanServerInvocationHandler JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.remote.JMXConnector JavaDoc;
16 import javax.management.remote.JMXConnectorFactory JavaDoc;
17 import javax.management.remote.JMXServiceURL JavaDoc;
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 {
33    public static void main(String JavaDoc[] args) throws Exception JavaDoc
34    {
35       // The JMXConnectorServer protocol, in this case is RMI.
36
String JavaDoc serverProtocol = "rmi";
37
38       // The RMI server's host: this is actually ignored by JSR 160
39
// since this information is stored in the RMI stub.
40
String JavaDoc serverHost = "host";
41
42       // The host, port and path where the rmiregistry runs.
43
String JavaDoc namingHost = "localhost";
44       int namingPort = 1099;
45       String JavaDoc jndiPath = "/jmxconnector";
46
47       // The address of the connector server
48
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:" + serverProtocol + "://" + serverHost + "/jndi/rmi://" + namingHost + ":" + namingPort + jndiPath);
49
50       // Connect a JSR 160 JMXConnector to the server side
51
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
52
53       // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
54
// connector server is bound to
55
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
56
57       // Call the server side as if it is a local MBeanServer
58
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
59       Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean JavaDoc.class, true);
60       MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc)proxy;
61
62       // The magic of JDK 1.3 dynamic proxy and JSR 160:
63
// delegate.getImplementationVendor() is actually a remote JMX call,
64
// but it looks like a local, old-style, java call.
65
System.out.println(delegate.getImplementationVendor() + " is cool !");
66    }
67 }
68
Popular Tags