Make your own free website on Tripod.com

RMI-IIOP
Building an RMI/IIOP Server Component

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.3 or Higher (I use JDK version 1.4)
.........................................The COS Naming JNDI Service Provider (I use JNDI version 1.2.1)

Building Java/RMI Server Components that use OMG's IIOP (Internet Inter-ORB Protocol) wire protocol

JavaSoft provides the RMI-IIOP set of libraries which allow Java/RMI applications to use OMG's Internet Inter-ORB Protocol (IIOP) instead of Sun's proprietary Java Remote Method Protocol (JRMP). This has made it possible to write Java/RMI Servers that can be accessed not only by regular Java clients, but also from CORBA clients and vice versa.

Caveat Emptor

Using RMI/IIOP, however, also means that there are restrictions imposed on the Remote interfaces that you define for your components. They are:

  • You have to make sure that your Java names don't conflict with the names generated by the IDL compiler (idlj.exe). You can get away with this by using underscores either at the start or within the name. Most Java names are fine.

  • Names that differ only by case may not work fine. Even though there is support for special conditions, it is better to avoid these situations altogether by making sure that you use non-conflicting names.

  • Please be aware that your object references that you share, may not be preserved exactly by the IIOP framework.

  • You cannot use constants in remote interfaces. If you have to, make sure these constants are either primitive types or strings that can be evaluated at compile time.

  • When you create your interfaces, make sure that it does not inherit the same method from more than one parent.

  • If you want distributed garbage collection, you will have to explicitly unexport objects to allow them to be cleaned up.

  • Finally, don't use UnicastRemoteObject, RMISocketFactory, Unreferenced, or other distributed garbage collection facilities that you use when you use RMI over JRMP.

The good news is, just like RMI/JRMP, RMI/IIOP allows server objects to receive messages from multiple threads, so they should be thread-safe. Also make sure that the CORBA Object Request Broker (ORB) that you use supports CORBA 2.3 or higher. This allows you to pass objects by value across the wire. If you are using the ORB  supplied by Sun, you should be fine.

In this article, we will build an RMI Server that can return the stock price of a stock given the symbol.

The various steps that are involved in creating an RMI Server that uses the IIOP protocol are as follows:

  1. Create the Servant interface and the Servant component class

  2. Create the RMI Server that can host these servant classes

  3. Compile the class files and generate the Skeletons and IDL File

  4. Start up the Server

1. Create the Servant Interface and Component Class File

Create the Servant interface - StockMarket.java - in a package called SimpleStocks

SimpleStocks/StockMarket.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
/**
 * The following example illustrates an RMI Server
 *
 * author: Gopalan Suresh Raj
 * Copyright (c), 2002. All Rights Reserved.
 * URL: http://gsraj.tripod.com/
 * email: gopalan@gmx.net
 */


package SimpleStocks;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 * This is an interface to a component that looks up
 * the price of a given Stock Symbol
 *
 * @author Gopalan Suresh Raj
 */

public interface StockMarket extends Remote {

  /**
   * Remote method that should contain the business logic.
   *
   * @param stock symbol String denoting the stock to look up.
   * @return price of the stock as a float
   */

  float getPrice( String stockSymbol ) throws RemoteException;
}

Create an implementation of the interface, the Servant class - StockMarketImpl.java - in the same package - SimpleStocks. The easiest way to implement an RMI-IIOP Servant is to extend the PortableRemoteObject class as shown on Line 20. You also have the option of exporting non-Portable remote objects, but by extending this class you get the export function automatically. Your class also implements the StockMarket remote interface as shown on Line 21.

SimpleStocks/StockMarketImpl.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
/**
 * The following example illustrates an RMI-IIOP Servant
 *
 * author: Gopalan Suresh Raj
 * Copyright (c), 2002. All Rights Reserved.
 * URL: http://gsraj.tripod.com/
 * email: gopalan@gmx.net
 */


package SimpleStocks;

import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;

/**
 * This component looks up the price of a given Stock Symbol
 *
 * @author Gopalan Suresh Raj
 */

public class StockMarketImpl extends PortableRemoteObject
                                             implements StockMarket {

  /**
   * Public No argument constructor
   */

  public StockMarketImpl() throws RemoteException {
    // Invoke the superclass to export this object
    super();
  }

  /**
   * Remote method that actually contains the business logic.
   *
   * @param stock symbol String denoting the stock to look up.
   * @return price of the stock as a float
   */

  public float getPrice( String stockSymbol ) throws RemoteException {
    float price = 0;
    for( int index = 0; index < stockSymbol.length(); index++ ) {
      price += (int) stockSymbol.charAt( index );
    }
    price /= 5;
    System.out.println ("Value of "+stockSymbol+" is US $"+price);
    return price;
  }

}

2. Develop an RMI Server that can host these Servant Classes

Develop a Server Application called StockMarketServer.java. Instead of using the RMI Registry to make your servant objects available, you need to use the JNDI context that supports CosNaming. Using the InitialContext class as shown on Line 47, you need to rebind the Server onject to its known name as shown on Line 50.

StockMarketServer.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
/**
 * The following example illustrates an RMI-IIOP Server
 *
 * author: Gopalan Suresh Raj
 * Copyright (c), 2002. All Rights Reserved.
 * URL: http://gsraj.tripod.com/
 * email: gopalan@gmx.net
 */


import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import SimpleStocks.StockMarketImpl;

/**
 * Creates a Server and binds the RMI Servant with the IIOP Registry
 *
 * Pre-requisites: You will need to have the COS naming server
 * running for the registration code to work.
 *
 * @author Gopalan Suresh Raj
 */

public class StockMarketServer {

  static final String CONTEXT_NAME = "java.naming.factory.initial";
  static final String IIOP_STRING  = "com.sun.jndi.cosnaming.CNCtxFactory";

  static final String URL_NAME = "java.naming.provider.url";
  static final String IIOP_URL_STRING  = "iiop://localhost:1000";

  /**
   * Entry Point to this application
   */

  public static void main(String[] args) {
    try {

      // Create the Object
      StockMarketImpl myObject = new StockMarketImpl();

      // Create the IIOP Initial Context
      Properties iiopProperties = new Properties();
      iiopProperties.put( StockMarketServer.CONTEXT_NAME,
                          StockMarketServer.IIOP_STRING );
      iiopProperties.put( StockMarketServer.URL_NAME,
                          StockMarketServer.IIOP_URL_STRING );
      InitialContext iiopContext = new InitialContext( iiopProperties );

      // Bind the object to the IIOP registry
      iiopContext.rebind( "NASDAQ", myObject );

      System.out.println( "StockMarket bound in the IIOP Registry as NASDAQ and is up and ready for eCommerce..." );
    }
    catch ( Exception exception ) {
      exception.printStackTrace ();
    }
  }
}

To run the server, make sure you have the COS Naming Server (tnameservice.exe) provided by Sun up and running for this registration code to work.

3. Compile the Class Files and Generate the IIOP Skeletons and the IDL File

Compile the Server and Servant Classes.

Command Prompt
C:\MyProjects\Cornucopia\iiop>javac -classpath . StockMarketServer.java

C:\MyProjects\Cornucopia\iiop>
javac -classpath . .\SimpleStocks\StockMarket.java .\SimpleStocks\StockMarketImpl.java

C:\MyProjects\Cornucopia\iiop>

Generate the IIOP Skeletons and the IDL File

Command Prompt
C:\MyProjects\Cornucopia\iiop>rmic -classpath . -iiop -idl SimpleStocks.StockMarketImpl

C:\MyProjects\Cornucopia\iiop>

4. Startup the ORB Services and the RMI Server

Start up the servers as shown below.

Command Prompt
C:\MyProjects\Cornucopia\iiop>start tnameserv -ORBInitialPort 1000

C:\MyProjects\Cornucopia\iiop>
start java -classpath . StockMarketServer

C:\MyProjects\Cornucopia\iiop>

The screen shot below shows the COS Naming Services Server up and running.

The screen shot below shows our RMI Server up and running across Client invocations

 

Now you need to build a client applications that can access this Server.

Java/RMI
Java/RMI - Under The Hood
Developing a Java/RMI Server using JRMP
Developing a Java/RMI Client using JRMP
Developing a Java/RMI Server Component using IIOP
Developing a Java/RMI Client to our RMI/IIOP Server Component
Developing a CORBA Client to our RMI/IIOP Server Component

 

Download the entire source code as a zip file.

 

click here to go to
My Advanced Java/J2EE Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

 


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited   times since January 13, 2002.

Last Updated : Jan 13, '02

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2002, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.