0% found this document useful (0 votes)
2 views20 pages

Lec 2

RMI (Remote Method Invocation) is a Java API that enables distributed applications by allowing objects to invoke methods on remote objects in different JVMs. It utilizes stub and skeleton objects for communication, where the stub represents the client side and the skeleton represents the server side. The document outlines the steps to create an RMI program, including creating a remote interface, implementing it, and running both server and client applications.

Uploaded by

Ahmed El-harmel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Lec 2

RMI (Remote Method Invocation) is a Java API that enables distributed applications by allowing objects to invoke methods on remote objects in different JVMs. It utilizes stub and skeleton objects for communication, where the stub represents the client side and the skeleton represents the server side. The document outlines the steps to create an RMI program, including creating a remote interface, implementing it, and running both server and client applications.

Uploaded by

Ahmed El-harmel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

RMI (Remote Method

Invocation)
RMI
• The RMI (Remote Method Invocation) is an API that provides a
mechanism to create distributed application in java.
• The RMI allows an object to invoke methods on an object running in
another JVM.
• The RMI provides remote communication between the applications
using two objects stub and skeleton.
Understanding stub and skeleton
• RMI uses stub and skeleton object for communication with the
remote object.
• A remote object is an object whose method can be invoked from
another JVM.
• Let's understand the stub and skeleton objects
stub
• The stub is an object, acts as a gateway for the client side.
• All the outgoing requests are routed through it.
• It resides at the client side and represents the remote object.
• When the caller invokes method on the stub object, it does the following
tasks:
• It initiates a connection with remote Virtual Machine (JVM),
• It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
• It waits for the result
• It reads (unmarshals) the return value or exception, and
• It finally, returns the value to the caller.
skeleton
• The skeleton is an object, acts as a gateway for the server side object.
• All the incoming requests are routed through it.
• When the skeleton receives the incoming request, it does the
following tasks:
• It reads the parameter for the remote method
• It invokes the method on the actual remote object, and
• It writes and transmits (marshals) the result to the caller.
Java RMI Example
• The is given the 6 steps to write the RMI program:
• Create the remote interface
• Provide the implementation of the remote interface
• Compile the implementation class and create the stub and skeleton
objects using the rmic tool
• Start the registry service by rmiregistry tool
• Create and start the remote application
• Create and start the client application
RMI Example
create the remote interface
• For creating the remote interface, extend the Remote interface and
declare the RemoteException with all the methods of the remote
interface.
• Here, we are creating a remote interface that extends the Remote
interface. There is only one method named add() and it declares
RemoteException.
import java.rmi.*;
public interface Adder extends Remote{
public int add(int x,int y)throws RemoteException;
}
Provide the implementation of the remote
interface
• Extend the UnicastRemoteObject class,
• you must define a constructor that declares RemoteException.
create the stub and skeleton objects using
the rmic tool.
• The rmic tool invokes the RMI compiler and creates stub and skeleton
objects.
• rmic AdderRemote
Start the registry service by the rmiregistry
tool
• Now start the registry service by using the rmiregistry tool.
• If you don't specify the port number, it uses a default port number. In
this example, we are using the port number 5000.
• rmiregistry 5000
Create and run the server application
• Now rmi services need to be hosted in a server process.
• The Naming class provides methods to get and store the remote
object.
• The Naming class provides 5 methods.
In this example, we are binding the remote
object by the name sonoo.
Create and run the client application
• At the client we are getting the stub object by the lookup() method of
the Naming class and invoking the method on this object.
• In this example, we are running the server and client applications, in
the same machine so we are using localhost.
• If you want to access the remote object from another machine,
change the localhost to the host name (or IP address) where the
remote object is located.
Exercise
• Consider a scenario, there are two applications running in different
machines.
• Let's say MachineA and MachineB, machineA is located in United
States and MachineB in India.
• MachineB want to get list of all the customers of MachineA
application.

You might also like