I have a service object that can be connected to via RMI. Currently I'm doing this:
Server
Registry r = LocateRegistry.createRegistry(1234);
r.bind("server", UnicastRemoteObject.exportObject(remoteServer, 0));
Client
RemoteServer s = LocateRegistry.getRegistry("example.com", 1234).lookup("server");
The registry on the server has only one use, to link to the single server object. I figured I might just as well do this on the server:
UnicastRemoteObject.exportObject(remoteServer, 1234);
But then how would I connect to the server object from the client?
The RMI Registry exists to solve the RMI bootstrap problem, which is simply that you can only get a remote stub via a remote method call, and to perform a remote method call you need a remote stub. The Registry reference provided by LocateRegistry.getRegistry()
solves this problem (and is used internally by Naming.lookup()
if you are using that API). [Note that this stub isn't obtained via a remote method: it is synthethized locally using the host:port you provide. If they aren't correct you won't find out until you use the Registry stub.]
You have several choices to solve the RMI bootstrap problem:
Use the RMI Registry.
Use an LDAP server via JNDI with the LDAP provider.
Use
UnicastRemoteObject,
serialize the stub obtained when you export the object, and use a shared file, or a socket, or sneakernet, to make the stub available to clients.Use RMI Activation; serialize the stub obtained when you registered the activatable, and distribute to all clients in a file, along with the client application. From the point of view of stub distribution this is a lot simpler than (3) because the stub remains constant for the life of the application, whereas in (3) you have to redestribute the stub on every export.
You can see that the Registry is certainly the simplest option. Note that you only have to use it to solve the bootstrap problem Once you have a stub, your own application remote methods can return further objects: you don't need more than one remote object in the Registry. You can consider that as a remote object factory.
Not impossible, but not terribly practical because the registry communicates the stub object of the exported object to the client (see http://www.developer.com/print.php/3455311). If you don't have another mechanism for that, you'll be stuck. Use of a registry in distributed systems has other benefits, so I'd actually recommend keeping it for other reasons (location transparency, etc).
来源:https://stackoverflow.com/questions/4864206/server-via-rmi-without-registry