Java remote method Invocation, Java RMI (Java remote methods invocation), an application programming interface for implementing remote procedure calls. It enables programs that are running on the client computer to invoke objects on the remote server. The Remote method invocation feature enables Java programmers to distribute operations in a network environment. The whole purpose of RMI is to simplify the use of remote interface objects as much as possible. Java RMI relies heavily on interfaces. When you need to create a remote object, the programmer hides the underlying implementation details by passing an interface. The client gets a handle to the remote object that is directly connected to the local root code, which is responsible for communicating through the network. In this way, programmers simply care about how to send messages through their own interface handles.
/* New Interface on server: */public interface Rmitestinterface extends Remote{public String gettest () throws remoteexception;}
/* Implementation of the interface: */public class Rmitestimpl implements Rmitestinterface{public Rmitestimpl () throws remoteexception{}@ Overridepublic String Gettest () throws Remoteexception{return "Hello MM";} public static void Main (string[] args) throws RemoteException {Rmitestimpl t=new rmitestimpl (); Rmitestinterface tt= (Rmitestinterface) unicastremoteobject.exportobject (t, 0); Registry Registry=locateregistry.createregistry (2001); Registry.rebind ("Test", TT); SYSTEM.OUT.PRINTLN ("Server is Start");}}
/*client end of the main program */public class Client {public static void main (string[] args) {try {Registry registry= Locateregistry.getregistry ("localhost", 2001); Rmitestinterface t= (rmitestinterface) registry.lookup ("test"); System.out.println ("Client:" +t.gettest ());} catch (RemoteException e) {e.printstacktrace ();} catch (Notboundexception e) {e.printstacktrace ();}}}
RMI Remote Invoke demo
in Java