I have a 3rd-party Java library I'm trying to encapsulate in a separate JVM with RMI (because the library has native calls that crash the JVM, and there are applications for running the RMI server on another machine).
My Java app on the client side works like this, at least when everything runs on the same machine:
- find the object bound to
FooBar/<id>
in the RMI registry - if it's already bound to the registry, use it and goto step 5.
- otherwise, start up a separate JVM to create the object in question.
- wait for a short timeout
- find the object bound to
FooBar/<id>
in the RMI registry - if we 开发者_StackOverflow中文版have an object, success! (otherwise failure)
The problem I have, is when the other JVM crashes, it seems to leave the binding present in the RMI registry, and then I end up using a phantom object connected to nothing.
Is there a way I can somehow check the proxy object bound to the RMI registry in step 2, so if the object is invalid, I can create a new one to rebind() to the RMI registry?
set a timeout on your rmi requests (see here). then if a request times out, consider the object dead. additionally, when i want very reliable rmi code, i will wrap the rmi calls on the client side with some sort of retry/backoff functionality (assuming your outgoing calls are idempotent). thus, you would try connecting a few times with delays in between before considering the target object dead.
(and no, there is no way to determine if the remote object is dead without trying to contact it).
精彩评论