Friday, May 9, 2008

JMX service URL

A Remote JMX Client is just like a Local JMX Client except that it must instantiate a Connector client and connect it up to a Connector server to get a MBeanServerConnection. It then uses the MBeanServerConnection in the same way as a Local JMX Client. Remote JMX applications can not work with an MBeanServer object, only an MBeanServerConnection.

The Agent must run a Connector Server using the same tranport protocol that you will use on the client side. If your server serves tranport "rmi", then your client(s) need to use protocol "rmi". Other protocols are jmxmp, http, snmp. RMI is the default transport protocol. So we have JMX Connector client and JMX connector server for different protocols.

On connector server side:

// Instantiate the MBean server
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Create an RMI connector server
JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi://srv:20000/jndi/rmi://srv:30000/server");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url,null, mbs);

The connector server cs is launched by calling the start() method of JMXConnectorServer, whereupon the instance of RMIConnectorServer that is created exports its underlying RMI server stub server to the RMI registry.

On connector client side:

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://srv:20000/jndi/rmi://srv:30000/server");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

As you can see, the Client defines the same service URL url as that defined by Server. This allows the connector client to retrieve the RMI connector server stub named server from the RMI registry running on port 30000 of the srv host, and to connect to the RMI connector server.

In doc, the service URL is explained in detail. A simplified URL is service:jmx:rmi:///jndi/rmi://srv:30000/server. The absent host means local host, and the absent port means the the exported remote object RMIServerImpl_Stub is not exported to a specified port. Remember when we export a remote object, The second argument in exportObject method, specifies which TCP port to use to listen for incoming remote invocation requests for the object. It is common to use the value zero, which specifies the use of an anonymous port. The actual port will then be chosen at runtime by RMI or the underlying operating system.

Compute engine = new ComputeEngine();
Compute stub =(Compute) UnicastRemoteObject.exportObject(engine, 0);Registry registry = LocateRegistry.getRegistry();
registry.rebind("computer", stub);

At last, the "jndi" in the middle of the url means we use JNDI to look up the remote object. RMIRegistry is just one of its service provider, others include file system, ldap, etc. So we may have jndi/ldap:localhost:839, jndi/c:/myphotos, etc.

Monitor Your Applications With JConsole
Remote JMX, RMI and JMXServiceURL
Implementing a remote interface
RMI Registry Service Provider for the Java Naming and Directory InterfaceTM (JNDI)

No comments: