是否可以在两个类之间使用双向 RMI?
我想在两个类(A 和 B)之间共享一些信息,这两个类在不同的 java 程序中运行。 我不想编写整个通信协议,而是想使用 java 内置 rmi 类来实现此目的。 目前,B 类能够远程运行属于 A 类的方法。 是否可以在 A 类中使用相同的“连接”来调用 B 类的方法? 否则我可能必须实现第二个 rmi 服务......
BR,
Markus
I want to share some information between two classes (A and B), which are running in different java programs. Instead of writing a whole communication protocol I want to use the java build-in rmi classes for that purpose. Currently class B is able to run a method which belongs to class A remotely. Is it somehow possible to use the same "connection" within class A to call a method of class B? Otherwise I probably have to implement a second rmi service ...
BR,
Markus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果
B
实现Remote
,则可以将其导出并作为 RMI 调用中的参数传递给A
。 在这种情况下,无需在 RMI 注册表中注册B
,因为客户端将显式传递对其的引用。If
B
implementsRemote
, it can be export and passed as a parameter in an RMI call toA
. In this scenario, there's no need to registerB
in an RMI registry, since the client is being passed a reference to it explicitly.我在 cleint 和服务器之间实现了 2 路 RMI,服务器使用注册表公开其存根
以下代码将给出一个更好的主意
I implemented 2 way RMI between cleint and server with server exposing its stub using Registry
The following code will gives a better idea
我已经有一段时间没有使用 RMI 了,但是 IIRC 如果类 B 实现 java.rmi.Remote 并将对其自身实例的引用作为参数传递给类 A 中的方法,则类A 应该接收一个存根,并且调用它的方法将在原始实例上调用。
然而,如果您有大量这样的 RMI 调用来回,您可能会遇到性能问题。
It's been a while since I've used RMI, but IIRC if class B implements
java.rmi.Remote
and passes a reference to an instance of itself as a parameter to the method in class A, then class A should receive a stub and methods called on it will be called on the original instance.However, if you have a lot of such RMI calls going back anf fro, you will probably encounter performance problems.
如果您将
B
作为参数传递给A
中的方法,然后使用该引用调用B
上的方法,我相当确定反向连接已建立,并且我相当确定 RMI 注册表是为B
所在的 JVM 创建的。 在某些时候,这给我们带来了一些特别严格的防火墙规则的麻烦。 我们的代码看起来有点像Web服务器
应用程序服务器
If you pass
B
as an argument to a method inA
and then use that reference to call a method onB
I am fairly certain that a reverse connection is established, and I am fairly certain that RMI Registry is created for the JVM whereB
resides. At some point this got us into a bit of trouble with particularly strict firewall rules. Our code looked a little something likeWeb Server
Application Server
两个 JVM 都需要实现 RMI 服务。 但查看 java.rmi 中的各个类确实非常容易。
您不能做的是以某种方式使用一个 RMI 连接并进行双向通信。
Both JVMs would need to implement RMI services. But that is really very easy look at the various classes in java.rmi.
What you can't do is somehow use one RMI connection and do two way communication.
某些 RMI 服务支持回调或侦听器,允许服务器通过同一连接异步调用客户端。 (抱歉,我不记得执行此操作的开放库的名称,快速谷歌并不是很有帮助)
标准 RMI 不支持这一点,相反您还需要将客户端公开为 RMI 服务。
Some RMI service support callbacks or listeners which allow the server to asynchronous call the client down the same connection. (Sorry I don't remember the name of the open libraries which do this, a quick google wasn't very helpful)
Standard RMI doesn't support this, instead you need to expose the client as an RMI service as well.
RMI 双向:
服务器:
RMI bidirectional:
SERVER:
如果没有第二个 RMI 服务器,B 类如何知道 A 类? 我认为您将需要两台服务器。
How would class B know about class A without a second RMI server though? I think you are going to need two servers.