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.
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 answerHide 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();
};
};
moduleacts like a Java package (namespace).interfacedeclares the remote methods.inmeans the parameter is passed from client to server.onewaymeans the method is asynchronous (no return value, no blocking).stringis an IDL primitive type (maps tojava.lang.Stringin 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
-fallflag generates both client-side stub and server-side skeleton.-fclientgenerates only the client stub.-fservergenerates only the server skeleton.
Step 3: Files Generated by idlj
After running the command, the following files are generated inside the HelloApp folder:
| Generated File | Purpose |
|---|---|
_HelloStub.java | Client-side stub - acts as proxy for the client |
HelloOperations.java | Interface containing method signatures |
Hello.java | Java interface extending CORBA Object |
HelloHelper.java | Helper class for narrowing object references |
HelloHolder.java | Holder class for out/inout parameters |
HelloPOA.java | Server-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.
2080 · 5 marks
List the steps to create an RMI application. Differentiate between RMI and CORBA. [5]
Show solved answerHide 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
rmiccompiler 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
| Feature | RMI | CORBA |
|---|---|---|
| Full Form | Remote Method Invocation | Common Object Request Broker Architecture |
| Language | Java only (Java-specific) | Language independent (C++, Java, Python, etc.) |
| Platform | Java Virtual Machine (JVM) dependent | Platform and language independent |
| Protocol | Uses JRMP (Java Remote Method Protocol) | Uses IIOP (Internet Inter-ORB Protocol) |
| Ease of Use | Simple and easy to implement | Complex to implement and configure |
| Object Broker | RMI Registry acts as broker | Object Request Broker (ORB) is used |
| Interface Definition | Defined using Java interfaces | Defined using IDL (Interface Definition Language) |
| Performance | Faster within Java environment | Relatively slower due to overhead |
| Interoperability | Limited to Java applications only | High interoperability across languages and platforms |
| Standard | Java 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.
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]
2076 · 5 marks
What are different layers of RMI architecture? Explain. [5]
2075 · 5 marks
Describe the process to run the RMI application. [5]
Make Unit 8 stick
Practice CSC419 with flashcards & quizzes