RMI applications typically use Java Remote Method Protocol (JRMP) for underlying communication. However, they can also use the CORBA’s IIOP as the communication protocol. This solution is known as RMI-IIOP, which combines the best features of RMI with the best features of CORBA.
RMI-IIOP utilizes the Java CORBA Object Request Broker (ORB). So, developers can work completely in the Java programming language. So, there is no separate IDL or mapping to learn. The rmic compiler may be used to generate the necessary code for connecting Java applications to others via IIOP. To work with CORBA applications in other languages, IDL may be generated using the rmic compiler with the -idl option. To generate IIOP stubs and tie classes, we use the rmic compiler with the -iiop option.
Note that RMI-IIOP provides interoperability with other CORBA objects implemented in different languages, provided that all the remote interfaces are originally defined as Java RMI interfaces.
1. An Example
In this section, we shall redevelop our calculator application where a client application invokes a method on a remote server object via IIOP.
1.1. Writing an interface
We use the same Calculator RMI interface used in Chapter 14.
import java.rmi.Remote;
public interface Calculator extends java.rmi.Remote {
int add (int a, int b) throws java.rmi.RemoteException;
}
1.2. Implementing the interface
Since, object will be accessed via IIOP, its class extends javax.rmi.PortabieRemoteObject. A sample implementation class (simpieCaicuiator.java) is shown below:
// SimpleCalculator.java
import javax.rmi.PortabieRemoteObject;
class SimpleCalculator extends PortableRemoteObject implements Calculator {
public SimpleCalculator() throws java.rmi.RemoteException {}
public int add(int a, int b) throws java.rmi.RemoteException {
System.out.println(”Received: ” + a + ” and ” + b);
int result = a + b;
System.out.println(”Sent: ” + result);
return result;
}
}
Since, super class’s constructor might fail to construct the object, defining a constructor that throws a RemoteException is necessary, even if it does nothing.
1.3. Writing the server
In the server, we create an instance of this SimpleCalculator and bind it to the CORBA’s naming service (orbd) via JNDI. The following is the source code of the server (CalculatorServerJNDI.java):
// CalculatorServerJNDI.java import javax.naming.*;
public class CalculatorServerJNDI {
public static void main(String args[]) {
try {
SimpleCalculator calc = new SimpleCalculator();
Context ic = new InitialContext();
ic.rebind(”Calculator”, calc );
System.out.println(”Calculator Server ready…”);
} catch (Exception e) { e.printStackTrace();}
}
}
1.4. Writing the Client
Writing the client is very simple. It gets a reference from the CORBA naming service via JNDI and invokes the add() method. The following is the source code of the client (CaicuiatorCiientJNDI.java):
//CalculatorClientJNDI.java import javax.naming.*;
public class CalculatorClientJNDI {
public static void main( String args[] ) {
try {
Context ic = new InitialContext();
Calculator calc = (Calculator)ic.lookup(”Calculator”);
int x =2, y = 3;
System.out.println(”Sent :” + x + ” and ” + y);
int result = calc.add(x, y);
System.out.println(”Received : ” + result);
} catch( Exception e ) {e.printStackTrace();}
}
}
1.5. Compiling server files
The server side of the application should have the files Calculator.java, SimpleCalculator. java and CalculatorServerJNDI.java. Compile the implementation class first using the following command:
javac SimpieCaicuiator.java
To create CORBA-compliant stub and skeleton files, use rmic compiler with the -iiop option:
rmic -iiop SimpleCalculator
This generates the client stub Caicuiator_stub.ciass and server skeleton _simpieCaicuiator_ Tie.ciass. Now, compile server source files as follows:
javac Calculator.java CalculatorServerJNDI.java
1.6. Compiling Client Files
The client side should have files Calculator.java, and CalculatorClientJNDI.java. Compile the interface file Calculator.java using the following command:
javac Calculator.java
To create CORBA-compliant stub, use the following file:
rmic -iiop Calculator
Now, compile the client file:
javac CalculatorClientJNDI.java
1.7. Running the Application
Start orbd in a host having IP address say 172.16.4.248 on port 67 8 9 as follows:
orbd -ORBInitialPort 6789
Start the server as follows:
java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory –
Djava.naming.provider.url=iiop://172.16.4.248:6789 CalculatorServerJNDI
Start the client as follows:
java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory –
Djava.naming.provider.url=iiop://172.16.4.248:6789 CalculatorClientJNDI
Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.