RMI JAVA
RMI
Remote method invocation(RMI) allow a java object to invoke method on an object running on another machine. RMI provide remote communication between java program. RMI is used for building distributed application.
Concept of RMI application
A RMI application can be divided into two part,Client program and Server program. A Server program creates some remote object, make their references available for the client to invoke method on it. A Client program make request for remote objects on server and invoke method on them. Stub and Skeleton are two important object used for communication with remote object.
Stub and Skeleton
Stub act as a gateway for Client program. It resides on Client side and communicate with Skeleton object. It establish the connection between remote object and transmit request to it.
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.
Skeleton object resides on server program. It is responsible for passing request from Stub to remote object
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.
Creating a Simple RMI application involves following steps
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
Define a remote interface
A remote interface specifies the methods that can be invoked remotely by a client. Clients program communicate to remote interfaces, not to classes implementing it. To be a remote interface, a interface must extend the Remote interface of java.rmi package. There is only one method named add() and it declares RemoteException.
import java.rmi.*;
public interface AddServerInterface extends Remote
{
public int sum(int a,int b) throws RemoteException;
}
Implementation of remote interface
For implementation of remote interface, a class must either extend UnicastRemoteObject or use exportObject() method of UnicastRemoteObject class.
import java.rmi.*;
import java.rmi.server.*;
public class Adder extends UnicastRemoteObject implements AddServerInterface
{
Adder()throws RemoteException
{
super();
}
public int sum(int a,int b)
{
return a+b;
}
}
Create AddServer and host rmi service
You need to create a server application and host rmi service Adder in it. This is done using rebind() method of java.rmi.Naming class. rebind() method take two arguments, first represent the name of the object reference and second argument is reference to instance of Adder.
import java.rmi.*;
import java.rmi.registry.*;
public class AddServer
{
public static void main(String args[])
{
try
{
AddServerInterface addService=new Adder();
Naming.rebind("AddService",addService);
//addService object is hosted with name AddService.
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Create client application
Client application contains a java program that invokes the lookup() method of the Naming class. This method accepts one argument, the rmi URL and returns a reference to an object of type AddServerInterface. All remote method invocation is done on this object.
import java.rmi.*;
public class Client
{
public static void main(String args[])
{
try
{
AddServerInterface st=(AddServerInterface)Naming.lookup("rmi://"+args[0]+"/AddService");
System.out.println(st.sum(25,8));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Create client application
Save all the above java file into a directory and name it as "rmi".
1. compile all the java files
javac *.java
2. Start RMI registry
start rmiregistry
3. Run Server file
java AddServer
4. Run Client file in another command prompt abd pass local host port number at run time
java Client 127.0.0.1
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )