160 likes | 344 Views
Remote Method Invocation (RMI). RMI is a mechanism for communicating (only) between two machines running Java Virtual Machines. The stub object (on machine A) has to build an information block thats consists of an identifier of the remote object to be used,
E N D
Remote Method Invocation (RMI) • RMI is a mechanism for communicating (only) between two machines running Java Virtual Machines
The stub object (on machine A) has to • build an information block thats consists of • an identifier of the remote object to be used, • an operation number describing the method to be called and • the marshalled parameters (method parameters have to be encoded into a format suitable for transporting them across the net) • send this information to the server
The tasks of the skeleton object (on machine B) are • to unmarshal the parameters, • to call the desired method on the real object lying on the server, • to capture the return value or exception of the call on the server, • to marshal this value, • to send a package consisting of the value in the marshalled form back to the stub on the client, machine A.
import java.rmi.*; public interface ArithmeticInterface extends Remote { int add(inta,int b)throws RemoteException; }
import java.rmi.*; import java.rmi.server.*; public class ArithmeticImplement extends UnicastRemoteObject implements ArithmeticInterface { intx,y; public ArithmeticImplement()throws RemoteException { super(); }
public int add(inta,int b)throws RemoteException { return a+b; } public static void main(String[] args)throws Exception { ArithmeticImplementaImp=new ArithmeticImplement(); Naming.rebind("rmi://localhost:1099/ArithmeticInterface",aImp); } }
import java.rmi.*; import java.io.*; public class ArithmeticClient { public ArithmeticClient() { String rmObj="rmi://localhost:1099/ArithmeticInterface"; try{ ArithmeticInterfaceaInterf= ArithmeticInterface)Naming.lookup(rmObj); intx,y;
InputStreamReader ir=new InputStreamReader(System.in); BufferedReaderkbdInput=new BufferedReader(ir); System.out.println("Enter nos:"); x=Integer.parseInt(kbdInput.readLine()); y=Integer.parseInt(kbdInput.readLine()); System.out.println("Result is "+aInterf.add(x,y)); } catch (Exception e) {} }
public static void main(String[] args)throws Exception { ArithmeticClient client=new ArithmeticClient(); } }
Compile all Java files - javac *.java Create Stub & Skelton - rmicArithmeticImplement Start RMI registry - First Command Prompt rmiregistry 1099 Start Server - Second Command Prompt java ArithmeticImplement Start Client - Third Command Prompt java ArithmeticClient