Java Remote Method Invocation (RMI)

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.

Import java.rmi.*;
Public interface Hello extends Remote {

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

/**
* 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
*/
public string Sayhellotosomebody (String somebodyname) throws RemoteException;
}

Import java.rmi.*;
Import java.rmi.server.*;
public class Helloimpl extends UnicastRemoteObject implements Hello {
/**
* 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
*/
Public Helloimpl () throws RemoteException {
}

/**
* Simple return to "Hello world!" "Words
*
* @return return "Hello world! "Words
* @throws java.rmi.RemoteException
*/
Public String HelloWorld () throws RemoteException {
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
*/
public string Sayhellotosomebody (String somebodyname) throws RemoteException {
Return "Hello," + Somebodyname + "!";
}
}

Import java.rmi.*;
Import java.net.*;
Import java.rmi.registry.*;
public class Server {
public static void Main (String args[]) {

try {
Create a remote object
Hello Rhello = new Helloimpl ();
An instance of the remote object Registry registry on the local host and specifies that the port is 8888, which is necessary (the Java default port is 1099), an indispensable step, the lack 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 of the binding is in the standard format: Rmi://host:port/name (where the protocol name can be omitted, the following two formulations 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 ("Creating a remote object has an exception! ");
E.printstacktrace ();
} catch (Alreadyboundexception e) {
System.out.println ("A duplicate bound object exception occurred! ");
E.printstacktrace ();
} catch (Malformedurlexception e) {
System.out.println ("There is a malformed URL!") ");
E.printstacktrace ();
}
}
}

Import java.rmi.*;
Import java.net.*;
public class Client {
public static void Main (String args[]) {
try {
Look for an object named Rhello in the RMI service registry and call the method on it
Hello Rhello = (hello) 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 ();
}
}
}

Original address: http://lavasoft.blog.51cto.com/62575/91679/

Java Remote Method Invocation (RMI)

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.