Prerequisite: RMI
RMI (Remote Method Invocation) is used for distributed object references system. A distributed object is an object which publishes its interface on other machines. A Remote Object is a distributed object whose state is encapsulated. Stub and Skeleton are two objects used to communicate with the remote object.
Stub: Stub is a gateway for client program which is used to communicate with skeleton object, by establishing a connection between them.
Skeleton: Resides on Server program which is used for passing the request from stub to the remote interface.
How communication and process takes place in RMI:
Steps to Run Java RMI Application in Console
- Creation of classes and interfaces for the problem statement: The steps involved in this are as follows:
- Create a Remote Interface which extends java.rmi.Remote:
A remote interface determines the object that can be invoked remotely by the client. This interface can be communicated with the client’s program. This Interface must extend java.rmi.Remote Interface. Problem Statement: Create an RMI Application for finding the factorial of a number Interface Programimportjava.math.BigInteger;ÂÂ// Creating an InterfacepublicinterfaceFactorial   Âextendsjava.rmi.Remote {   Â// Declaring the method   ÂpublicBigInteger fact(intnum)       Âthrowsjava.rmi.RemoteException;}
- Create a class which extends java.rmi.server.UnicastRemoteObject and implements the previous interface.
This class will implement the remote interface. Do the required calculation for the problem statement. Implementation of Interfaceimportjava.math.BigInteger;ÂÂ// Extends and Implement the class// and interface respectivelypublicclassFactorialImpl   Âextendsjava.rmi.server.UnicastRemoteObject   ÂimplementsFactorial {   Â// Constructor Declaration   ÂpublicFactorialImpl()       Âthrowsjava.rmi.RemoteException   Â{       Âsuper();   Â}   Â// Calculation for the problem statement   Â// Implementing the method fact()   Â// to find factorial of a number   ÂpublicBigInteger fact(intnum)       Âthrowsjava.rmi.RemoteException   Â{       ÂBigInteger factorial = BigInteger.ONE;       Âfor(inti =1; i <= num; ++i) {           Âfactorial = factorial                           Â.multiply(                               ÂBigInteger                                   Â.valueOf(i));       Â}       Âreturnfactorial;   Â}}
- Create a Server Class (with localhost and service name)
For hosting a service, the server program is created whereby using java.rmi.Naming.rebind() method can be called which takes two arguments i.e., an object reference (service name) and instances reference. Server Programimportjava.rmi.Naming;ÂÂpublicclassFactorialServer {   Â// Implement the constructor of the class   ÂpublicFactorialServer()   Â{       Âtry{           Â// Create a object reference for the interface           ÂFactorial c =newFactorialImpl();           Â// Bind the localhost with the service       Â}       Âcatch(Exception e) {           Â// If any error occur           ÂSystem.out.println("ERR: "+ e);       Â}   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// Create an object       ÂnewFactorialServer();   Â}}
- Create a Client Class (with localhost and service name)
Client program will invokes java.rmi.Naming.lookup() method for RMI URL and returns an instance of object type (Factorial Interface). All RMI is done on this object Client Programimportjava.net.MalformedURLException;importjava.rmi.Naming;importjava.rmi.NotBoundException;importjava.rmi.RemoteException;ÂÂpublicclassFactorialClient {   Âpublicstaticvoidmain(String[] args)   Â{       Âtry{           Â// Create an remote object with the same name           Â// Cast the lookup result to the interface           ÂFactorial c = (Factorial);           Â// Call the method for the results           ÂSystem.out.println(c.fact(30));       Â}       Â// If any error occur       Âcatch(MalformedURLException murle) {           ÂSystem.out.println("\nMalformedURLException: "                              Â+ murle);       Â}       Âcatch(RemoteException re) {           ÂSystem.out.println("\nRemoteException: "                              Â+ re);       Â}       Âcatch(NotBoundException nbe) {           ÂSystem.out.println("\nNotBoundException: "                              Â+ nbe);       Â}       Âcatch(java.lang.ArithmeticException ae) {           ÂSystem.out.println("\nArithmeticException: "+ ae);       Â}   Â}}
 
- Create a Remote Interface which extends java.rmi.Remote:
- Compilation of all program
Use javac to compile all four programs and rmic (RMI Compiler) to create a stub and skeleton class files. 
- Running the system:
After the compilation phase, the system is now ready to run. To run the system, open three console screen (move to that path where the program resides). One for the client, one for server and one for the RMI Registry. - Start with a registry, use rmiregistry, if there is no error registry will start running and now move to second screen.
- In the second console run the server program and host the FactorialService. It will start and wait for the client connection and it will load the implementation into memory.
- In the third console, run the client program.
 
- Start with a registry, use 
In this way RMI can be run in three console for localhost. RMI uses Network stack and TCP/IP Stack for communication of three different JVM’s.


 
                                    








