Jason S
Jason S

Reputation: 189686

rmi registry: best way to assure there's one running?

I'm missing something about the difference between

  1. starting rmiregistry at the command prompt (separately from whatever Java process is running a server that implements an RMI interface)
  2. having the server call LocateRegistry.getRegistry()
  3. 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?

Upvotes: 1

Views: 1360

Answers (2)

sZpak
sZpak

Reputation: 247

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.

Upvotes: 2

npinti
npinti

Reputation: 52185

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) {   /*  */  }
    }

Upvotes: 1

Related Questions