Developing an RMI-IIOP stateless CORBA application

This section tells you how to use the RMI-IIOP development style to create a stateless CORBA object application (approach 2 in Developing stateless CORBA objects, rather than the CORBA development approach described in previous sections).

The RMI-IIOP approach involves developing a standard Java™ Remote Method Invocation (RMI) application and deploying it to use IIOP as its transport protocol. This is the approach taken by enterprise beans.
Note: This section specifically documents how to develop a stateless CORBA application using RMI-IIOP. Enterprise beans are deployed using other tools, such as the Assembly Toolkit (ATK). For information about deploying enterprise beans, see Deploying enterprise beans.

When using RMI-IIOP there is no need to define an interface using IDL—though, if required, the IDL can optionally be generated later. Instead, we start by defining at least one remote interface. Note that, in this context, a “remote interface” means any Java interface that extends java.rmi.Remote. This is not the same thing as an enterprise bean's “Remote Interface”. Using the terminology just defined, both an enterprise bean's Remote Interface and its Home Interface would qualify as “remote interfaces”, because they both ultimately extend java.rmi.Remote.

This remote interface should be coded to follow the rules of Java RMI. An example remote interface is shown below:
package hello;
public interface HelloWorldRMI extends java.rmi.Remote
{ 
  	public String sayHello(String msgFromClient) throws java.rmi.RemoteException;
}  

The above interface defines a single method called sayHello that takes a String as a parameter and returns a String. All the methods on the interface must be defined to throw java.rmi.RemoteException.

Next, you should provide a server-side implementation of this interface. An example is shown below:
package hello;
public class _HelloWorldRMIImpl implements HelloWorldRMI
{ 
	public String sayHello(String msgFromClient)
	{ return "Hello: You said: " + msgFromClient;}
}  

The implementation class implements the interface previously created. The naming convention used for the implementation class is _<interface name>Impl. This naming convention is required if the server object is to be located using the CORBA CosLifeCycle Generic Factory approach. If you do not use this naming convention, the Generic Factory will not be able to construct instances of your stateless CORBA object.

One of the advantages of RMI-IIOP over the more traditional IDL-based development process is that you are not forced to extend a base class. This means that you can chose to use your own inheritance hierarchy if you wish. You may also implement multiple remote interfaces with a single server object.

You should compile both of the above classes using the javac compiler or equivalent.

The next thing to do is to produce the server-side Tie file for this stateless CORBA object. This is done using the RMI compiler (RMIC). You must use an RMI compiler shipped with an IBM® Java 2 SDK. If you use the version of RMIC supplied with a Sun MicroSystems' Java 2 SDK, the generated Tie file is not guaranteed to work with the CICS® ORB.

The command to use is as follows:
 rmic -iiop hello._HelloWorldRMIImpl 
Note that RMIC is being run against the server-side implementation class.
Next we need the client-side stub class. This is also produced using the RMI compiler. Ensure that you use an appropriate RMI compiler for your client ORB. The command to use is as follows:
rmic -iiop hello.HelloWorldRMI
Note that RMIC is being run against the remote interface class.
Once this is complete, you should have the following classes available:
hello\HelloWorldRMI.class              - the remote interface
hello\_HelloWorldRMIImpl.class         - the stateless CORBA object
hello\__HelloWorldRMIImpl_Tie.class    - the RMI-IIOP server side Tie file
hello\_HelloWorldRMI_Stub.class        - the RMI-IIOP client side Stub file   
The next thing to do is to write the client application. The client application is very similar to the client application developed using the IDL-based approach to CORBA development (described in Developing the IIOP client program). As before, we still need to find a reference to the stateless CORBA object using the CORBA CosLifeCycle Generic Factory. Here is part of an example RMI-IIOP client application:
ORB orb = ORB.init((String[]) null, (java.util.Properties) null);

// The following method reads the generic factory IOR from a file and returns
// it in the string
String factoryIOR = getFactoryIOR();

// Turn the stringified reference into the proxy
org.omg.CORBA.Object genFacRef = orb.string_to_object(factoryIOR);

// narrow to correct interface
GenericFactory fact = GenericFactoryHelper.narrow(genFacRef);

// The Generic factory needs a key, which is a sequence of namecomponents
NameComponent nc = new NameComponent("hello::HelloWorldRMI","object interface");

//Now create the object
org.omg.CORBA.Object objRef=fact.create_object(new NameComponent[]{nc},
                                              new NVP[] {});

// and narrow to correct interface using the RMI-IIOP narrow operation
HelloWorldRMI remote = (HelloWorldRMI) javax.rmi.PortableRemoteObject.narrow
                       (objRef, HelloWorldRMI.class);

// Invoke the remote method
System.out.println("Received from Server: "+remote.sayHello("Hi!")+"\n");}  

As with the IDL-based client application, it will be necessary to have the omgcos.jar file from the CICS lib HFS directory on your workstation and client machines in order to find the CosLifeCycle classes.

All that remains is to package the server- and client-side applications into JAR files and to add the server-side JAR file to the CICS shareable application class path.

If you want to generate IDL, for the RMI-IIOP remote interface, that would be suitable for use with a non-Java-based CORBA client application, use the following command:
rmic -idl hello.HelloWorldRMI