0% found this document useful (0 votes)
10 views5 pages

22 Rmi

RMI (Remote Method Invocation) is a Java API that enables remote communication between applications using stub and skeleton objects. The stub acts as a client-side gateway, handling method calls and communication with the remote JVM, while the skeleton processes incoming requests on the server side. To implement RMI, developers must create a remote interface, provide its implementation, generate stub and skeleton objects, and set up both server and client applications using the RMI registry.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

22 Rmi

RMI (Remote Method Invocation) is a Java API that enables remote communication between applications using stub and skeleton objects. The stub acts as a client-side gateway, handling method calls and communication with the remote JVM, while the skeleton processes incoming requests on the server side. To implement RMI, developers must create a remote interface, provide its implementation, generate stub and skeleton objects, and set up both server and client applications using the RMI registry.

Uploaded by

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

RMI (Remote Method Invocation)

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.

stub and skeleton


RMI uses stub and skeleton object for communication with the remote object.

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:

1. It initiates a connection with remote Virtual Machine (JVM),


2. It writes and transmits the parameters to the remote Virtual Machine (JVM), [marshaling]
3. It waits for the result
4. It reads the return value or exception [unmarshaling]
5. 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:

1. It reads the parameter for the remote method


2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.

Steps to write an RMI program

1. Create the remote interface


2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub and skeleton objects using the
rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application

Example
1)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.

import java.rmi.*;
public interface Adder extends Remote{

public int add(int x,int y)throws RemoteException;


}

2) Provide the implementation of the remote interface

For providing the implementation of the Remote interface, we need to

 Either extend the UnicastRemoteObject class,


 or use the exportObject() method of the UnicastRemoteObject class

import java.rmi.*;
import java.rmi.server.*;

public class AdderRemote extends UnicastRemoteObject implements Adder{

AdderRemote()throws RemoteException{
super();
}

public int add(int x,int y)


{
return x+y;}

}
3) 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

4) Start the registry service by the rmiregistry tool


rmiregistry 5000

5) 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.

1. public static java.rmi.Remote lookup(java.lang.String) throws


java.rmi.NotBoundException, java.net.MalformedURLException,
java.rmi.RemoteException; it returns the reference of the remote object.
2. public static void bind(java.lang.String, java.rmi.Remote) throws
java.rmi.AlreadyBoundException, java.net.MalformedURLException,
java.rmi.RemoteException; it binds the remote object with the given name.
3. public static void unbind(java.lang.String) throws java.rmi.RemoteException,
java.rmi.NotBoundException, java.net.MalformedURLException; it destroys the
remote object which is bound with the given name.
4. public static void rebind(java.lang.String, java.rmi.Remote) throws
java.rmi.RemoteException, java.net.MalformedURLException; it binds the
remote object to the new name.
5. public static java.lang.String[] list(java.lang.String) throws
java.rmi.RemoteException, java.net.MalformedURLException; it returns an array
of the names of the remote objects bound in the registry.

import java.rmi.*;
import java.rmi.registry.*;

public class MyServer{

public static void main(String args[]){


try{

Adder stub=new AdderRemote();


Naming.rebind("rmi://localhost:5000/test",stub);

}catch(Exception e){System.out.println(e);}
}

6) Create and run the client application


import java.rmi.*;

public class MyClient{

public static void main(String args[]){


try{
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/test");

System.out.println(stub.add(34,4));

}catch(Exception e){System.out.println(e);}
}

You might also like