RMI - Naming:lookup & rebind conflict


2003-09-14 04:13:49 PM
jbuilder5
Hi,
I have problem that rebind fails after calling lookup. Why, and what to do
in the between to prevent this from happening. More deails follows.
Thanks.
====DETAILS===
RMI server needs to know if there is an instance already running. If not it
continues witn binding. Here is how I do it.
try {
IIMServer oldServer =
(IIMServer)Naming.lookup("//localhost:1099/applications/XYServer/IMSERVER");
if (oldServer.ping()) {
System.out.println("Server already running. Bye.");
System.exit(-1);
}
} catch (Exception e) {
System.out.println("no old server.");
}
This above part works fine. However the following does fail and the flow
turns into
the exception and finally exits with -1.
System.setSecurityManager(new RMISecurityManager());
URL url = ClassLoader.getSystemResource("classes"); // app's root dir
must be in classpath !!
System.setProperty("java.rmi.server.codebase", url.toString()+"/");
try {
Naming.rebind("//localhost:1099/applications/XYServer/IMSERVER",
this);
} catch (Exception e) {
e.printStackTrace();
System.exit( -1);
}
System.out.println("Server created.");
It is fascinating that if I remove first part of code (which seeks for old
instance of server)
the second part works perfectly. (btw: policy and codebase are set nicely
and properly).
Why does that happen ? I am astonished. If anyone had this problem please
let me know the solution.
Kindest regards
Petar.