RMI使用学习 小结

yizhihongxing

RMI使用学习 小结

1. RMI简介

RMI(远程方法调用)是Java编程语言中用于实现远程过程调用的应用程序编程接口。RMI使一个Java虚拟机上的对象能够调用在另一个Java虚拟机上的对象的方法。RMI实现了对象级别的远程过程调用,用户不必关心底层的网络通讯细节。

RMI使用Java远程调用(Java Remote Method Invocation)技术,Java RMI机制使两台计算机的Java虚拟机能够协同工作,其中一台Java虚拟机可以调用另一台Java虚拟机中的方法。

2. RMI使用步骤

RMI对于Java程序员来说相对比较复杂,需要考虑到的因素较多,使用步骤如下:

2.1 实现远程接口

必须有一个java接口,声明了客户端可以远程调用的方法。

public interface HelloWorld extends java.rmi.Remote {
    public String sayHello() throws java.rmi.RemoteException;
}

2.2 实现远程接口的实现类

必须有一个实现了远程接口的实现类。

public class HelloWorldImpl extends java.rmi.server.UnicastRemoteObject implements HelloWorld {
    public HelloWorldImpl() throws java.rmi.RemoteException {
        super();
    }
    public String sayHello() throws java.rmi.RemoteException {
        return "Hello World!";
    }
}

2.3 创建和安装远程对象

可以通过Java RMI提供的rmiregistry工具(位于JDK的bin文件夹下),让客户端通过远程对象来访问实现类的对象。

HelloWorld obj = new HelloWorldImpl();
try {
    // Bind the remote object's stub in the registry
    Registry registry = LocateRegistry.getRegistry();
    registry.bind("HelloWorld", obj);
    System.err.println("Server ready");
} catch (Exception e) {
    System.err.println("Server exception: " + e.toString());
    e.printStackTrace();
}

2.4 启动RMI注册表

需要在服务端启动RMI注册表,使用rmiregistry命令即可。

rmiregistry &

2.5 运行客户端

客户端必须知道实现远程接口的名称或地址。

try {
    // 指定ip和端口号,连接远程对象
    Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
    HelloWorld obj = (HelloWorld) registry.lookup("HelloWorld");// 获取远程对象调用接口方法
    String message = obj.sayHello();
    System.out.println("Message: " + message);
} catch (Exception e) {
    System.err.println("Client exception: " + e.toString());
    e.printStackTrace();
}

3. RMI使用示例

3.1 实现一个简单的计算器

远程接口

public interface Calculator extends java.rmi.Remote {
    public int add(int x, int y) throws java.rmi.RemoteException;
    public int minus(int x, int y) throws java.rmi.RemoteException;
}

远程接口实现类

public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator {
    public CalculatorImpl() throws java.rmi.RemoteException {
        super();
    }
    public int add(int x, int y) throws java.rmi.RemoteException {
        return x + y;
    }
    public int minus(int x, int y) throws java.rmi.RemoteException {
        return x - y;
    }
}

启动服务端和客户端

需要在服务端启动RMI注册表,并将远程对象绑定到注册表;客户端连接注册表,获取远程对象,并调用远程接口中定义的方法。

服务端:

Calculator obj = new CalculatorImpl();
try {
    // Bind the remote object's stub in the registry
    Registry registry = LocateRegistry.getRegistry();
    registry.bind("Calculator", obj);
    System.err.println("Server ready");
} catch (Exception e) {
    System.err.println("Server exception: " + e.toString());
    e.printStackTrace();
}

客户端:

try {
    Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
    Calculator obj = (Calculator) registry.lookup("Calculator");
    // add
    int result1 = obj.add(5, 10);
    System.out.println("5 + 10 = " + result1);
    // minus
    int result2 = obj.minus(10, 5);
    System.out.println("10 - 5 = " + result2);
} catch (Exception e) {
    System.err.println("Client exception: " + e.toString());
    e.printStackTrace();
} 

3.2 实现一个简单的通信程序

远程接口

public interface Chat extends java.rmi.Remote {
    void send(String name, String s) throws java.rmi.RemoteException;
    String get() throws java.rmi.RemoteException;
}

远程接口实现类

public class ChatImpl extends java.rmi.server.UnicastRemoteObject implements Chat {
    String message;
    public ChatImpl() throws java.rmi.RemoteException {
        super();
        message = "";
    }
    public void send(String name, String s) throws java.rmi.RemoteException {
          message = name + ": " + s;
          System.out.println(message);
    }
    public String get() throws java.rmi.RemoteException {
          return message;
    }
}

启动服务端和客户端

需要在服务端启动RMI注册表,并将远程对象绑定到注册表;客户端连接注册表,获取远程对象,并调用远程接口中定义的方法。

服务端:

Chat obj = new ChatImpl();
try {
    // Bind the remote object's stub in the registry
    Registry registry = LocateRegistry.getRegistry();
    registry.bind("Chat", obj);
    System.err.println("Server ready");
} catch (Exception e) {
    System.err.println("Server exception: " + e.toString());
    e.printStackTrace();
}

客户端:

Chat obj = (Chat) Naming.lookup("rmi://localhost/Chat");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String name;
String message;
System.out.println("Please type in your name:");
name = br.readLine();
while (true) {
    System.out.print("Message: ");
    message = br.readLine();
    obj.send(name, message);
}

4. 结论

  • RMI是JAVA中远程方法调用的重要的API和实现工具。
  • 实现RMI需要考虑到的因素较多,实现过程中需要实现远程接口和远程接口实现类,同时运行服务端和客户端。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:RMI使用学习 小结 - Python技术站

(0)
上一篇 2023年6月15日
下一篇 2023年6月15日

相关文章

  • java针对于时间转换的DateUtils工具类

    Java中处理日期时间相关的操作,可以使用Java标准库中的Date类。但是,Date类存在一些问题,如线程不安全、时间戳的精确度不够、不便于进行时间格式化等。因此,在Java平台上,一些常用的时间操作会使用第三方库提供的工具类来进行处理。其中,熟知的DateUtils是封装了一些基于时间转换常见操作的在线性安全、方便使用的工具类。 DateUtils提供了…

    Java 2023年5月20日
    00
  • SpringDataJPA之Specification复杂查询实战

    下面详细讲解“SpringDataJPA之Specification复杂查询实战”的完整攻略。 一、什么是Specification Specification(规范)是Spring Data JPA提供的一种查询定义方式,它可以让我们通过编写Java代码构造查询,从而实现类似HQL的灵活嵌入查询的功能。Specification提供了查询复杂条件时的灵活性…

    Java 2023年5月20日
    00
  • java读取cvs文件并导入数据库

    敬爱的读者,首先感谢您对 Java 编程的热爱。关于如何从CSV文件中读取数据并将其导入数据库,本文将提供一个完整的攻略,详细介绍每个步骤。在本文中,我们将使用Java编写代码来实现该功能。 1. 准备CSV文件 首先,需要准备好包含数据的 CSV 文件。CSV 文件是一种纯文本格式,用于存储和交换以逗号、制表符、分号等分隔符隔开的数据。你可以使用 Micr…

    Java 2023年5月20日
    00
  • SpringBoot controller参数校验方法详细讲解

    下面我就为您讲解一下“SpringBoot controller参数校验方法详细讲解”的攻略。 一、前言 Spring Boot 是一个非常流行的 Java 开发框架,可用于快速构建高效率的应用程序。在我们使用 Spring Boot 进行开发的过程中,请求参数的校验也是非常重要的一环。本文将详细讲解 Spring Boot Controller 参数校验的…

    Java 2023年5月20日
    00
  • javascript获取四位数字或者字母的随机数

    当我们需要生成随机数时,可以使用JavaScript提供的Math.random()方法,并对其进行处理,可以生成指定范围内的随机数字或字母。以下是获取四位数字或字母随机数的完整攻略。 第一步:生成一个随机数 使用JavaScript内置的Math.random()方法可以生成一个0到1之间的随机小数。 const randomNumber = Math.r…

    Java 2023年6月15日
    00
  • Java实现文件监控器FileMonitor的实例代码

    下面我将为您详细介绍Java实现文件监控器FileMonitor的实例代码攻略。 FileMonitor简介 FileMonitor是Java文件监控器的一种实现方式。它可以用于监控指定目录下的文件或文件夹的变化,包括文件的创建、修改、删除等操作,以便及时做出相应的处理。 实现步骤 引入相关依赖 使用FileMonitor需要引入相应的依赖,其中最重要的是c…

    Java 2023年5月20日
    00
  • JavaSpringBoot报错“TransactionSystemException”的原因和处理方法

    当使用Java的Spring Boot框架时,可能会遇到“TransactionSystemException”错误。这个错误通常是由以下原因之一引起的: 事务管理器配置错误:如果事务管理器配置错误,则可能会出现此错误。在这种情况下,需要检查事务管理器的配置并进行必要的更改。 事务注解使用错误:如果事务注解使用错误,则可能会出现此错误。在这种情况下,需要检查…

    Java 2023年5月5日
    00
  • 什么是EVB?EVB技术的简单介绍

    下面是关于EVB的详细讲解。 什么是EVB? EVB全称为Evaluation Board(评估板),是一种硬件开发工具,用于快速评估和开发不同种类的芯片、模块、传感器等硬件设备。它通常包括主板、外设接口、调试器等硬件和相关的软件开发工具。EVB与PCB(Printed Circuit Board,印刷电路板)相比,更注重快速原型和快速评估,能够快速搭建出一…

    Java 2023年6月15日
    00
合作推广
合作推广
分享本页
返回顶部