KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > rmi > iiop > 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.rmi.iiop;
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 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.4 $
25  */

26 public class Client
27 {
28    public static void main(String JavaDoc[] args) throws Exception JavaDoc
29    {
30       // The JMXConnectorServer protocol, in this case is IIOP
31
String JavaDoc serverProtocol = "iiop";
32
33       // The RMI server's host: this is actually ignored by JSR 160
34
// since this information is stored in the RMI stub.
35
String JavaDoc serverHost = "host";
36
37       // The host and port where the COSNaming service runs and the path under which the stub is registered.
38
String JavaDoc namingHost = "localhost";
39       int namingPort = 1199;
40       String JavaDoc jndiPath = "/jmxconnector";
41
42       // The address of the connector server
43
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:" + serverProtocol + "://" + serverHost + "/jndi/iiop://" + namingHost + ":" + namingPort + jndiPath);
44
45       // Connect a JSR 160 JMXConnector to the server side
46
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(url);
47
48       // Retrieve an MBeanServerConnection that represent the MBeanServer the remote
49
// connector server is bound to
50
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
51
52       // Call the server side as if it is a local MBeanServer
53
ObjectName JavaDoc delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
54       Object JavaDoc proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean JavaDoc.class, true);
55       MBeanServerDelegateMBean JavaDoc delegate = (MBeanServerDelegateMBean JavaDoc)proxy;
56
57       // The magic of JDK 1.3 dynamic proxy and JSR 160:
58
// delegate.getImplementationVendor() is actually a remote JMX call,
59
// but it looks like a local, old-style, java call.
60
System.out.println(delegate.getImplementationVendor() + " is cool !");
61    }
62 }
63
Popular Tags