Java RMI remote method call

Source: Internet
Author: User
Java RMI refers to remote method invocation ). 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. Java RMI is not a new technology (in the java1.1 era), but it is a very important underlying technology. The well-known EJB is built on RMI. There are some open-source Remote Call components, and the underlying technology is RMI. In the era of Web Service and SOA, should every application use clumsy web service components for implementation? After comparison and testing, RMI is the simplest, it is most suitable for some small applications. The following is a simple example to describe the principles and applications of RMI. The example below is a simple helloworld, but covers the core applications and development modes of RMI. /**

* Created by intellij idea.

* User: leizhimin

* Date: 21:50:02

* To define a remote interface, you must inherit the remote interface. The RemoteException must be thrown for methods that need to be remotely called.

*/
Public interface ihello extends remote {


/**

* Simply return "Hello world! "

* @ Return returns "Hello world! "

* @ Throws java. RMI. RemoteException

*/

Public String helloworld () throws RemoteException;


/**

* A simple business method returns a greeting based on the input person name

* @ Param somebodyname name

* @ Return returns the corresponding greeting.

* @ Throws java. RMI. RemoteException

*/

Public String sayhellotosomebody (string somebodyname) throws RemoteException;

}/**

* Created by intellij idea.

* User: leizhimin

* Date: 21:56:47

* Remote interface implementation

*/
Public class helloimpl extends unicastremoteobject implements ihello
{

/**

* The unicastremoteobject constructor throws a RemoteException. Therefore, the default constructor must be written and the RemoteException must be declared.

*

* @ Throws RemoteException

*/

Public helloimpl () throws RemoteException {

}


/**

* Simply return "Hello world! "

*

* @ Return returns "Hello world! "

* @ Throws java. RMI. RemoteException

*/

Public String helloworld () throws RemoteException {

Return "Hello world! ";

}


/**

* A simple business method returns a greeting based on the input person name

*

* @ Param somebodyname name

* @ Return returns the corresponding greeting.

* @ Throws java. RMI. RemoteException

*/

Public String sayhellotosomebody (string somebodyname) throws RemoteException {

Return "hello," + somebodyname + "! ";

}

}/**

* Created by intellij idea.

* User: leizhimin

* Date: 22:03:35

* Create an Rmi registry, start the RMI service, and register remote objects in the RMI registry.

*/
Public class helloserver {

Public static void main (string ARGs []) {


Try {

// Create a remote object

Ihello rhello = new helloimpl ();

// The Registry instance of the remote object registry on the local host, and the specified port is 8888. This step is required (the default Java port is 1099). The Registry creation is missing, the object cannot be bound to the remote registry.

Locateregistry. createregistry (8888 );


// Register the remote object to the RMI registration server and name it rhello

// The standard format of the bound URL is: RMI: // host: Port/Name (the Protocol name can be omitted. The following two methods are correct)

Naming. BIND ("RMI: // localhost: 8888/rhello", rhello );
// Naming. BIND ("// localhost: 8888/rhello", rhello );


System. Out. println (">>>> info: the remote ihello object is successfully bound! ");

} Catch (RemoteException e ){

System. Out. println ("an exception occurred when creating a remote object! ");

E. printstacktrace ();

} Catch (alreadyboundexception e ){

System. Out. println ("An error occurred while repeatedly binding objects! ");

E. printstacktrace ();

} Catch (malformedurlexception e ){

System. Out. println ("url malformed! ");

E. printstacktrace ();

}

}

}/**

* Created by intellij idea.

* User: leizhimin

* Date: 22:21:07

* In the client test, the client calls the remote method on the remote object and returns the result.

*/
Public class helloclient {

Public static void main (string ARGs []) {

Try {

// Search for the object named rhello in the RMI service registry and call the method on it

Ihello rhello = (ihello) Naming. Lookup ("RMI: // localhost: 8888/rhello ");

System. Out. println (rhello. helloworld ());

System. Out. println (rhello. sayhellotosomebody ("lava "));

} Catch (notboundexception e ){

E. printstacktrace ();

} Catch (malformedurlexception e ){

E. printstacktrace ();

} Catch (RemoteException e ){

E. printstacktrace ();

}

}

} Run the RMI server program: run the RMI client program: Conclusion: From the process above, RMI is closely dependent on the server IP address and port, however, you do 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.

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.