Here are some bullet points to understand the working of RMI registry in Java:-
RMIRegistry is run on the server machine.
RMIRegistry is a remote object itself binding to a well known port 1099.
When the server is run, in the UnicastRemoteObject’s constructor it exports itself on an anonymous port on the server machine.( This port is unknown to the client).
When Naming.rebind() is called, a reference to the Implementation class is passed as the second parameter.
Naming.rebind(“rmi://localhost:1099/refName”,inst);
The naming class uses the getClass() method to get the name of the server class and adds _Stub to it to get the name of the stub class.
It then loads the stub class into the JVM.
A RemoteRef object is obtained from the server implementation class instance (inst) passes to it. RemoteRef ref = inst.getRef();
This RemoteRef object encapsulated all the details about the server (hostname, port etc)
A stub instance is created by passing the RemoteRef object to it.
ServerImpl_Stub stub = new ServerImpl_Stub(ref);
This instance is passed to the RMIRegistry for binding along with the public name (refName).
When the client executes Naming.lookup() it passes the public name to the RMIRegistry. The registry then returns the stub object stored against that name to the client.
Filed under: JAVA RMI Registry internal working