RMI - Remote Method Invocation
RMI is a method by which we can access remote objects.RMI provides security to the objects in server.The advantage of RMI over networking is the low level functions like sending the values through streams,the usage of sockets and many such are done by the two things called the stub and the skeleton.
The Client could access the objects in the Server through the use of 3rd party software called rmi registry.The Client could access the Server only through the use of interface this is mainly because of security reasons.The Client can't access the methods which are not in the interface because these methods are mainly written for some internal computations.The Server could bind its functionality through the use of some proxy name.The Client looks up the registry by using the server's proxy name.
Note:A Server can have many proxies.Each proxy can provide one specific service.A service is nothing but the useful task. For example,Server has 2 proxies.Proxy1 can provide "Arithmetic operations".Proxy2 can provide "Reversing the strings".
EXAMPLE
1)Create an interface so that the signatures(methods inside interface)could only be accessed by the Client.The Server's entire properties cannot be shared by the Client.
import java.rmi.*;
public interface inter1 extends Remote
{
public void get(int a,int b) throws RemoteException;
public int display() throws RemoteException;
}
2)Create a Server class which implements this interface so it has to neccessarily override the signatures inside it.In addition to it we create a method called calculate() which is used for computation process namely Addition.
import java.rmi.*;
import java.rmi.server.*;
public class Server1 extends UnicastRemoteObject implements inter1
{
int x,y,z;
public Server1() throws RemoteException
{
}
public void get(int a,int b) throws RemoteException
{
x=a;
y=b;
}
public void calculate() throws RemoteException
{
z=x+y;
}
public int display() throws RemoteException
{
calculate();
return z;
}
public static void main(String args[])
{
try
{
Server1 s=new Server1();
Naming.rebind("Addition",s);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
3)Create a Client class.The Client looks up the registry to see whether the necessary service is available or not.If the required service is found then the computations are performed and the result is returned.
import java.rmi.*;
public class Client1
{
public static void main(String args[])throws Exception
{
int a;
inter1 i1=(inter1)Naming.lookup("Addition");
i1.get(10,30);
a=i1.display();
System.out.println(a);
}
}
Thus the three classes are developed.Now,we have to compile,start the registry and run these classes
1)Compile the interface named inter1.
2)Compile the server class named Server1.
3)Use rmic command to generate stub* and skeleton*
rmic Server1
Now the stub and skeletons are produced.
4)Start the rmiregistry so that the 3rd party software registry would be started which is used by Servers to bind their services and which is used by Clients to lookup the services provided by the Servers.
5)Run the server class..
6)Run the client class in a different environment(cmd).
* The stub and skeleton are used on Client and Server sides for data transferring.In the above example the client uses the get method to send two parameters namely 10 and 30.The stub receives these parameters, martials it and then sends it to the skeleton on the Server side.The skeleton receives the object from the stub unmartials(transforms it to the real parameters as sent by the stub) it and then computes based on the value sent by the Client.