3

I have an instance to a class A that implements java.rmi.Remote.

In order to check the health of the connection to the RMI Server, I invoke a custom-made, trivial member function of the instance of A and see if an Exception is thrown. That's not really elegant. Therefore my question:

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?

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).

datahaki
  • 600
  • 7
  • 23
  • Doesn't this depend on your code? If your instance is doing something, and has not returned, then instantly you can't perform another method invocation. Just as it returns from the previous invocation, then it'll handle other method call. – Am_I_Helpful Aug 21 '16 at 14:26
  • thanks for the comment! For my question, let's assume that the instance is _not busy_ doing something, i.e. not stuck in a method. My question is concerned only with the connection status of the instance. – datahaki Aug 21 '16 at 14:41
  • If the application is currently running within the scope of the instance, then YES, the object won't be reclaimed by the GC. So, I believe, you can safely assume that the object is available to you to perform any of its method invocation. – Am_I_Helpful Aug 21 '16 at 15:03
  • If the server is not available, then calling the method will throw an exception. I would like to know the status of the server in advance. – datahaki Aug 21 '16 at 15:23
  • 3
    So, the question boils down to check how server is online. So, I'm flagging it as duplicate of http://stackoverflow.com/questions/17147352/checking-if-server-is-online-from-java-code – Am_I_Helpful Aug 21 '16 at 16:45
  • I'd like to think that RMI is not _any_ server. Also, via the instance of Remote i cannot simply _know_ the server. So, I'd like to think that the problem is a bit more specific. – datahaki Aug 21 '16 at 17:08
  • It is not what you'd like to think! Your remote method is executed only because the server is active, else if the server would be down, the method invocation wouldn't execute, citing the reason that the connection refused/timeout exception. – Am_I_Helpful Aug 21 '16 at 17:12
  • i have added a paragraph to the question. – datahaki Aug 21 '16 at 17:15
  • 1
    Possible duplicate: http://stackoverflow.com/questions/19274163/in-java-rmi-how-can-a-client-know-that-its-server-is-dead ? –  Aug 21 '16 at 17:46

2 Answers2

2

From 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.


Your questions :

  1. 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?

This question boils down to how to check programmatically that a given server/system is UP. This question has already been answered several times here and several other forums. One such question which answers this is Checking if server is online from Java code.

  1. 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).

Then again, the answer is pretty easy. If the instance of the class was busy performing a step within the remote method invocation, then there would be a connection related exception thrown instantly.

Again, 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.


The lookup would keep on working perfectly, till the server is UP and doesn't get down while the client is performing operation on the remote method. You must decide here how your application should behave if the peer restarts. Additionally, there is no such concept as session in RMI.

I hope this answers all of your questions.

Community
  • 1
  • 1
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
1

Your question is founded on a fallacy.

Knowing the status in advance doesn't help you in the slightest. The status test is followed by a timing window which is followed by your use of the server. During the timing window, the status can change. The server could be up when you test and down when you use. Or it could be down when you test and up when you use.

The correct way to determine whether any resource is available is to try to use it. This applies to input files, RMI servers, Web systems, ...

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).

In this case you will get either a java.rmi.ConnectException or a java.rmi.NoSuchObjectException depending on whether the remote object restarted on a different port or the same port.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Don't you think there's value in figuring out a way to identify whether the RMI Server is up or not for ex. in case of Connect Exceptions that are intermittent? Shouldn't that be addressed? – py_ios_dev Jan 27 '22 at 18:28