java.rmi.ConnectException:当 RMI 服务器是 RMI 客户端的子进程时,连接被拒绝托管 [英] java.rmi.ConnectException: Connection refused to host when RMI server is child process of RMI client

查看:70
本文介绍了java.rmi.ConnectException:当 RMI 服务器是 RMI 客户端的子进程时,连接被拒绝托管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Java 程序 - RMIServer 和 RMIClient.如果我将它们作为两个单独的 Java 调用启动,一切都按预期工作.那是

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

但在我的项目中,碰巧我需要客户端 java 程序来生成服务器,然后连接到它.我同时使用了 runtime.exec() 方法和 ProcessBuilder 来生成服务器实例,但是当我这样做时,我得到以下内容:

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:

但有趣的是,如果手动杀死 RMIClient 实例,RMIServer 将变为孤立的(父为 pid:1),然后如果我运行 RMIClient,我可以连接到孤立的 RMI 服务器就好了!

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!

因此,当 RMIServer 和 RMIClient 具有父子关系时,我无法通过 RMI 进行连接.

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*****(显然我没有足够的声誉来回答我自己的问题:-)

*****ANSWER***** (Apparently I do not have enough reputation to answer my own question :-)

这是我的错误.RMIServer 和 RMIClient 类在两个独立的 Java 线程中实例化.在 RMIServer 注册远程对象之前,用于实例化的 RMIClient 类,因此我收到此异常.

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.

推荐答案

碰巧我需要客户端 java 程序来生成服务器然后连接到它

it so happens that I need the client java program to spawn the server and then connect to it

为什么?在这种情况下,您甚至不需要 RMI,更不用说第二个 JVM.只需在当前JVM中创建远程对象并直接调用其方法即可.

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 本身完全不知道父/子关系,所以我不会进行这种调查.异常字符串Connection denied to host: 0:0:0:0:0:0:0:1"非常可疑.你从哪里得到远程对象的存根?这就是连接目标的来源.

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.

这篇关于java.rmi.ConnectException:当 RMI 服务器是 RMI 客户端的子进程时,连接被拒绝托管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆