欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RMI流程简述

程序员文章站 2024-02-14 14:05:22
...

RMI通过proxy和socket实现了远程的方法调用。让我们能方便的想调用本地方法一样调用远程的方法。

调用流程

RMI流程简述

公共包

服务端和客户端必须都有,并且包名一致

/**
 * 消息接口
 */
package core.rmi.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
// Remote 接口用于标识其方法可以从非本地虚拟机上调用的接口
public interface MessageService extends Remote {

    MessageDto getMessage() throws RemoteException;
}


/**
 * 消息类,需要序列化
 */
package core.rmi.common;
import java.io.Serializable;

public class MessageDto implements Serializable {

    private static final long serialVersionUID = 1L;

    private String content;
    private String sender;
    private String receiver;

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

服务端

/**
 * 消息实现类
 */
package core.rmi.server;
import core.rmi.common.MessageDto;
import core.rmi.common.MessageService;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MessageServiceImp extends UnicastRemoteObject implements MessageService {

    protected MessageServiceImp() throws RemoteException {
    }

    @Override
    public MessageDto getMessage() throws RemoteException {  // 网络不稳定,需要抛出异常
        MessageDto dto = new MessageDto();
        dto.setSender("server");
        dto.setReceiver("client");
        dto.setContent("this is message.");
        System.out.println("send message ... ");
        return dto;
    }
}


/**
 * 测试类
 */
package core.rmi.server;
import core.rmi.common.MessageService;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Test {

    public static void main(String[] args) {
        new Test().test();
    }

    private void test() {
        try {
            Registry registry = LocateRegistry.createRegistry(8888);
            MessageService service = new MessageServiceImp();
            Naming.rebind("rmi://127.0.0.1:8888/messageService", service);
            System.out.println("server is running ... ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

客户端

/**
 * 测试类
 */
package core.rmi.client;
import core.rmi.common.MessageDto;
import core.rmi.common.MessageService;
import java.rmi.Naming;

public class Test {

    public static void main(String[] args) {
        new Test().test();
    }

    private void test() {
        try {
            MessageService service = (MessageService) Naming.lookup("rmi://127.0.0.1:8888/messageService");
            MessageDto message = service.getMessage();
            showMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showMessage(MessageDto dto) {
        StringBuilder builder = new StringBuilder();
        builder.append("sender=").append(dto.getSender()).append(", receiver=").append(dto.getReceiver()).append(", content=").append(dto.getContent());
        System.out.println(builder.toString());
    }
}

注:

客户端、服务端、注册服务,可以在运行在三台机器上。示例中注册服务和服务端放在一起运行。
个人懒得码字,比较喜欢画图和代码搞定,如果想看详细版本可参考java RMI原理详解

相关标签: rmi java