Porting old MX4J remoting code to JSR 160

Introduction

MX4J version 1.x provided a custom implementation of a connector server and connector client based on the RMI protocol.
That old code is now obsolete due to the fact that MX4J implements JSR 160. In the following sections will be explained how to port old MX4J 1.x remoting code to the standard JSR 160 API.

Porting Examples

The following example will show old MX4J 1.x remoting code compared to JSR 160 code, with respect to creating and starting a connector server over JRMP on server side.

Example 3.23. Old MX4J 1.x remoting code, server side

               
(1)
String jndiName = "jrmp";

(2)
mx4j.adaptor.rmi.jrmp.JRMPAdaptor adaptor = new mx4j.adaptor.rmi.jrmp.JRMPAdaptor();

(3)
adaptor.putJNDIProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
adaptor.putJNDIProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
adaptor.setJNDIName(jndiName);

(4)
adaptor.start();
               
            

Example 3.24. JSR 160 remoting code, server side

               
(1)
JMXServiceURL address = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jrmp");

(3)
Map environment = new HashMap();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
environment.put(Context.PROVIDER_URL, "rmi://localhost:1099");

(2)
JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnector(address, environment, null); (2)

(4)
cntorServer.start();
               
            

Note the following differences:

  1. How the JNDI name "jrmp" has been replaced by a JMXServiceURL with an URLPath of "/jndi/jrmp".
  2. How the instantiation of the adaptor has been replaced by usage of a factory.
  3. How the JNDI properties are passed via a Map.

Note that in both cases the adaptor (connector server) must be started in order to be able to accept incoming connections (4).

Note also that both the JRMPAdaptor and the JMXConnectorServer are MBeans, and as such they can be registered inside an MBeanServer (no differences here).

The following example will show old MX4J 1.x remoting code compared to JSR 160 code, with respect to creating and connecting a connector over JRMP on client side.

Example 3.25. Old MX4J 1.x remoting code, client side

               
(1)
String jndiName = "jrmp";

(2)
mx4j.connector.rmi.jrmp.JRMPConnector connector = new mx4j.connector.rmi.jrmp.JRMPConnector();

(3)
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
environment.put(Context.PROVIDER_URL, "rmi://localhost:1099");

(4)
connector.connect(jndiName, environment);

(5)
mx4j.connector.RemoteMBeanServer server = connector.getRemoteMBeanServer();
               
            

Example 3.26. JSR 160 remoting code, client side

               
(1)
JMXServiceURL address = new JMXServiceURL("rmi", "localhost", 0, "/jndi/jrmp");

(3)
Map environment = new HashMap();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
environment.put(Context.PROVIDER_URL, "rmi://localhost:1099");

(2)
JMXConnector connector = JMXConnectorFactory.newJMXConnector(address, environment);

(4)
connector.connect(environment);

(5)
MBeanServerConnection server = connector.getMBeanServerConnection();
               
            

Again, note the following differences:

  1. How the JNDI name "jrmp" has been replaced by a JMXServiceURL with an URLPath of "/jndi/jrmp".
  2. How the instantiation of the connector has been replaced by usage of a factory.
  3. How the JNDI properties are passed via a Map.
  4. The different number of arguments passed to the connect() method.
  5. How the mx4j.connector.RemoteMBeanServer class is replaced by the javax.management.MBeanServerConnection class.

Very similar changes apply when the old MX4J 1.x remoting code is using the mx4j.adaptor.rmi.iiop.IIOPAdaptor and the mx4j.connector.rmi.iiop.IIOPConnector.