Java RMI detailed introduction and simple examples

  • 2020-06-12 08:59:00
  • OfStack

Java RMI,

Summary:

Java RMI refers to remote method calls (Remote Method Invocation). It is a mechanism for objects on one Java virtual machine to invoke methods on objects on 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 well-known EJB is based on rmi, there are now a number of open source remote invocation components, the underlying technology is also rmi.

In the age of Web Service and SOA, should every application be implemented with the clunky Web Service component? After comparative testing, RMI is the simplest and most suitable for 1 small application.

The following example illustrates the principle and application of RMI with a simple example, which covers the core application and development pattern of RMI.


 
/** 
* Created by IntelliJ IDEA. 
* User: leizhimin 
* Date: 2008-8-7 21:50:02 
*  define 1 A remote interface that must be inherited Remote Interface, where methods that require remote invocation must be thrown RemoteException abnormal  
*/ 
public interface IHello extends Remote { 

  /** 
   *  Simply return" Hello World ! " With the words  
   * @return  Return" Hello World ! " With the words  
   * @throws java.rmi.RemoteException 
   */ 
  public String helloWorld() throws RemoteException; 

  /** 
   * 1 A simple business method that returns a greeting based on the person's name passed in  
   * @param someBodyName  The person's name  
   * @return  Return the appropriate greeting  
   * @throws java.rmi.RemoteException 
   */ 
  public String sayHelloToSomeBody(String someBodyName) throws RemoteException; 
}
 


/** 
* Created by IntelliJ IDEA. 
* User: leizhimin 
* Date: 2008-8-7 21:56:47 
*  Remote interface implementation  
*/ 
public class HelloImpl extends UnicastRemoteObject implements IHello { 
  /** 
   *  because UnicastRemoteObject Is thrown RemoteException Exception, so the default constructor here must be written and thrown must be declared RemoteException abnormal  
   * 
   * @throws RemoteException 
   */ 
  public HelloImpl() throws RemoteException { 
  } 

  /** 
   *  Simply return" Hello World ! " With the words  
   * 
   * @return  Return" Hello World ! " With the words  
   * @throws java.rmi.RemoteException 
   */ 
  public String helloWorld() throws RemoteException { 
    return "Hello World!"; 
  } 

  /** 
   * 1 A simple business method that returns a greeting based on the person's name passed in  
   * 
   * @param someBodyName  The person's name  
   * @return  Return the appropriate greeting  
   * @throws java.rmi.RemoteException 
   */ 
  public String sayHelloToSomeBody(String someBodyName) throws RemoteException { 
    return " hello " + someBodyName + "!"; 
  } 
}


/** 
* Created by IntelliJ IDEA. 
* User: leizhimin 
* Date: 2008-8-7 22:03:35 
*  create RMI Registry, start RMI Service and register the remote object to RMI In the registry.  
*/ 
public class HelloServer { 
  public static void main(String args[]) { 

    try { 
      // create 1 Three remote objects  
      IHello rhello = new HelloImpl(); 
      // Remote Object registry on localhost Registry , and specify the port as 8888 , this 1 Step is essential ( Java The default port is 1099 ) Indispensable 1 Step, without registry creation, the object cannot be bound to a remote registry  
      LocateRegistry.createRegistry(8888); 

      // Register the remote object with RMI Register on the server and name as RHello 
      // The binding of URL The standard format 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 binding successful! "); 
    } catch (RemoteException e) { 
      System.out.println(" An exception has occurred when creating a remote object! "); 
      e.printStackTrace(); 
    } catch (AlreadyBoundException e) { 
      System.out.println(" A duplicate bound object exception has occurred! "); 
      e.printStackTrace(); 
    } catch (MalformedURLException e) { 
      System.out.println(" happen URL Abnormal! "); 
      e.printStackTrace(); 
    } 
  } 
}


 
/** 
* Created by IntelliJ IDEA. 
* User: leizhimin 
* Date: 2008-8-7 22:21:07 
*  A client test where a remote method on a remote object is called on the client and the result is returned.  
*/ 
public class HelloClient { 
  public static void main(String args[]){ 
    try { 
      // in RMI The lookup name in the service registry is RHello Object and calls methods on it  
      IHello rhello =(IHello) Naming.lookup("rmi://localhost:8888/RHello"); 
      System.out.println(rhello.helloWorld()); 
      System.out.println(rhello.sayHelloToSomeBody(" The lava ")); 
    } catch (NotBoundException e) { 
      e.printStackTrace(); 
    } catch (MalformedURLException e) { 
      e.printStackTrace(); 
    } catch (RemoteException e) { 
      e.printStackTrace();  
    } 
  } 
}

Run RMI server program:

Run the RMI client program:

Conclusion:

From the above procedure, RMI is tightly dependent on the server's IP address and port, but it is not known at the time of development what the future server IP and port will be like, but the client program relies on this IP and port.

This is one of the limitations of RMI. There are two ways to solve this problem: 1 by using DNS, and 2 by encapsulating IP to expose it outside the program code.
The second limitation of RMI is that RMI is a remote call of Java language, and the program languages on both ends must be Java implementation. For communication between different languages, Web Service or common object request proxy system (CORBA) can be considered to achieve this.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: