Naming MBeans

MX4J ships with two naming MBeans, the NamingService and the CosNamingService, that wrap as MBeans respectively the rmiregistry and the tnameserv tools provided with the JDK.

The NamingService MBean

The NamingService MBean allows you to start the rmiregistry in the same JVM as other MBeans, such as the RMI MBean (see examples in the documentation), or the RMI adaptor.
The MBean's class is mx4j.tools.naming.NamingService.

The usage of the NamingService MBean does not require additional libraries, as all the needed classes are already shipped with the JDK.

Deployment

The NamingService MBean is easily deployed into a JMX Agent. The following code snippet shows how to deploy it into a JMX Agent.

Example 6.10. Deploying the NamingService MBean

                  
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName naming = new ObjectName("Naming:type=registry");
server.createMBean("mx4j.tools.naming.NamingService", naming, null);
               
               

To use the NamingService MBean as rmiregistry, it must be started; the NamingService MBean can be started and stopped at wish, simply by invoking the start() and stop() methods of the management interface.

Example 6.11. Starting and stopping the NamingService MBean

                  
Object proxy = MBeanServerInvocationHandler.newProxyInstance(server, naming, NamingServiceMBean.class, false);
NamingServiceMBean mbean = (NamingServiceMBean)proxy;
mbean.start();
...
mbean.stop();


or using the MBeanServer:


server.invoke(naming, "start", new Object[0], new String[0]);
...
server.invoke(naming, "stop", new Object[0], new String[0]);
            
               

It is also possible to specify the port on which the rmiregistry will run. Simply pass this parameter to the constructor of the NamingService MBean. By default the rmiregistry runs on port 1099.

Example 6.12. Changing the default port

                  
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName naming = new ObjectName("Naming:type=registry");

server.createMBean("mx4j.tools.naming.NamingService", naming, null, new Object[] {new Integer(2099)}, new String[] {"int"});


or


NamingService mbean = new NamingService(3099);
server.registerMBean(mbean, naming);


or via MLet file (specify the suitable codebase)


<MLET CODE="mx4j.tools.naming.NamingService"
      ARCHIVE="mx4j-tools.jar"
      CODEBASE="../lib/">
   <ARG TYPE="int" VALUE="4099">
</MLET>
               
               

It is also possible to change the rmiregistry port at runtime. Just start the NamingService MBean, stop it after a while, change the port it runs on, and restarting it, so that it will accept requests on the new port.
Below you can see the steps needed to perform this change.

Example 6.13. Changing the port at runtime

                  
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName naming = new ObjectName("Naming:type=registry");
server.createMBean("mx4j.tools.naming.NamingService", naming, null);
Object proxy = MBeanServerInvocationHandler.newProxyInstance(server, naming, NamingServiceMBean.class, false);
NamingServiceMBean mbean = (NamingServiceMBean)proxy;

mbean.start();
...
mbean.stop();
mbean.setPort(5099);
mbean.start();
            
               

The CosNamingService MBean

The CosNamingService MBean is very similar to the NamingService MBean: it allows you to start tnameserv in the same JVM as other MBeans.
The MBean's class is mx4j.tools.naming.CosNamingService.

The usage of the CosNamingService MBean does not require additional libraries, as all the needed classes are already shipped with the JDK.

Deployment

The CosNamingService MBean is easily deployed into a JMX Agent, in a way very similar to the NamingService MBean.

Exactly like the NamingService MBean, the CosNamingService MBean must be first registered in the MBeanServer and then started using the management interface it exposes.
There is the possibility to set the port on which the service will listen for incoming connections, exactly like the NamingService MBean.

One difference between the CosNamingService and the NamingService MBeans is that the latter provides and easy API to be implemented, so that creating, starting and stopping it can be implemented easily by the MX4J team, while the former does not.

A consequence of this is that tnameserv is started using Runtime.exec() and stopped by killing the process created when starting the MBean.

Since starting and stopping an external process may result in the start (); and stop (); returning before the process is really started (or stopped), it is recommended to sleep some time after start() and stop() in order to let tnameserv to start and stop completely.
A suggested value for this delay may be few seconds (1000-5000 milliseconds), but the value strongly depends on the hardware and operative system.