8 Rmi And Corba

Advanced Java Programming · Unit 8 · 3 hrs

RMI and CORBA

Exam-focused notes for RMI and CORBA (Advanced Java Programming, CSC419): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.

RMI and CORBA in short

This unit covers two ways of building distributed Java applications: Remote Method Invocation (RMI), Java's own mechanism for calling methods on objects running in another JVM, and CORBA, a language-independent standard that predates it.

RMI and CORBA: notes

Introduction of RMI, Architecture of RMI, Creating and Executing RMI Applications

RMI (Remote Method Invocation) is an API that lets an object invoke a method on an object running inside a different JVM, possibly on a different physical machine, making it Java's core mechanism for building distributed applications: systems whose components live on different machines but coordinate to appear, to the end user, as a single system. RMI's design goals are to keep the complexity of writing a distributed application low, to preserve Java's type safety across the network boundary, to provide distributed garbage collection of remote objects, and to make working with a remote object feel as close as possible to working with a local one. For an application to count as distributed, it must be able to locate a remote method, communicate with the remote object, and load the class definitions the remote object needs.

RMI achieves this through a stub and a skeleton. The stub lives on the client and acts as a local proxy for the remote object: when the caller invokes a method on the stub, the stub opens a connection to the remote JVM, sends the method's parameters across it, waits for the result, reads the return value, and hands it back to the caller as though the call had been local. The skeleton lives on the server side and is the stub's counterpart: it receives the incoming request, reads the parameters, invokes the real method on the actual object, and sends the result back to the caller.

The RMI architecture is organized into layers. The application layer is where the client and server programs themselves communicate. The proxy layer, sitting beneath it, holds the client stub and the server skeleton described above. The Remote Reference Layer (RRL) maintains the session for each method call and handles the details of duplicated remote references. The transport layer, at the bottom, is responsible for actually setting up the network connection between the two machines, built on standard TCP/IP.

What this unit covers

  • Introduction of RMI, Architecture of RMI, Creating and Executing RMI Applications
  • Introduction to CORBA, RMI vs CORBA, Architecture of CORBA, IDL, Simple CORBA Program

Introduction to CORBA, RMI vs CORBA, Architecture of CORBA, IDL, Simple CORBA Program

2081 · 5 marks

How do you write an IDL file and generate the stub and skeleton code for CORBA implementation? Explain. [5]

Show solved answer

Writing IDL File and Generating Stub/Skeleton Code for CORBA Implementation

What is IDL in CORBA?

CORBA (Common Object Request Broker Architecture) uses Interface Definition Language (IDL) to separate the interface from the implementation. IDL is language-neutral, meaning the same IDL file can be used to generate code in Java, C++, Python, or any other supported language. This is a key advantage of CORBA over RMI, which is Java-specific.


Steps to Write an IDL File and Generate Stub/Skeleton

Step 1: Write the IDL File

An IDL file defines the interface (methods/operations) that the remote object will expose. It does not contain any implementation logic.

Syntax/Structure of an IDL file:

module ModuleName {
    interface InterfaceName {
        returnType methodName(in paramType paramName);
    };
};

Example: Hello.idl

module HelloApp {
    interface Hello {
        string sayHello();
        oneway void shutdown();
    };
};
  • module acts like a Java package (namespace).
  • interface declares the remote methods.
  • in means the parameter is passed from client to server.
  • oneway means the method is asynchronous (no return value, no blocking).
  • string is an IDL primitive type (maps to java.lang.String in Java).

Step 2: Compile the IDL File to Generate Stub and Skeleton

Use the idlj compiler (IDL-to-Java compiler) that comes with the JDK.

Command:

idlj -fall Hello.idl
  • -fall flag generates both client-side stub and server-side skeleton.
  • -fclient generates only the client stub.
  • -fserver generates only the server skeleton.

Step 3: Files Generated by idlj

After running the command, the following files are generated inside the HelloApp folder:

Generated FilePurpose
_HelloStub.javaClient-side stub - acts as proxy for the client
HelloOperations.javaInterface containing method signatures
Hello.javaJava interface extending CORBA Object
HelloHelper.javaHelper class for narrowing object references
HelloHolder.javaHolder class for out/inout parameters
HelloPOA.javaServer-side skeleton (Portable Object Adapter)

Step 4: Implement the Server (using the Skeleton)

import HelloApp.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;

public class HelloImpl extends HelloPOA {
    public String sayHello() {
        return "Hello from CORBA Server!";
    }
    public void shutdown() {
        // shutdown logic
    }
}
  • The server class extends HelloPOA (the generated skeleton).

Step 5: Implement the Client (using the Stub)

import HelloApp.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;

public class HelloClient {
    public static void main(String[] args) throws Exception {
        ORB orb = ORB.init(args, null);
        // Locate the remote object and invoke method
        Hello helloRef = HelloHelper.narrow(
            orb.string_to_object("corbaname::localhost#Hello")
        );
        System.out.println(helloRef.sayHello());
    }
}
  • The client uses HelloHelper.narrow() to get a reference to the remote object.
  • The stub (_HelloStub) handles all network communication transparently.

Summary Diagram

Hello.idl
    |
    | idlj -fall Hello.idl
    |
    +---> _HelloStub.java   (Client Stub)
    +---> HelloPOA.java     (Server Skeleton)
    +---> Hello.java, HelloHelper.java, etc.
    
Client uses Stub --> ORB --> Skeleton --> Server Implementation

Key Points to Remember

  • IDL is language-independent; it only defines the interface, not the implementation.
  • The stub resides on the client side and acts as a local proxy.
  • The skeleton resides on the server side and delegates calls to the actual object.
  • CORBA passes objects by reference (unlike RMI which can pass by value or reference).
  • The ORB (Object Request Broker) handles all communication between stub and skeleton.
Open this answer on its own page →

2080 · 5 marks

List the steps to create an RMI application. Differentiate between RMI and CORBA. [5]

Show solved answer

RMI Application Creation Steps and RMI vs CORBA


Steps to Create an RMI Application

RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed applications in Java, allowing an object to invoke methods on an object running in another JVM. The following steps are required:

Step 1: Define the Remote Interface

  • Create an interface that extends java.rmi.Remote
  • Declare all methods that can be invoked remotely
  • Each method must throw java.rmi.RemoteException
public interface MyRemote extends Remote {
    public String sayHello() throws RemoteException;
}

Step 2: Implement the Remote Interface

  • Create a class that implements the remote interface
  • Extend UnicastRemoteObject
  • Implement all declared remote methods
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
    public String sayHello() throws RemoteException {
        return "Hello from Server!";
    }
}

Step 3: Generate Stub and Skeleton

  • As noted in the reference, RMI uses two objects: stub and skeleton for remote communication
  • Stub: Client-side proxy that forwards calls to the remote object
  • Skeleton: Server-side object that receives calls and passes them to the actual object
  • Use rmic compiler to generate these: rmic MyRemoteImpl

Step 4: Start the RMI Registry

  • Start the RMI registry service (acts as a naming service)
  • Run: rmiregistry (default port 1099)

Step 5: Register the Remote Object (Server Side)

  • Bind the remote object to the RMI registry using Naming.rebind()
MyRemote obj = new MyRemoteImpl();
Naming.rebind("MyRemoteService", obj);

Step 6: Write and Run the Client

  • Look up the remote object from the registry using Naming.lookup()
  • Invoke remote methods through the stub
MyRemote obj = (MyRemote) Naming.lookup("rmi://localhost/MyRemoteService");
System.out.println(obj.sayHello());

Difference Between RMI and CORBA

FeatureRMICORBA
Full FormRemote Method InvocationCommon Object Request Broker Architecture
LanguageJava only (Java-specific)Language independent (C++, Java, Python, etc.)
PlatformJava Virtual Machine (JVM) dependentPlatform and language independent
ProtocolUses JRMP (Java Remote Method Protocol)Uses IIOP (Internet Inter-ORB Protocol)
Ease of UseSimple and easy to implementComplex to implement and configure
Object BrokerRMI Registry acts as brokerObject Request Broker (ORB) is used
Interface DefinitionDefined using Java interfacesDefined using IDL (Interface Definition Language)
PerformanceFaster within Java environmentRelatively slower due to overhead
InteroperabilityLimited to Java applications onlyHigh interoperability across languages and platforms
StandardJava standard (Sun/Oracle)Industry standard by OMG (Object Management Group)

Summary

RMI is simpler and best suited for pure Java distributed applications, while CORBA is more powerful and suitable for heterogeneous, multi-language distributed systems requiring high interoperability.

Open this answer on its own page →

2078 · 5 marks

Why CORBA is important? Compare CORBA with RMI. [5]

CORBA (Common Object Request Broker Architecture) is a standard defined by the Object Management Group (OMG) that enables communication between objects written in different programming languages running on different platforms. 1. Language Independence: CORBA allows objects written in different la...

Open this answer on its own page →

Introduction of RMI, Architecture of RMI, Creating and Executing RMI Applications

2077 · 10 marks

What is the significance of stub and skeleton in RMI? Create a RMI application such that a client sends an Integer number to the server and the server return the factorial value of that integer. Give a clear specification for every step.[10]

--- RMI (Remote Method Invocation) is a Java API that provides a mechanism to create distributed applications. It allows an object in one JVM to invoke methods on an object running in another JVM. The two key objects that enable this remote communication are the Stub and the Skeleton. --- The stu...

Open this answer on its own page →

2076 · 5 marks

What are different layers of RMI architecture? Explain. [5]

RMI (Remote Method Invocation) is a Java API that allows an object running in one Java Virtual Machine (JVM) to invoke methods on an object running in another JVM, enabling distributed application development. --- RMI architecture consists of the following three main layers: --- This is the topmo...

Open this answer on its own page →

2075 · 5 marks

Describe the process to run the RMI application. [5]

Remote Method Invocation (RMI) is a Java API that allows an object running in one Java Virtual Machine (JVM) to invoke methods on an object running in another JVM, possibly on a different machine. It enables distributed computing in Java. --- - Create an interface that extends java.rmi.Remote. - ...

Open this answer on its own page →