22 Rmi
22 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.
stub
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:
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:
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{
import java.rmi.*;
import java.rmi.server.*;
AdderRemote()throws RemoteException{
super();
}
}
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
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.
import java.rmi.*;
import java.rmi.registry.*;
}catch(Exception e){System.out.println(e);}
}
System.out.println(stub.add(34,4));
}catch(Exception e){System.out.println(e);}
}