Home >Java >JavaBase >Introduction to Java RMI (with examples)

Introduction to Java RMI (with examples)

尚
forward
2019-11-27 13:36:482335browse

Introduction to Java RMI (with examples)

Java RMI refers to Remote Method Invocation. It is a mechanism that allows an object in one Java virtual machine to call methods on an object in another Java virtual machine. Any object that can be called with this method must implement the remote interface. (Recommended: java video tutorial)

Java RMI is not a new technology (it was available in the era of Java 1.1), but it is a very important underlying technology.

The famous EJBs are all based on rmi. There are also some open source remote calling components, and their underlying technology is also rmi.

In the era of vigorously advocating Web Service and SOA, should every application use clumsy Web Service components to implement it? After comparative testing, RMI is the simplest. In some small applications, it is most suitable.

The following uses a simple example to illustrate the principles and applications of RMI. The following example is a simple HelloWorld, but it covers the core applications and development models of RMI.

/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-7 21:50:02
* 定义一个远程接口,必须继承Remote接口,其中需要远程调用的方法必须抛出RemoteException异常
*/
public interface IHello extends Remote {

    /**
     * 简单的返回“Hello World!"字样
     * @return 返回“Hello World!"字样
     * @throws java.rmi.RemoteException
     */
    public String helloWorld() throws RemoteException;

    /**
     * 一个简单的业务方法,根据传入的人名返回相应的问候语
     * @param someBodyName  人名
     * @return 返回相应的问候语
     * @throws java.rmi.RemoteException
     */
    public String sayHelloToSomeBody(String someBodyName) throws RemoteException;
}
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-8-7 21:56:47
* 远程的接口的实现
*/
public class HelloImpl extends UnicastRemoteObject implements IHello {
    /**
     * 因为UnicastRemoteObject的构造方法抛出了RemoteException异常,因此这里默认的构造方法必须写,必须声明抛出RemoteException异常
     *
     * @throws RemoteException
     */
    public HelloImpl() throws RemoteException {
    }

    /**
     * 简单的返回“Hello World!"字样
     *
     * @return 返回“Hello World!"字样
     * @throws java.rmi.RemoteException
     */
    public String helloWorld() throws RemoteException {
        return "Hello World!";
    }

    /**
     * 一个简单的业务方法,根据传入的人名返回相应的问候语
     *
     * @param someBodyName 人名
     * @return 返回相应的问候语
     * @throws java.rmi.RemoteException
     */
    public String sayHelloToSomeBody(String someBodyName) throws RemoteException {
        return "你好," + 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();  
        }
    }
}

Run the RMI server program:

Introduction to Java RMI (with examples)

Run the RMI client program:

Introduction to Java RMI (with examples)

Summary :

From the above process, RMI is very dependent on the IP address and port of the server. However, during development, we do not know what the future server IP and port will be, but the client program depends on this IP and port. .

This is also one of the limitations of RMI. There are two ways to solve this problem: one is to solve it through DNS, and the other is to expose the IP outside the program code through encapsulation.

The second limitation of RMI is that RMI is a remote call in Java language. The programming language at both ends must be implemented in Java. For communication between different languages, you can consider using Web Service or Common Object Request Agency (CORBA). to fulfill.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Introduction to Java RMI (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete