The XMOJO Project
<< Prev Chapter 4.2 RMI Adaptor Next >>

RMI Adaptor


RMI Adaptor enables application clients and managers to connect to a JMX agent through RMI protocol. A RMI Adaptor has two ends, the RMI Server and the RMI Client.

RMI Registry is one of the major components of the RMI architecture. (Discussing about the RMI architecture is outside the scope of this documentation).

An RMI Registry is a simple server that enables an application to look up objects that are being exported for remote method invocation. The RMI Registry tracks the addresses of remote objects that are being exported by their applications. All objects are assigned unique names so that they can be easily identified.

 Communication Mechanism 

When the RMI Adaptor is started, it will start an RMI Registry in the same machine, and an instance of remoteMBeanServer is bound with the name AdventNetRMIAdaptor in this RMI Registry.

When the Client initially contacts the JMX agent, it contacts the RMI Registry and performs a lookup passing the name AdventNetRMIAdaptor. If the name exists, the RMI Registry sends an instance of the remoteMBeanServer to the caller. Once the remoteMBeanServer instance is obtained, the Client will make uses this instance to invoke the methods on the MBeanServer running in the JMX agent.

The Communication Mechanism is represented in the image shown below:

RMI_Adaptor

 RMI Adaptor as an MBean 

The RMI adaptor itself is implemented as a Dynamic MBean. It exposes four attributes and two operations. The attributes are Port, State, Active, and ObjectName where Port is a read write attribute and the rest three are read only attributes. The Operations are startService and stopService. Since RMI adaptor itself is implemented as an MBean, it can be managed like other MBeans.

 Registering RMI Adaptor 

RMI Adaptor is registered like any other MBean registered with the MBeanServer. A sample code snippet is shown below:

import com.adventnet.adaptors.rmi.RMIAdaptor;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

MBeanServer mbs = null;
ObjectName name = null;
Integer rmiPort = 1099;

private void registerRMIAdaptor()
{

try
{
// create an instance of MBeanServer
mbs = MBeanServerFactory.createMBeanServer();

// create a new object name
name = new ObjectName("Adaptors:name=RMI,port=" + rmiPort);

RMIAdaptor rmiadaptor = new RMIAdaptor();
rmiadaptor.setPort(rmiPort);
mbs.registerMBean(rmiadaptor, name);
}
catch (Exception e)
{
  e.printStackTrace();
}

}


If the port number specified for starting the RMI adaptor is already occupied, the RMI adaptor will not be started and java.net.bind exception will be thrown.

 How to Test RMI Adaptor ? 

RMI adaptor can be tested in the following ways:
  1. Using RMI client API
  2. Using MBean Browser GUI tool

 Testing RMI Adaptor Using RMI Client 

RMI Client is a connector which aids in communicating between a manager and a JMX agent. The manager application can use an RMI Client to connect to an RMI Server running in any JVM in the same host, or even different host, provided both the Client and Server are connected in a same network.

 Establishing Connection Using RMI Client 

The below given code snippet shows how to establish a connection to a RMI Connector Server using a RMI Client.

import com.adventnet.adaptors.clients.rmi.RMIClient;

String
hostname = 
"localhost";
int port = 1099;
String protocol = "RMI";
RMIClient client = 
new RMIClient();

try
{
// establishing connecting with the Connector Server
client.connect(hostname, port, protocol);

}
catch (Exception e)
{
  e.printStackTrace();
}

 Testing RMI Adaptor Using MBeanBrowser 

MBeanBrowser is a GUI tool, which is used to test and manage the applications via RMI.  This internally uses the RMI client API for communicating with the JMX agent.

To test RMI adaptor using MBeanBrowser:
  1. Open the MBeanBrowser tool.
  2. Select RMI as the communication protocol.
  3. Make sure the client settings for RMI protocol reflect the current testing environment.
  4. Connect to the server (Operations menu -> connect).
  5. The target application can be managed by getting/setting attributes, invoking operations, etc.

<< Prev Home Next >>
HTML Adaptor
Examples