Java RMI-remote method call

Source: Internet
Author: User
 

Recently, when I read the proxy mode in head first design patterns, I mentioned Java RMI, that is, Java remote method call. So I found some information about Java RMI on the Internet.

I. Java RMI Overview

RMI is called Remote Method invocation in English. It is a mechanism that allows objects on a Java Virtual Machine to call methods on objects in another Java virtual machine. Any object that can be called using this method must implement this remote interface.

RMI provides customer-assisted objects and service-assisted objects to create customer-assisted objects in the same way as service objects. The advantage of RMI is that you do not have to write network code and I/O Code yourself. The remote call of a customer Program (that is, the real service) is the same as the normal method call to the object on the JVM running the customer's local machine.

RMI sets the customer's secondary object as the stub, and the service secondary object as the skeleton (skeleton ).

The RMI structure is as follows:

Ii. How to Use RMI for remote method calls in Java?

The procedure is as follows:

1. Create a remote interface

2. Remote production implementation

3. Use rmic to generate stub and skeleton

4. Start RMI Registry (rmiregistry)

5. Start remote service

 

Iii. Example Program

Myremote. Java

/*** Remote server interface */package rmidemo;/*** RemoteException and remote interface in Java. in the RMI package **/import Java. RMI. *;/** defines a remote interface, which must be extended from Java. RMI. remote interface. **/Public interface myremote extends remote {/*** all remote methods must throw a RemoteException. * @ Return * @ throws RemoteException */Public String sayhello () throws RemoteException ;}

Myremoteimpl. Java

/*** Remote service (Implementation) */package rmidemo; import Java. RMI. naming; import Java. RMI. remoteException; import Java. RMI. server. unicastremoteobject;/***** @ author CCF *** // It is the easiest way to expand unicastremoteobject to create a remote object. * The myremote interface is the required remote interface */public class myremoteimpl extends unicastremoteobject implements myremote {/*** you must implement the remote interface. Of course, you must implement all interface methods, but note that you do not need to declare RemoteException. */@ Overridepublic string sayhello () throws RemoteException {// todo auto-generated method stubreturn "server says: 'hes'";}/*** your superclass (unicastremoteobject) the constructor declares an exception, so you must write a constructor. * This means that your constructor is calling Insecure code (its constructor ). * @ Throws RemoteException */Public myremoteimpl () throws RemoteException {}/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubtry {/*** generates a remote object before using naming. rebind () to rmiregistry. * The customer will search for it in RMI registry using the name you registered. */Myremote service = new myremoteimpl (); naming. rebind ("remotehello", service);} catch (exception ex) {ex. printstacktrace ();}}}

Myremoteclient. Java

Package rmidemo;/*** the naming class used for rmiregister lookup is in the Java. Rmi package. * In the client test, the client calls the remote method of the remote object and returns the result. */Import Java. RMI. *; public class myremoteclient {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub} public void go () {try {/** the returned value is of the object type, so do not forget the conversion type. * In the lookup function, you need the IP address or host name, and the name used when the server is bound or rebound. */Myremote service = (myremote) naming. lookup ("RMI: // 127.0.0.1/remotehello"); string S = service. sayhello (); system. out. println (s);} catch (exception ex) {ex. printstacktrace ();}}}

Blog master ninahan's blog Java
The example in helloworld of RMI is similar to the example I excerpted from head first design patterns. He explained it clearly!

Summary:RMI depends closely on the server IP address and port, but does not know the future Server IP address and port during development, but the client program depends on this IP address and port. This is also one of the limitations of RMI. There are two ways to solve this problem: one is to solve the problem through DNS, and the other is to expose the IP address to the program code through encapsulation. The second limitation of RMI is that RMI is a remote call in Java, and the programming languages at both ends must be implemented in Java, for communication between different languages, you can use web service or public Object Request proxy system (CORBA.

Iv. References:

1. Head first design patterns, pp. No. -450.

2. Java RMI server framework: use Asynchronous Process Manager to include RMI server applications

3. helloworld of Java RMI: helloworld of Java RMI

4. Java RMI thread model and internal implementation mechanism: Java RMI thread model and internal implementation mechanism

5. jdk_api_rj6_zh_cn.chm Reference Manual

 

 

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.