java - RMI 客户端如何查看并获取在注册表中注册的所有动态远程对象?

标签 java rmi rmiregistry

我有一个要求,即 RMI 客户端需要获取在远程 RMI 注册表中注册的所有远程对象。

第一个问题,我知道有一个 registry.list() 方法可以返回对象的所有名称。但是,如果还注册了其他类型的对象,我该如何获得我想要的类型的对象(实现我想要的接口(interface))。我应该遍历名称并使用 try/catch 语句,尝试实例化每个远程对象吗?

第二个问题,如果注册了一个新的对象,客户端如何才能被注意到?在我的要求中,服务器会将新对象动态注册到注册表中,客户端需要尽快更新并访问新对象。难道我只用一个线程周期性地列出所有的名字来找出新的对象吗?

请注意,远程对象将从不同的节点导出,而不是从同一节点导出。我知道它们是否来自同一个节点,可能我可以使用从实例化远程对象之一调用的回调功能。

最佳答案

How can RMI client sees and get all dynamic remote objects registered in a registry?

通过调用 Naming.list()Registry.list()。

I have a requirement that the RMI client needs to get all remote objects registered in a remote RMI registry.

见上文。

How do I get the objects of exactly the type I want (implementing the interface that I want), supposed that there are also other kinds of objects registered as well? Shall I traverse the names and use a try/catch statement, trying to instanced each remote objects?

只遍历list()返回的名字; lookup() 每一个;并使用 instanceof 判断是否是你需要的类型。如果注册表包含您在客户端没有所有必要类的 stub ,您将需要捕获 ClassNotFoundException。以更少的步骤完成所有这些的简单方法是通过 JNDI listBindings()方法。

Second question, how can a client gets noticed if a new object is registered?

不能。没有为 RMI 注册表定义的监听器系统。客户端必须进行轮询。

In my requirement, the servers will dynamicly registered new objects in to registry, and the client needs to get updated and access the new objects sooner. Shall I just use a thread to periodically list all names to find out new objects?

是的。

Please note that the remote objects are to be exported from different nodes, but not from a same node.

你会发现这很难安排,因为你只能调用 bind() 和来自与注册表相同的主机的 friend 。您必须组织一些中间远程对象来为每个非本地节点进行注册。

I know if they are from the same node, possibly I can use the call-back feature called from one of the instanced remote-object.

来自同一节点并不是这样做的先决条件。您可以在防火墙不妨碍的任何拓扑中执行 RMI 回调。

关于java - RMI 客户端如何查看并获取在注册表中注册的所有动态远程对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32940389/

相关文章:

java - 将 XML 与 xmlunit 进行比较时忽略文本差异

java - Spring Boot 2 健康执行器默认映射

java - 序列化 MBeanServerConnection 以便由 RMI 的远程方法返回

java - 使用 ProcessBuilder 时客户端无法连接 RMI 注册表

java - RMI - 在同一台机器上工作,而不是在 LAN 中

java - JCombobox 和 JTextfield

java - 在 Mac OS (Snow Leopard) 上切换 J2SE 版本

programming-languages - CORBA/RMI 和 (D) COM 在今天还有用吗?

java - 客户端的 RMI 可激活错误