I'm now using LocateRegistry.createRegistry(1099)
rathern than using the registry in a external process. However the registry dies after the main program ends. For instance, if I make a simple program that creates the registry it'll not work because after the main executino the code ends. I was expecting the LocateRegistry
code to create a thread, but it seems that this is not the case. Is this the normal behavior of using LocateRegistry
or I'm missing something?
Code sample:
// ommited imports
public class RMITest {
public static void main(String[] args) {
LocateRegistry.createRegistry(1099);
// JVM will exit now!!!
}
}
The RMI Server start and suddenly dies. How
I was expecting the LocateRegistry code to create a thread
It's not that simple.
Exporting the first object on a new port creates a thread that listens on that port, and unexporting the last object listening on a port causes that thread to exit. This is for all remote objects, not just local Registry objects.
Unexporting can happen automatically via local GC, which in turn can be trigged by remote DGC.
Your JVM exits because you aren't saving the value returned by LocateRegistry.createRegistry()
in a static variable, so it gets GC'd, so the object gets unexported, so there are no remote objects exported on port 1099, so the thread that listens on 1099 exits, so there are no non-daemon threads, so the JVM exits.
Solution: store the result of LocateRegistry.createRegistry()
in a static variable. You can use that to unexport the Registry when you want your JVM to exit.