Remote method invocation
Introduction First introduced in JDK 1.1. Allows distributed Java programs to work with each others by behaving as if they are not distributed programs.
Writing RMI services A remote methods are declared explicitly in the interface, RMI interface definition The interface should extend the Remote interface. In order to write a simple RMI application Follow following steps: –Write the Java RMI interface definition for the application with the specified operations. –Implement the interface. – implement the server application, who is responsible for remote object registration. –Design and implement a Client which invokes the server to invoke remote methods, the client should do the following: Locate the remote objects Link to the remote objects
A simple RMI interface file import java.rmi.*; public interface Interface extends Remote { public int f1(int no) throws java.rmi.RemoteException; public String f2(int sno) throws java.rmi.RemoteException; } //end interface
Implementing the interface import java.io.*; import java.util.*; import java.rmi.*; import java.rmi.server.*; import java.lang.*; import java.text.DecimalFormat; public class Implementation extends UnicastRemoteObject implements Interface { private final int recordSize=88; private static byte[] mutex; public Implementation () throws RemoteException { super( ); }
Implementing the interface public int f1(int no) throws RemoteException { // do something } public String f2(int no)throws RemoteException { // do something } } // end class
Implementing RMI server import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.Registry; import java.rmi.registry.LocateRegistry; import java.net.*; import java.io.*; public class Server { public static void main(String args[]) throws Exception { startRegistry(2000); Implementation exportedObj = new Implementation(); String registryURL = "rmi://localhost:2000/App"; Naming.rebind(registryURL, exportedObj); }
Writing a RMI client import java.io.*; import java.rmi.*; public class Client { public Client(){ } public static void main(String args[]) { try { String registryURL = "rmi://" + localhost: 2000/App"; // find the remote object and cast it to an interface object Interface h = (Interface)Naming.lookup(registryURL); System.out.println("Lookup completed " ); h.f1(5); // Call remote method }
References ng/rmi/RMI.html#IntroRMIhttp://java.sun.com/developer/onlineTraini ng/rmi/RMI.html#IntroRMI overview.htmlhttp://java.sun.com/docs/books/tutorial/rmi/ overview.html varmi/javarmi.htmlhttp:// varmi/javarmi.html