HelloWorld of Java RMI

HelloWorld of Java RMI
 
Java RMI refers to Remote Method Invocation. It is a mechanism that enables an object on one Java virtual machine to call a method on an object in another Java virtual machine. Any object that can be called with this method must implement this remote interface.
 
Java RMI is not a new technology (in the era of Java 1.1), but it is a very important underlying technology.
The famous EJB is built on the basis of rmi, and now there are some open source remote call components, and the underlying technology is also rmi.
 
In the era of vigorously advocating Web Service and SOA, should every application be implemented with clumsy Web Service components? After the comparison test, RMI is the simplest, and it is the most suitable for some small applications.
 
The following is a simple example to illustrate the principle and application of RMI. The following example is a simple HelloWorld, but it has covered the core application and development mode of RMI.
 
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-7 21:50:02
* Define a remote interface, which must inherit the Remote interface, and the methods that need to be called remotely must throw RemoteException
*/

public interface IHello extends Remote {

     /**
     * simply returns the words "Hello World!"
     * @return returns the words "Hello World!"
     * @throws java.rmi.RemoteException
     */

     public String helloWorld() throws RemoteException;

     /**
     * A simple business method that returns the corresponding greeting according to the incoming person name
     * @param someBodyName person name
     * @return returns the corresponding 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
* Implementation of remote interface
*/

public class HelloImpl extends UnicastRemoteObject implements IHello {
     /**
     * Because of the construction of UnicastRemoteObject The method throws RemoteException, so the default constructor must be written here, and it must declare to throw RemoteException
     *
     * @throws RemoteException
     */

     public HelloImpl() throws RemoteException {
    }

     /**
     * Simply return "Hello World!"
     *
     * @return returns the words "Hello World!"
     * @throws java.rmi.RemoteException
     */

     public String helloWorld() throws RemoteException {
         return "Hello World!" ;
    }

     /**
     * A simple business method that returns the corresponding greeting according to the incoming person name
     *
     * @param someBodyName person 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: 2008-8-7 22:03:35
* 创建RMI注册表,启动RMI服务,并将远程对象注册到RMI注册表中。
*/

public class HelloServer {
     public static void main(String args[]) {

         try {
             //创建一个远程对象
            IHello rhello = new HelloImpl();
             //本地主机上的远程对象注册表Registry的实例,并指定端口为8888,这一步必不可少(Java默认端口是1099),必不可缺的一步,缺少注册表创建,则无法绑定对象到远程注册表上
            LocateRegistry.createRegistry(8888);

             //把远程对象注册到RMI注册服务器上,并命名为RHello
             //绑定的URL标准格式为:rmi://host:port/name(其中协议名可以省略,下面两种写法都是正确的)
            Naming.bind( "rmi://localhost:8888/RHello",rhello);
//            Naming.bind("//localhost:8888/RHello",rhello);

            System.out.println(">>>>>INFO:远程IHello对象绑定成功!");
        } catch (RemoteException e) {
            System.out.println("创建远程对象发生异常!");
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            System.out.println("发生重复绑定对象异常!");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("发生URL畸形异常!");
            e.printStackTrace();
        }
    }
}
 
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-7 22:21:07
* 客户端测试,在客户端调用远程对象上的远程方法,并返回结果。
*/

public class HelloClient {
     public static void main(String args[]){
         try {
             //在RMI服务注册表中查找名称为RHello的对象,并调用其上的方法
            IHello rhello =(IHello) Naming.lookup( "rmi://localhost:8888/RHello");
            System.out.println(rhello.helloWorld());
            System.out.println(rhello.sayHelloToSomeBody("熔岩"));
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();  
        }
    }
}
 
运行RMI服务端程序:
 
运行RMI客户端程序:
 
总结:
从上面的过程来看,RMI对服务器的IP地址和端口依赖很紧密,但是在开发的时候不知道将来的服务器IP和端口如何,但是客户端程序依赖这个IP和端口。
这也是RMI的局限性之一。这个问题有两种解决途径:一是通过DNS来解决,二是通过封装将IP暴露到程序代码之外。
RMI的局限性之二是RMI是Java语言的远程调用,两端的程序语言必须是Java实现,对于不同语言间的通讯可以考虑用Web Service或者公用对象请求代理体系(CORBA)来实现。
 
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326936386&siteId=291194637