Remote method call-RMI

Source: Internet
Author: User

RMI

RMI (Remote Method Invocation) is an important mechanism for Java to implement transparent remote calls. In remote calls, the client only provides interfaces provided by the server. This interface is used to call remote servers. Its power is reflected in its powerful ability to develop distributed network applications. It is one of the core Solutions of a network distributed application system pure Java. In fact, it can be regarded as the Java version of RPC. However, traditional RPC cannot be well applied to distributed object systems. Java RMI supports communication between Program-level objects stored in different address spaces to achieve seamless remote calls between remote objects.




Sun JDK 6. the r m I Implementation in versions earlier than 0 is based on TCP/IP + Bio. the RMI server listens to external interfaces on a port by starting the RMI registration object, the implementation instance is bound to the RMI registration object as a string. The RMI client proxy represents access to the server interface. The RMI client encapsulates the Server Object strings, methods, and parameters to be accessed into an object, after being serialized into a stream, it is transmitted to the RMI server through TCP/IP + Bio. After receiving the request object from the client, the RMI server parses the object string, method, and parameters, and finds an instance that provides business functions from the RMI registration object through the object string, then, the Method Instance Object is obtained through reflection based on the method to be accessed, and parameters are passed in to call the server-side object instance, the returned result is serialized as a stream and returned to the client in TCP/IP + Bio mode. After receiving the stream, the client deserializes it as an object and returns it to the caller.


Simple Example

Remote Call interface:

/*** Remote interface <br/> * defines a remote interface, must inherit the remote interface ** @ author joeson * @ since 07/2014 */public interface hellormi extends remote {// The RemoteException must be thrown for the method to be remotely called. Public String Hello () throws RemoteException ;}


Remote Call interface implementation:

/***** Remote Call implementation class <br/> * must inherit the unicastremoteobject class, And the RemoteException is thrown due to the unicastremoteobject constructor. * The default constructor must be written here, A RemoteException must be thrown. ** @ Author joeson * @ since 07/2014 **/public class hellormiimpl extends unicastremoteobject implements hellormi {public hellormiimpl () throws RemoteException {super (); // todo auto-generated constructor stub} @ overridepublic string Hello () throws RemoteException {return "Hello, RMI ";}}


RMI server:

/*** RMI server <br/> *** @ author joeson * @ since 07/2014 ***/public class serverrmi {public static void main (string [] ARGs) {try {// create a remote object hellormi Hello = new hellormiimpl (); // instance of the remote object registry on the local host, and specify port 8080, this step is required (the default Java port is 1099) and an indispensable step. If the registry is missing, you cannot bind an object to the locateregistry on the remote registry. createregistry (8080); // register the remote object to the RMI registration server and name it "hello". The bound URL is in the standard format: RMI: // host: Port/namenaming. BIND ("RMI: // localhost: 8080/h Ello ", hello); system. out. println ("RMI Service Startup... ");} catch (RemoteException e) {system. out. println ("an exception occurred when creating a remote object! ");} Catch (alreadyboundexception e) {system. Out. println (" An error occurred while binding duplicate objects! "); E. printstacktrace ();} catch (malformedurlexception e) {system. Out. println (" url malformed! "); E. printstacktrace ();}}}


RMI client implementation:

/*** RMI client <br/> ** @ author joeson * @ since 07/2014 **/public class clientrmi {public static void main (string ARGs []) {try {// search for the object named hello in the RMI service registry and call the method hellormi Hello = (hellormi) Naming on it. lookup ("RMI: // localhost: 8080/Hello"); system. out. println (hello. hello ();} catch (notboundexception e) {e. printstacktrace ();} catch (malformedurlexception e) {e. printstacktrace ();} catch (RemoteException e) {e. printstacktrace ();}}}


Running result



The preceding figure shows that a client calls the server through RMI to implement distributed system calls. The underlying implementation of RMI lies in reflection, serialization, deserialization, and socket programming, the client does not need to provide specific implementation, but interacts with the specific implementation of the server through the interface, and calls the specific implementation of the server to achieve the desired effect of the client, this is the business processing model of the distributed system.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.