I have 2 separate questions about Java RMI in a simple hello-world client-server application using RMI at http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/hello/hello-world.html
(1) In the Server code, is the registry.bind("Hello", stub); a blocking-call ? I want to eventually make my program peer-to-peer so I would need my server to continue after exporting an interface. Is there a way to continue execution in my server main after I export my interface ?
(2) In my remote method executing on the server I return a String. Suppose my client crashes while just before the remote method on server wants to return. Can I detect this in my server remote method. Specifically can this work in the remote method -
..
try {
return str;
} catch (Exception e) {
System.err.println("Client seems to be down so cannot return"); return str;
}
The above is roughly what I want to achieve and the exception does not seem to be triggered: (a) Do I need to throw a particular type of exception ? http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/guide/rmi/spec/rmi-exceptions.doc.html (b) If this will even work, I still need to return something in the catch {} block to not get a compilation error. That seems logically incorrect.
I can do the reverse i.e - In client I receive an exception is the Server crashes so my remote method cannot return, but here I need to detect in the server if the client has died before the remote method in server returns.
(1) In the Server code, is the registry.bind("Hello", stub); a blocking-call ? I want to eventually make my program peer-to-peer so I would need my server to continue after exporting an interface. Is there a way to continue execution in my server main after I export my interface ?
It is blocking for the duration of the call, but all the call has to do essentially is HashMap.add() with a bit of error checking. It doesn't prevent continued execution of the code after it while something external happens, in the sense that accept() does.
(2) In my remote method executing on the server I return a String. Suppose my client crashes while just before the remote method on server wants to return. Can I detect this in my server remote method. Specifically can this work in the remote method
No. The RMI framework may detect and handle the case after your return statement has executed, but you won't be told about it. It will log the problem and close the connection.
I need to detect in the server if the client has died before the remote method in server returns.
You can't. The only way provided by RMI to know about disappearing clients is the Unreferenced interface.