Search code examples
javarmidistributed-computing

RMI Server vs. RMI Registry


On Oracle's FAQ page about Java RMI, it says:

While the Java Remote Method Invocation (Java RMI) server can theoretically be on any host, it is usually the same host as that on which the registry is running, and on a different port. Even if the server is mistaken about its hostname or IP address (or has a hostname that simply isn't resolvable by clients), it will still export all of its objects using that mistaken hostname, but you will see an exception every time you try to receive one of those objects.

I don't understand the difference between the RMI Server and the RMI Registry. I thought they were the same thing. How can the RMI Registry work if it wasn't a server of some sorts?


Solution

  • What is RMI registry:

    RMI registry is a service where remote objects are registered and registered remote objects are looked up by RMI clients. If you want your object to be remotely accessible (could be many reason like you keep on updating the logic and not feasible to ship to the implementation each time, so allow remote invocation through RMI) then register it in a RMI registry and then a RMI client will look up the remote object (using remote reference of the object) and then can invoke the methods on the remote object.

    Below is definition of registry from Oracle Javadoc

    A registry is a remote object that maps names to remote objects. A server registers its remote objects with the registry so that they can be looked up. When an object wants to invoke a method on a remote object, it must first lookup the remote object using its name. The registry returns to the calling object a reference to the remote object, using which a remote method can be invoked.

    What is RMI server:

    RMI server is that actual server where the JVM is running and the object (remote object) is living. RMI client ultimately wants this object.

    As per your concern, yes this server (RMI server) could be different from the server where RMI registry is running. And you could understand why! I could register object from different servers in same RMI registry and I can have that registry running on a totally different server. Please read more below for more explanation on this.

    How do Java RMI clients contact remote Java RMI servers?

    For an Java RMI client to contact a remote Java RMI server, the client must first hold a reference to the server (this is where RMI registry is coming into picture, to give you reference to the RMI server). The Naming.lookup method call is the most common mechanism by which clients initially obtain references to remote servers.

    Every remote reference contains a server hostname and port number that allow clients to locate the VM that is serving a particular remote object (this is where RMI server is coming into picture). Once a Java RMI client has a remote reference, the client will use the hostname and port provided in the reference to open a socket connection to the remote server.

    Please do read this from same Oracle FAQs.

    You can very well connect with the RMI registry but you may not be able to get the remote object and that's when people report java.net.UnknownHostException, which means that RMI registry is able to give the reference of remote object BUT RMI server which is actually hosting the remote object or running the JVM where object is living, is not found or client is not able to connect.

    So, RMI registry and RMI server are 2 different things.

    An analogy could be that HTTP server is used to provide access to HTTP resources (hyper text documents) which are available on a server. However, typically hypertext docs will be on same physical box as HTTP server but RMI registry can provide access to reference of remote objects which are on different server (RMI server).