Unit8 RMI and COBRA
Unit8 RMI and COBRA
RMI (Remote Method Invocation) is a Java API that provides a mechanism to create
distributed application in Java. RMI allows Java programs (of a single java application)
running on different machines to talk to each other and work together as if they were
all running on the same machine. This is useful for building distributed applications
where different parts of the application run on different machines and need to
communicate with each other.
RMI allows objects to communicate and interact with each other in a distributed
environment, such as a network. RMI enables Java objects to invoke methods on
objects located on another machine.
RMI provides a secure and reliable way to communicate between distributed objects
but however, RMI can be slow and may not be suitable for high-performance
distributed systems.
Stub
Stub in RMI (Remote Method Invocation) is a client-side object that acts as a
representative for a remote object located on a different machine (server). A "stub" is
a local representative of a remote object that resides on a different Java Virtual
Machine (JVM) of client and can be used to invoke methods on the remote object.
When a client program wants to access a remote object, it obtains a stub for that
object from a registry or naming service. The stub is responsible for making the remote
method call and returning the result back to the client program.
When a client program wants to use/access a remote object, it first obtains a reference
to the remote object using a naming service. The reference returned is a stub object
that acts as a representative for that remote object. The client then uses this stub
object to invoke methods on the remote object as like if it was a local object. The stub
object intercepts or takes over the method calls made by the client to the remote
object and forwards them over the network to the server where the actual object
resides. The results are then sent back to the client through the stub object.
The stub object hides the details of network communication between client and server
machine from the client, making it easy for the client to interact with the remote object
without worrying about the underlying network protocols.
Skeleton
A "skeleton" is a server-side object that receives remote method invocations from the
client and dispatches them to the actual remote object lying on server that the client
wants to access. The skeleton object is responsible for receiving the method call,
unmarshalling the parameters, invoking the method on the remote object, marshalling
the result, and sending it back to the client.
It does the tasks like:
- Receiving the remote method call from client and reading the parameters and
unmarshalling them.
- Invoking the remote method on actual remote object.
- Marshalling and sending back the result to the client.
RMI Architecture
1. Define the remote interface: Define a Java interface that extends the Remote
class and declares the methods that will be remotely accessible.
2. Implement the remote interface: Implement the remote interface in a Java class
that provides the actual implementation of the methods declared in the remote
interface.
3. Create a server program: Write a Java program that creates an instance of the
remote object and registers it with the RMI registry.
4. Start the RMI registry: Start the RMI registry on the server machine using the
"rmiregistry" command.
5. Create a client program: Write a Java program that obtains a reference to the
remote object from the RMI registry and invokes its methods.
6. Compile the programs: Compile the server and client programs using the "javac"
command.
7. Start the server program: Start the server program on the server machine using
the "java" command.
8. Run the client program: Run the client program on the client machine using the
"java" command.
9. Check the output: Check the output of the client program to verify that the
remote methods were invoked successfully.
It is important to note that when running a Java RMI application, the server and client
programs should be executed on different machines. This is because RMI is designed to
allow objects to be accessed remotely over a network.
Example:
Here's a step-by-step guide to creating an RMI (Remote Method Invocation) application
in Java that calculates the factorial of a number sent by a client:
1. Create two packages: one for the client and one for the server. For this example,
we'll call them "client" and "server".
import java.rmi.Remote;
import java.rmi.RemoteException;
4. In the "client" package, create a class called "FactorialClient" that will create a
connection to the server and call the remote "factorial" method. Here's the code:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
5. Compile both the server and client classes and run them.
Old questions
(only one 5 marks questions asked in majority of question set. So, one 5 marks
question coming is chance).
i. What is RMI? Differentiate it with CORBA? (5)[2071][2072][2075]
ii. Write short note on: a. RMI architecture (2.5)[2072] [2073]
iii. What is CORBA? How is it different from RMI? (2+3) [2074]
iv. What are different layers of RMI architecture? Explain. (5)[2076]
v. Describe the process to run the RMI application. [5] [2075]
vi. 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)
[2077]
vii. Explain RMI architecture layers in detail. Write a Java programs using RMI to
find product of two numbers. (4+6) [2078]
viii. Why CORBA is important? Compare CORBA with RMI. (3+2) [2078]
ix. How CORBA differs from RMI? Discuss the concepts of IDL briefly. (2 + 3)
[model question]
4. Write a java program that writes objects of Employee class in the file named
emp.doc. Create Employee class as of your interest. (5)
5. What are layout managers? Explain Gridbag layout with suitable example. (1 +
4)
6. What is the use of action command in event handling? Explain with example. (1
+ 4)
7. What causes SQL exception? How it can be handled? Explain with example. (1
+ 4)
8. Write a java program using TCP such that client sends number to server and
displays its factorial. The server computes factorial of the number received
from client. (5)
9. How JavaFx differs from Swing? Explain steps of creating GUI using javaFx. (2 +
3)
10.What are different ways of writing servlet programs? Write a sample Servlet
program using any one way. (1 + 4)
11.How CORBA differs from RMI? Discuss the concepts of IDL briefly. (2 + 3)
12.When thread synchronization is necessary? Explain with suitable example. (1 +
4)