问题
I have found a safe way to get LocateRegistry
(even if the registry does not already exist).
Registry registry = null;
try {
registry = LocateRegistry.getRegistry(52365);
registry.list();
// This call will throw an exception if the registry does not already exist
}
catch (RemoteException e) {
registry = LocateRegistry.createRegistry(52365);
}
Is it possible to firstly check registry existence and use getRegistry
or createRegistry
in accordance with the result of the check?
回答1:
Is it possible to firstly check registry existence and use getRegistry or createRegistry in accordance with the result of the check?
Certainly. Just try the createRegistry()
first; catch the ExportException
, which means it's already running, and do the getRegistry()
unconditionally.
I want avoid unnecessary calling of getRegistry when it doesn't exist
That's not a valid concern. It is a virtually cost-free call. No network activity.
来源:https://stackoverflow.com/questions/14982760/is-it-possible-to-manually-check-locateregistry-existing