Java RMI's HelloWorld Chapter

Source: Internet
Author: User

Java RMI refers to remote method invocation (invocation). It is a mechanism that enables an object on one Java virtual machine to invoke a method on an object in another Java virtual machine. Any object that can be called with this method must implement the remote interface. Java RMI is not a new technology (in the era of Java1.1), but it is a very important underlying technology. The famous EJBs are built on RMI, and there are open-source remote calling components, and the underlying technology is RMI. In the era of Web service, SOA, is not every application should choose clumsy Web service components to achieve, through the comparison test, RMI is the simplest, in some small applications is the most appropriate. Here is a simple example of the principle and application of RMI, the following example is a simple HelloWorld, but already covers the core application and development model of RMI./**
* Created by IntelliJ idea.
* User:leizhimin
* Date:2008-8-7 21:50:02
* Defines a remote interface that must inherit from the remote interface, where the method requiring a call must be thrown remoteexception exception
*/
PublicInterfaceIhelloextendsRemote {

/**
* Simple return to "Hello world!" "Words
* @return return "Hello world! "Words
* @throws java.rmi.RemoteException
*/
PublicString HelloWorld ()throwsRemoteException;

/**
* A simple business method that returns the appropriate greeting according to the name of the incoming person
* @param somebodyname names
* @return return the appropriate greeting
* @throws java.rmi.RemoteException
*/
PublicString Sayhellotosomebody (String somebodyname)throwsRemoteException;
}/**
* Created by IntelliJ idea.
* User:leizhimin
* Date:2008-8-7 21:56:47
* Implementation of the remote interface
*/
Public classHelloimplextendsUnicastRemoteObjectImplementsIhello {
/**
* Because the UnicastRemoteObject constructor throws a RemoteException exception, the default constructor here must be written and must declare a RemoteException exception to be thrown
*
* @throws RemoteException
*/
PublicHelloimpl ()throwsremoteexception {
}

/**
* Simple return to "Hello world!" "Words
*
* @return return "Hello world! "Words
* @throws java.rmi.RemoteException
*/
PublicString HelloWorld ()throwsremoteexception {
return"Hello world!";
}

/**
* A simple business method that returns the appropriate greeting according to the name of the incoming person
*
* @param somebodyname names
* @return return the appropriate greeting
* @throws java.rmi.RemoteException
*/
PublicString Sayhellotosomebody (String somebodyname)throwsremoteexception {
return"Hello,"+ Somebodyname +"!";
}
}/**
* Created by IntelliJ idea.
* User:leizhimin
* Date:2008-8-7 22:03:35
* Create the RMI registry, start the RMI service, and register the remote object in the RMI registry.
*/
PublicclassHelloServer {
PublicStaticvoidMain (String args[]) {

Try{
//Create a remote object
Ihello Rhello =NewHelloimpl ();
///The remote object registry on the local host is an instance of registry and specifies that the port is 8888, which is essential (the Java default port is 1099), an indispensable step, the absence of registry creation, the inability to bind the object to the remote registry
Locateregistry.createregistry (8888);

//Register the remote object on the RMI registration server and name it Rhello
the URL standard format for the//binding is: Rmi://host:port/name (where the protocol name can be omitted, the following two types of notation are correct)
Naming.bind ("RMI://localhost:8888/rhello ", Rhello);
//Naming.bind ("//localhost:8888/rhello", Rhello);

System.out.println (">>>>>info: Remote Ihello object binding succeeded! ");
}Catch(RemoteException e) {
System.out.println ("An exception occurred creating the remote Object!" ");
E.printstacktrace ();
}Catch(Alreadyboundexception e) {
System.out.println ("A duplicate bound object exception occurred!" ");
E.printstacktrace ();
}Catch(Malformedurlexception e) {
System.out.println ("A malformed URL has occurred!" ");
E.printstacktrace ();
}
}
} /**
* Created by IntelliJ idea.
* User:leizhimin
* Date:2008-8-7 22:21:07
* Client tests, the client invokes the remote method on the remote object, and returns the result.
*/
Publicclasshelloclient {
PublicStaticvoidMain (String args[]) {
Try{
//Find an object named Rhello in the RMI service registry and invoke 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 RMI Server program: Run RMI client program: summary: From the above process, RMI on the server IP address and port dependence is very close, but in the development of the time do not know the future server IP and port, but the client program depends on this IP and port. This is one of the limitations of RMI. There are two ways to solve this problem: one is through DNS, and the other is to expose IP to program code through encapsulation. The second limitation of RMI is that RMI is a remote invocation of the Java language, the programming language must be Java implementation, the communication between different languages can be considered by the Web service or the Common Object Request Agent system (CORBA) to achieve. Http://java.sun.com/javase/6/docs/technotes/guides/rmi/index.html http://java.sun.com/javase/6/docs/technotes/ Guides http://docs.huihoo.com/java/rmi/whitepage/index.html

Java RMI's HelloWorld Chapter

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.