Java RMI (remote method call) development

Source: Internet
Author: User
Tags object serialization stub

Reference

Https://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch2.html

Http://www.cnblogs.com/wxisme/p/5296441.html

http://blog.csdn.net/qb2049_xg/article/details/3278672

Http://classfoo.com/ccby/article/p1wgbVn

Http://www.cnblogs.com/yin-jingyu/archive/2012/06/14/2549361.html

RMI and RPC

RMI seems to me to be more like Java-specific RPC, or purely object-oriented RPC. As RPC is a very important content of the distribution, but also the basis of Java message middleware.

RMI principle

The essence is to synchronize a Java object in two places (can be based on JDK its own object serialization or protocol-based HTTP data serialization) A and a!, but one of the Java object A method call, through the RMI proxy ability to be forwarded to another Java object a! execution, and a The results of the execution as a result of the execution. In this way, the distributed computation of the computational process is realized. In general, these two objects exist on two machines.

    1. Serializes an object that can be called remotely, and then binds to the RMI Server (the callee, the runner) as a stub
    2. The RMI client will first download the stub deserialization and then initiate the Client call, and the RMI bottom (RMI Interface layer & Transport layer) will tell the request parameter encapsulation to send to the RMI Server
    3. RMI Server receives the encapsulated parameters, passes to the piles (skeleton), parses the parameters by the piles, and invokes the corresponding stub method with the parameters.
    4. After the stub method is executed by the RMI server, the returned results are encapsulated and transferred to the RMI Client (that is, the keynote party, the caller).

The current Java version does not need to create skeleton, and do not need to rmic to compile stubs, but I am learning to use the rmic compiled stub, the example is also done.

RMI Server Authoring

Write RMI Server Interface, which will also be used on the client (RMI client) for use by clients. Lists the interfaces for open remote calls

1  PackageOrg.lyh.server;2 3 ImportJava.rmi.Remote;4 Importjava.rmi.RemoteException;5 6 /**7 * Created by Lvyahui on 2016/4/22.8  */9  Public InterfaceItimeserverextendsRemote {Ten     LongGetServerTime ()throwsremoteexception; One     intAddintAintbthrowsremoteexception; A}

Writing the time server interface

1  PackageOrg.lyh.server.impl;2 3 ImportOrg.lyh.server.ITimeServer;4 5 Importjava.rmi.RemoteException;6 Importjava.rmi.server.RMIClientSocketFactory;7 Importjava.rmi.server.RMIServerSocketFactory;8 ImportJava.rmi.server.UnicastRemoteObject;9 Ten /** One * Created by Lvyahui on 2016/4/22. A  */ -  Public classTimeserverextendsUnicastRemoteObjectImplementsItimeserver { -  the      PublicTimeserver (intPortthrowsRemoteException { -         Super(port); -     } -  +      PublicTimeserver ()throwsRemoteException { -     } +  A      PublicTimeserver (intPort, Rmiclientsocketfactory CSF, Rmiserversocketfactory SSF)throwsRemoteException { at         Super(Port, CSF, SSF); -     } -  - @Override -      Public LongGetServerTime ()throwsRemoteException { -         returnSystem.currenttimemillis (); in     } -  to @Override +      Public intAddintAintbthrowsRemoteException { -         returnA +b; the     } *}

Binds a well-written server implementation object to the Java RMI Name Service

1  PackageOrg.lyh;2 3 ImportOrg.lyh.server.impl.TimeServer;4 5 Importjava.net.MalformedURLException;6 Importjava.rmi.Naming;7 Importjava.rmi.RemoteException;8 9  Public classMain {Ten      Public Static voidMain (string[] args) { One         Try { ATimeserver timeserver =Newtimeserver (); -             /*on the RMI server bound to the JVM*/ - //naming.bind ("T1", timeserver); the //naming.rebind ("T1", timeserver); -             /*This is required when the RMI registry server is specified with a port or is not in the native runtime*/ -Naming.rebind ("//localhost/t1", timeserver); -System.out.println ("Bind is Finish"); +}Catch(RemoteException e) { - e.printstacktrace (); +}Catch(malformedurlexception e) { A e.printstacktrace (); at         } -     } -}

At this point, to use rmic further compile Timeserver.class file, get timeserver_stub.class stub object file. If you don't use Rmic compilation, you can also get stubs by writing code.

Because I developed it using idea, I went into the Rmi\out\production\rmi directory compilation and executed Rmic Org.lyh.server.impl.TimeServer

This will generate the Timeserver_stub.class file in the same directory

Then start the RMI name registration service, execute the rmiregistry command (a script under Jdk\bin), you can execute Rmiregistry port execution ports, otherwise the default is 1099

Below you can execute the [email protected] method, bind the stub (register) to the RMI name service with the name T1

RMI Client Writing

The RMI client is simple, pulls the stub, then initiates the call, the call is transferred to the server for execution, and the result is obtained, and the returned results are obtained directly from the interface method return.

1  Packageorg.lyh.client;2 3 ImportOrg.lyh.server.ITimeServer;4 5 Importjava.net.MalformedURLException;6 Importjava.rmi.Naming;7 Importjava.rmi.NotBoundException;8 Importjava.rmi.RemoteException;9 Ten /** One * Created by Lvyahui on 2016/4/22. A  */ -  Public classtimeclient { -      Public Static voidMain (string[] args) { the         Try { -Itimeserver itimeserver = (itimeserver) naming.lookup ("Rmi://192.168.18.1/t1"); - System.out.println (Itimeserver.getservertime ()); -System.out.println (Itimeserver.add (3,7)); +}Catch(notboundexception e) { - e.printstacktrace (); +}Catch(malformedurlexception e) { A e.printstacktrace (); at}Catch(RemoteException e) { - e.printstacktrace (); -         } -     } -}

To be more realistic, I uploaded this client to a virtual machine to compile and run

Java RMI (remote method call) development

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.