RMI in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
Start RMI registry
Run Server file
Run Client file in another command prompt and pass localhost port number at runtime
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
RMI
Remote method invocation(RMI) allows a Java object to invoke a method on an object running on another machine. RMI provides remote communication between java program. RMI is used for building a distributed application.
Concept of RMI application
An 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 a method on it. A Client program makes a request for remote objects on the server and invokes a method on them. Stub and Skeleton are two important objects used for communication with the remote object.
Stub and Skeleton
Stub act as a gateway for the Client program. It resides on the Client side and communicates with Skeleton object. It establishes the connection between a remote object and transmits a request to it.
Skeleton object resides on a server program. It is responsible for passing the request from Stub to a remote object.
Creating a Simple RMI application involves following steps
- Define a remote interface.
- Implementing remote interface.
- create and start remote application
- create and start 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, an interface must extend the Remote interface of a java.rmi package.
import java.rmi.*; public interface AddServerInterface extends Remote { public int sum(int a,int b); }
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 method
rebind()
ofjava.rmi.Naming class. rebind()
the method takes two arguments, first represent the name of the object reference and the second argument is a reference to an instance of Adderimport 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 method
lookup()
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);} } }
Steps to run this RMI application
Save all the above java file into a directory and name it as "rmi"
- compile all the java files
javac *.java
Start RMI registry
start rmiregistry
Run Server file
java AddServer
Run Client file in another command prompt and pass localhost port number at runtime
java Client 127.0.0.1
Comments
Post a Comment