JAVA RMI Example

Source: Internet
Author: User

RMI is an RPC framework in the Java language, this article gives the following basic examples:

1. Implement the interface:

 Public Interface extends Remote {    publicint Add (intintthrows  java.rmi.RemoteException;}

2, the server to implement the interface:

 Public classCalcimplextendsUnicastRemoteObjectImplementsIcalc {protectedCalcimpl ()throwsRemoteException {Super(); } @Override Public intAddintPARAM1,intparam2) {        returnParam1 +param2; }     Public Static voidMain (string[] args) {Icalc calc; Try {            //The following code mainly solves the connect confused problemSystem.setproperty ("Java.rmi.server.hostname", "192.168.0.16"); Calc=NewCalcimpl (); Locateregistry.createregistry (1011); Java.rmi.Naming.rebind ("Rmi://192.168.0.16:1011/calc", Calc); } Catch(RemoteException e) {e.printstacktrace (); } Catch(malformedurlexception e) {e.printstacktrace (); }    }}

3. Define the test client:

 Public class Client {    publicstaticvoid  main (string[] args) {        Icalc calc;         Try {            = (icalc) naming.lookup ("Rmi://192.168.0.16:1011/calc");            System.out.println (Calc.add (2,3));         Catch (Malformedurlexception | RemoteException | notboundexception e) {            e.printstacktrace ();     }}}

4. Test execution sequence:

1, server-side compilation Javac Calcimpl

2, create pile file rmic Calcimpl, the directory will be added calcimpl_stub;

3, the implementation of Rmiregister, registration of the procedure;

4. Start the service Java Calcimpl in a new window;

5, copy the calcimpl_stub to the client, execute Java client, the client prints the budget result

JAVA RMI Example

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.