java.rmi.ConnectException:当 RMI 服务器是 RMI 客户端的子进程时,连接被拒绝托管
我有两个 Java 程序 - RMIServer 和 RMIClient。如果我将它们作为两个单独的 Java 调用启动,一切都会按预期工作。那就是
$ java -cp someclasspath server.mainserverclass &
$ java -cp someclasspath client.mainclientclass
但是在我的项目中,碰巧我需要客户端java程序来生成服务器然后连接到它。我使用了 runtime.exec() 方法和 ProcessBuilder 来生成服务器实例,但是当我这样做时,我得到以下结果:
.Exception in thread "main" java.rmi.ConnectException:
Connection refused to host: 0:0:0:0:0:0:0:1; nested exception is:
但有趣的是,如果手动终止 RMIClient 实例,RMIServer成为孤立的(父级是 pid:1),然后如果我运行 RMIClient,我可以很好地连接到孤立的 RMI 服务器!
因此,当 RMIServer 和 RMIClient 具有父子关系时,我无法通过 RMI 连接。
非常感谢任何帮助/指示。
*****回答*****(显然我没有足够的声誉来回答我自己的问题:-)
这是我的错误。 RMIServer 和 RMIClient 类在两个单独的 Java 线程中实例化。 RMIClient 类在 RMIServer 注册远程对象之前实例化,因此出现此异常。
亲子关系与这个问题无关。碰巧这些类的创建顺序是问题的根本原因。
I have two Java programs - RMIServer and RMIClient. Everything just works as expected, if I launch them as two separate Java invocations. That is
$ java -cp someclasspath server.mainserverclass
&
$ java -cp someclasspath client.mainclientclass
But in my project, it so happens that I need the client java program to spawn the server and then connect to it. I used both the runtime.exec()
method and ProcessBuilder to spawn a server instance, but when I do that I get the following:
.Exception in thread "main" java.rmi.ConnectException:
Connection refused to host: 0:0:0:0:0:0:0:1; nested exception is:
But the interesting part is that if manually kill the RMIClient instance, the RMIServer gets orphaned(parent is pid:1
), and then if I run the RMIClient, I can connect to orphaned RMI server just fine!
So, in seems like when the RMIServer and RMIClient have a parent-child relationship, I am unable to connect via RMI.
Any help/pointers is much appreciated.
*****ANSWER***** (Apparently I do not have enough reputation to answer my own question :-)
This was my mistake. The RMIServer and RMIClient classes were instantiated in two separate Java threads. The RMIClient class for instantiated before the RMIServer registered the remote object and hence I get this exception.
The parent child relationship is irrelevant to this problem. It so happened that order in which these classes were getting created was the root cause of the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么?在这种情况下,您甚至不需要 RMI,更不用说第二个 JVM 了。只需在当前JVM中创建远程对象并直接调用其方法即可。
RMI 本身完全不知道父/子关系,所以我不会进行这种调查。异常字符串“连接拒绝托管:0:0:0:0:0:0:0:1”非常可疑。您从哪里获得远程对象的存根?这就是连接目标的来源。
Why? In that circumstance you don't even need RMI let alone the second JVM. Just create the remote object in the current JVM and call its methods directly.
RMI itself is completely unaware of the parent/child relationship so I wouldn't pursue that line of investigation. The exception string 'Connection refused to host: 0:0:0:0:0:0:0:1' is extremely suspicious. Where did you get the stub for the remote object? That's where the connect target comes from.