RMI
RMI
RMI
Stub
Stub reside on client side.
Remote object forward request to stub at client side and then stub forward
request to network.
Stub perform the following steps:
Initiate remote objects.
Remote objects passes parameter to the stub.
Stub forward the request to skeleton.
Stub displays the result to the caller.
Skeleton
Skeleton reside on server side.
Skeleton perform following steps:
Skeleton receives the request from the stub.
Skeleton calls the remote method requested by the stub.
It display the result to the stub.
Architecture of RMI
Layers Of RMI
The RMI Registry is a naming service. RMI server programs use the
server to bind the remote java object with the names.
Clients executing on local or remote machines retrieve the
remote objects by their names.
Client executing on local and remote machine receive the remote
object by their names registered with the RMI registry and then
execute method on the object.
RMI create a remote proxy for that object and send to the client.
An object proxy contain the reference to an object.
Start registry by following command
Now second step to bind the remote object with the RMI registry execute the server
program.
By using the name of remote object in server registry client program locate the
remote object and then method are called on the remote object by the client
program.
Goals of RMI
Following are the goals of RMI −
• To minimize the complexity of the application.
• To preserve type safety.
• Distributed garbage collection.
• Minimize the difference between working with local and remote
objects.
Steps to write RMI Program
public static java.rmi.Remote lookup(java.lang.String) throws It returns the reference of the remote object.
java.rmi.NotBoundException, java.net.MalformedURLException,
java.rmi.RemoteException;
public static void bind(java.lang.String, java.rmi.Remote) throws It binds the remote object with the given name.
java.rmi.AlreadyBoundException, java.net.MalformedURLException,
java.rmi.RemoteException;
public static void unbind(java.lang.String) throws java.rmi.RemoteException, It destroys the remote object which is bound with the given name.
java.rmi.NotBoundException, java.net.MalformedURLException;
public static void rebind(java.lang.String, java.rmi.Remote) throws It binds the remote object to the new name.
java.rmi.RemoteException, java.net.MalformedURLException;
public static java.lang.String[] list(java.lang.String) throws It returns an array of the names of the remote objects bound in the registry.
java.rmi.RemoteException, java.net.MalformedURLException;
In this example, we are binding the remote object by the name sonoo.
1. import java. Rmi.*;
2. import java.rmi.registry.*;
3. public class My Server{
4. public static void main(String args[]){
5. try{
6. Adder stub=new AdderRemote();
7. Naming.rebind("rmi://localhost:5000/sonoo",stub);
8. }catch(Exception e){System.out.println(e);}
9. }
10. }
6) 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.
import java.rmi.*;
public class MyClient{
public static void main(String args[]){
try{
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));
}catch(Exception e){}
}
}
Distributed object model
Enable any object in the local system to directly interact with an object on a
remote host.
GOALS
RMI reduce the complexity of distributed object
Support remote object communication that call object on
different places.
Easy to use.