I have an RMI server which publishes an object like so:
int port = ....;
String name = "...";
Remote server = UnicastRemoteObject.exportObject(someobject, port+1);
Registry registry = LocateRegistry.createRegistry(port);
registry.rebind(name, server);
log.info("created service name "+name+", on port "+(port+1)
+" in registry on port "+port);
When I stop the program and just rerun it, it logs that it has created everything, but then immediately exits, likely due to the rmi server thread exiting (the last non-daemon). I registered a defaultUncaughtExceptionHandler
, but get nothing logged from it. Only after a minute or so I am able to restart the program without it immediately exiting.
If I would always use the same port, this would only half surprise me, because I know that ports may be blocked a little while after their last use. But the behavior is the same even if I use a different port (+100 or so) each time.
Any ideas why this happens and how to possible prevent it?
You need to store the result of createRegistry()
into a static variable. Otherwise it is liable to be garbage-collected.