3

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?

Community
  • 1
  • 1
Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70
  • No. If there is an error in the function you will need to catch it and react accordingly. – mercutio Feb 20 '13 at 15:16
  • @mercutio I don't try to fully avoid exceptions. But I want avoid unnecessary calling of `getRegistry` when it doesn't exist. I've update the question. – Nelson Tatius Feb 20 '13 at 15:26

1 Answers1

2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483