rmi registry: best way to assure there's one running?
I'm missing something about the difference between
- starting rmiregistry at the command prompt (separately from whatever Java process is running a server that implements an RMI interface)
- having the ser开发者_高级运维ver call
LocateRegistry.getRegistry()
- having the server call
LocateRegistry.createRegistry(Registry.REGISTRY_PORT)
I just want my server to register its exported objects with the registry, creating one if there isn't one running already. What's the best way to do this?
Old thread but...
man rmiregistry
says:
The methods of the java.rmi.registry.LocateRegistry class are used to get a registry operating on the local host or local host and port.
otherwise you have:
The URL-based methods of the java.rmi.Naming class operate on a registry and can be used to look up a remote object on any host and on the local host
So I guess that's the important difference. Other thing is SecurityManager and policies.
This is how I used to do it, not sure if it is the right way though :/. I also had to mess around with policy files, so if this gives you trouble as well (the security manager part) you must create a policy file and use it.
try
{
try
{
java.rmi.registry.LocateRegistry.createRegistry(1099);
}
catch (java.rmi.server.ExportException e) { /* */ }
System.setSecurityManager(new java.rmi.RMISecurityManager());
Registry registry = LocateRegistry.getRegistry("127.0.0.1",1099);
registry.rebind(...);
}
catch (Exception e) { /* */ }
}
精彩评论