当前位置: 首页 > news >正文

RMI初探



接口


import java.rmi.Remote;
import java.rmi.RemoteException;public interface IFoo extends Remote {String say(String name) throws RemoteException;
}

import java.rmi.Remote;
import java.rmi.RemoteException;public interface IBar extends Remote {String buy(String name) throws RemoteException;
}

实现


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;public class FooImpl /* extends UnicastRemoteObject */ implements IFoo {private int index;public FooImpl() throws RemoteException {this(0);}public FooImpl(int port) throws RemoteException {// ObjectTable.objTableUnicastRemoteObject.exportObject(this, port);}@Overridepublic String say(String name) throws RemoteException {String message = "say" + (index++);System.out.println(message);return name + ": " + message;}
}

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;public class BarImpl implements IBar {private int index;public BarImpl() throws RemoteException {this(0);}public BarImpl(int port) throws RemoteException {// ObjectTable.objTableUnicastRemoteObject.exportObject(this, port);}@Overridepublic String buy(String name) throws RemoteException {String message = "buy" + (index++);System.out.println(message);return name + ": " + message;}
}

两个对象实例化之后, 观察 ObjectTable 类里的 objTable 和 implTable 属性内容
// sun.rmi.transport.ObjectTable
public final class ObjectTable {private static final Map<ObjectEndpoint, Target> objTable = new HashMap();private static final Map<WeakRef, Target> implTable = new HashMap();
}

ObjectTable.objTable 属性内容

在这里插入图片描述


ObjectTable.implTable 属性内容

在这里插入图片描述



Server


import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;public class Server {public static void main(String[] args) throws RemoteException, AlreadyBoundException, MalformedURLException {IBar bar = new BarImpl();IFoo foo = new FooImpl();//方式一Registry registry = LocateRegistry.createRegistry(58082);registry.bind("bar", bar);//方式二Naming.bind("rmi://192.168.31.141:58082/foo", foo);}
}

bind 方法会向 RegistryImpl的Hashtable<String, Remote> bindings属性put操作


执行Server的main方法之后, 服务端 registry 对象的属性如下

在这里插入图片描述


ObjectTable.objTable 属性内容里多了一个RegistryImpl_Stub

在这里插入图片描述



服务端会有一个线程(RMI TCP Accept-58082) 监听 58082 端口, 等待客户端的请求

在这里插入图片描述






客户端


import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;public class Client {public static void main(String[] args) throws Exception {//方式一Registry registry = LocateRegistry.getRegistry("192.168.31.141", 58082);IFoo foo = (IFoo) registry.lookup("foo");System.out.println(foo.say("druid"));//方式二IBar bar = (IBar) Naming.lookup("rmi://192.168.31.141:58082/bar");System.out.println(bar.buy("hikari"));}
}


参考文献

1. RMI源码调试

http://www.lryc.cn/news/221153.html

相关文章:

  • NLP之BM25:BM25算法的简介、相关库、案例应用之详细攻略
  • YOLOv5改进,全维动态卷积
  • TypeScript学习Ts的类型声明,关于类
  • Zabbix监控
  • 2023-11-04:用go语言,如果n = 1,打印 1*** 如果n = 2,打印 1*** 3*** 2*** 如果n = 3,打印
  • 顺序表学习笔记(基础)
  • PyTorch入门学习(十九):完整的模型验证套路
  • YOLO目标检测数据集大全【含voc(xml)、coco(json)和yolo(txt)三种格式标签+划分脚本+训练教程】(持续更新建议收藏)
  • PHP保存时自动删除末尾的空格,phpstorm自动删除空白字符串
  • 2022 icpc杭州站 C. No Bug No Game - 背包dp
  • Temp directory ‘C:\WINDOWS\TEMP‘ does not exist
  • 【单片机基础小知识-如何通过指针来读写寄存器】
  • CountDownTimer倒计时使用
  • MySQL索引事务存储引擎
  • 【服务器使用】vscode winscp进行服务器容器连接(含修改初始密码)
  • Go和JavaScript结合使用:抓取网页中的图像链接
  • 通信协议---串口、RS232、RS485
  • UE5 c++将自定义UserWdiget添加到对应菜单栏
  • 三级缓存【又称提前暴露(early exposure)】
  • 【ARM Coresight 系列文章 3.5 - ARM Coresight -- JTAG-DP(JTAG Debug Port) 详细介绍】
  • 【笔记】回顾JavaWeb结合自身开发的项目——分层解耦与IOC、MySQL简单查询
  • Modelsim 使用教程(5)——Analyzing Waveforms
  • String-固长字符串序列
  • RABC权限模型与Spring Security
  • linux 编译lpthread
  • 工业自动化工厂PLC远程控制网关物联网应用
  • Nginx 实现负载均衡
  • 浅谈测试需求分析
  • 18、Python的编码规范:PEP 8介绍及基本遵循原则
  • AI:48-基于卷积神经网络的气象图像识别