如何获取java.rmi.Remote实例的状态
How to obtain the status of an instance of java.rmi.Remote
我有一个 class A
的实例,它实现了 java.rmi.Remote
.
为了检查与 RMI 服务器的连接的健康状况,我调用了 A
实例的一个定制的普通成员函数,并查看是否抛出异常。那不是很优雅。因此我的问题:
是否有任何 native 方法来检查连接是否可用于 A
实例上的方法调用,即不需要实际尝试调用成员函数?
一个特例是: 如果 RMI 服务器在客户端 A
实例的生命周期内重新启动,那么 A
变得无效和失效(尽管服务器可能已备份并且健康)。
来自 Java RMI FAQ :
F.1 At what point is there a "live" connection between the client and
the server and how are connections managed?
When a client does a "lookup" operation, a connection is made to the
rmiregistry on the specified host. In general, a new connection may or
may not be created for a remote call. Connections are cached by the
Java RMI transport for future use, so if a connection is free to the
right destination for a remote call, then it is used. A client cannot
explicitly close a connection to a server, since connections are
managed at the Java RMI transport level. Connections will time out if
they are unused for a period of time.
您的问题:
- Is there any native way to check if the connection is available for
method invocation on the instance of A, i.e. without the need to
actually try to call a member function?
这个问题归结为如何以编程方式检查给定的 server/system 是否正常。这个问题已经在这里和其他几个论坛上回答了好几次。回答这个问题的一个这样的问题是 Checking if server is online from Java code.
- A special case is: Should the RMI server be restarted during the
lifetime of the instance of A on the client side, then the instance of
A becomes invalid and defunct (although the server might be back up
and healthy).
话又说回来,答案很简单。如果 class 的实例正忙于执行远程方法调用中的某个步骤,则会立即抛出与连接相关的异常。
同样,from RMI FAQ, D.8 Why can't I get an immediate notification when a client crashes?:
If a TCP connection is held open between the client and the server
throughout their interaction, then the server can detect the client
reboot(I'm adding here: and vice-versa) when a later attempt to write to the connection
fails (including the hourly TCP keepalive packet, if enabled).
However, Java RMI is designed not to require such permanent
connections forever between client and server(or peers), as it impairs
scalability and doesn't help very much.
Given that it is absolutely impossible to instantly determine when a
network peer crashes or becomes otherwise unavailable, you must decide
how your application should behave when a peer stops responding.
查找将继续完美地工作,直到服务器启动并且在客户端对远程方法执行操作时不会关闭。您必须在此处决定您的应用程序在对等方重新启动时的行为方式。另外,RMI中没有session这个概念。
我希望这能回答你所有的问题。
你的问题是建立在谬论之上的。
提前知道状态对你没有任何帮助。状态测试后跟一个时间window,然后是您对服务器的使用。在时间 window 期间,状态可能会发生变化。服务器可能在您测试时启动,在您使用时关闭。也可能是测试时下降,使用时上升。
判断任何资源是否可用的正确方法是尝试使用它。这适用于输入文件、RMI 服务器、Web 系统...
Should the RMI server be restarted during the lifetime of the instance of A on the client side, then the instance of A becomes invalid and defunct (although the server might be back up and healthy).
在这种情况下,您将获得 java.rmi.ConnectException
或 java.rmi.NoSuchObjectException
,具体取决于远程对象是在不同端口还是相同端口上重新启动。
我有一个 class A
的实例,它实现了 java.rmi.Remote
.
为了检查与 RMI 服务器的连接的健康状况,我调用了 A
实例的一个定制的普通成员函数,并查看是否抛出异常。那不是很优雅。因此我的问题:
是否有任何 native 方法来检查连接是否可用于 A
实例上的方法调用,即不需要实际尝试调用成员函数?
一个特例是: 如果 RMI 服务器在客户端 A
实例的生命周期内重新启动,那么 A
变得无效和失效(尽管服务器可能已备份并且健康)。
来自 Java RMI FAQ :
F.1 At what point is there a "live" connection between the client and the server and how are connections managed?
When a client does a "lookup" operation, a connection is made to the rmiregistry on the specified host. In general, a new connection may or may not be created for a remote call. Connections are cached by the Java RMI transport for future use, so if a connection is free to the right destination for a remote call, then it is used. A client cannot explicitly close a connection to a server, since connections are managed at the Java RMI transport level. Connections will time out if they are unused for a period of time.
您的问题:
- Is there any native way to check if the connection is available for method invocation on the instance of A, i.e. without the need to actually try to call a member function?
这个问题归结为如何以编程方式检查给定的 server/system 是否正常。这个问题已经在这里和其他几个论坛上回答了好几次。回答这个问题的一个这样的问题是 Checking if server is online from Java code.
- A special case is: Should the RMI server be restarted during the lifetime of the instance of A on the client side, then the instance of A becomes invalid and defunct (although the server might be back up and healthy).
话又说回来,答案很简单。如果 class 的实例正忙于执行远程方法调用中的某个步骤,则会立即抛出与连接相关的异常。
同样,from RMI FAQ, D.8 Why can't I get an immediate notification when a client crashes?:
If a TCP connection is held open between the client and the server throughout their interaction, then the server can detect the client reboot(I'm adding here: and vice-versa) when a later attempt to write to the connection fails (including the hourly TCP keepalive packet, if enabled). However, Java RMI is designed not to require such permanent connections forever between client and server(or peers), as it impairs scalability and doesn't help very much.
Given that it is absolutely impossible to instantly determine when a network peer crashes or becomes otherwise unavailable, you must decide how your application should behave when a peer stops responding.
查找将继续完美地工作,直到服务器启动并且在客户端对远程方法执行操作时不会关闭。您必须在此处决定您的应用程序在对等方重新启动时的行为方式。另外,RMI中没有session这个概念。
我希望这能回答你所有的问题。
你的问题是建立在谬论之上的。
提前知道状态对你没有任何帮助。状态测试后跟一个时间window,然后是您对服务器的使用。在时间 window 期间,状态可能会发生变化。服务器可能在您测试时启动,在您使用时关闭。也可能是测试时下降,使用时上升。
判断任何资源是否可用的正确方法是尝试使用它。这适用于输入文件、RMI 服务器、Web 系统...
Should the RMI server be restarted during the lifetime of the instance of A on the client side, then the instance of A becomes invalid and defunct (although the server might be back up and healthy).
在这种情况下,您将获得 java.rmi.ConnectException
或 java.rmi.NoSuchObjectException
,具体取决于远程对象是在不同端口还是相同端口上重新启动。