J2EE--RMI

Source: Internet
Author: User
RMI:

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

 

Use: to define a remote interface class:
<Span style = "font-size: 18px;"> Import Java. RMI. remote; import Java. RMI. remoteException; // defines a remote interface, which must inherit the remote interface. The RemoteException must be thrown for methods to be remotely called. Public interface ihello extends remote {Public String helloworld () throws RemoteException; public String sayhellotoperson (string username) throws RemoteException;} </span>
Remote interface implementation class:
<Span style = "font-size: 18px;"> Import Java. RMI. remoteException; import Java. RMI. server. extends; public class helloimplc extends implements ihello {protected helloimplc () throws RemoteException {} Public String helloworld () throws RemoteException {return "";} Public String sayhellotoperson (string username) throws RemoteException {return "a hello a" + username + "welcome" ;}}</span>
The unicastremoteobject class must be inherited. At the same time, the unicastremoteobject constructor throws a RemoteException. Therefore, the default constructor must be written and the RemoteException must be declared.
The following are important servers responsible for creating the RMI registry, starting the RMI service, and registering remote objects to the RMI registry.
<Span style = "font-size: 18px;"> Import java.net. malformedurlexception; import Java. RMI. alreadyboundexception; import Java. RMI. naming; import Java. RMI. remoteException; import Java. RMI. registry. locateregistry; public class helloserver {public static void main (string [] ARGs) {try {// create a remote object ihello rhello = new helloimple (); // remote object registry instance on the local host, and specify the port to 8888. // this step is required (Java default port is 1099, the registry cannot be bound if it is missing Set the object to locateregistry on the remote registry. createregistry (8888); // register the remote object to the RMI registration server and name it rhello // the URL to be bound in the standard format: RMI: // host: port/name naming. BIND ("RMI: // 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! ");} 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 () ;}}</span>

Call the remote method code on the client:
<Span style = "font-size: 18px;"> Import java.net. malformedurlexception; import Java. RMI. naming; import Java. RMI. notboundexception; import Java. RMI. remoteException; public class helloclient {public static void main (string ARGs []) {try {// search for an object named rhello in the RMI service registry, and call the method ihello rhello = (ihello) naming. lookup ("RMI: // localhost: 8888/rhello"); system. out. println (rhello. helloworld (); system. out. println (rhello. sayhellotoperson ("well-off");} catch (notboundexception e) {e. printstacktrace ();} catch (malformedurlexception e) {e. printstacktrace ();} catch (RemoteException e) {e. printstacktrace () ;}}</span>

Running result: run the RMI server program. Result:


Result of running the RMI client program:

Through this example, we can see that as long as the client implements the remote call interface on the server side and knows the server address, the method on the server side can be called.


Summary:

From the above process, RMI depends closely on the server IP address and port, but it 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 WebService 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.