Make your own free website on Tripod.com

RMI-IIOP
Building an Java/RMI Client using IIOP

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 Client that uses OMG's IIOP (Internet Inter-ORB Protocol) wire protocol

Developing an RMI/IIOP client is really simple and straight-forward.

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

  1. Create the Client class

  2. Copy the IIOP Stubs and compile the Client

  3. Make sure the Server is running and then startup the Client

1. Create the Client Class File

Create the RMI Client - RmiClient.java - in a package called RmiClient. The main difference between a JRMP Client and an IIOP client is the use of the PortableRemoteObject class. The other difference is the use of JNDI to locate a reference to the Server as shown on Line 48. As soon as a reference is found, the PortableRemoteObject object is used to narrow down the reference to the appropriate type, once again as shown on Line 48. Once the reference to the appropriate type is found, you can invoke any operation on it as shown on Line 53.

RmiClient/RmiClient.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:
59:
60:
/**
 * The following example illustrates an RMI-IIOP Client
 *
 * author: Gopalan Suresh Raj
 * Copyright (c), 2002. All Rights Reserved.
 * URL: http://gsraj.tripod.com/
 * email: gopalan@gmx.net
 */

package RmiClient;

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

/**
 * This Client looks up the price of a given Stock Symbol
 *
 * Pre-requisites: You will need to have the COS naming server
 * and the StockMarket Server up and running before you can
 * invoke them from this client.
 *
 * @author Gopalan Suresh Raj
 */

public class RmiClient {

  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 IIOP Initial Context
      Properties iiopProperties = new Properties();
      iiopProperties.put( RmiClient.CONTEXT_NAME,
                                   RmiClient.IIOP_STRING );
      iiopProperties.put( RmiClient.URL_NAME,
                                   RmiClient.IIOP_URL_STRING );
      InitialContext iiopContext = new InitialContext( iiopProperties );

      // Obtain a reference to the Servant Object
      StockMarket server = (StockMarket) PortableRemoteObject.narrow( iiopContext.lookup ( "NASDAQ" ),
                                                                                                              StockMarket.
class );

      // Invoke the Servant
      System.out.println( "The price of MY COMPANY is " +
                                   server.getPrice( "MY_COMPANY" ) );      
    }
    catch ( Exception exception ) {
      exception.printStackTrace ();
    }

  }
}

2. Copy the IIOP Subs and Compile the Client

Copy the IIOP Stubs from the SimpleStocks folder to the RmiClient folder, and compile the client as shown below.

Command Prompt
C:\MyProjects\Cornucopia\iiop>copy .\SimpleStocks\_StockMarket_Stub.class .\RmiClient
1 file(s) copied.

C:\MyProjects\Cornucopia\iiop>
copy .\SimpleStocks\_StockMarketImpl_Tie.class .\RmiClient
1 file(s) copied.

C:\MyProjects\Cornucopia\iiop>
javac -classpath . .\RmiClient\RmiClient.java

C:\MyProjects\Cornucopia\iiop>

3. Make sure that the Servers are running and startup the client

Make sure that the Servers are up. If they are not, start them up using the following commands.

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

Startup the Client as shown below, and invoke operations on the Server.

Command Prompt
C:\MyProjects\Cornucopia\iiop>java -classpath . RmiClient.RmiClient
The price of MY COMPANY is 159.2

C:\MyProjects\Cornucopia\iiop>

 

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.