February 15, 2019
Hot Topics:

Understanding Java RMI Internals

  • January 6, 2005
  • By Ahamed Aslam.K
  • Send Email »
  • More Articles »

Is RMIRegistry Absolutely Necessary?
Can I Do Away With RMIRegistry and Bind/Rebind/Lookup Methods?

That is how it works. Is RMIRegistry an absolute necessity? From the above discussion, it is evident that an RMIRegistry is not a must. RMIRegistry is used just for the bootstrapping purpose, nothing else. Once the client gets the stub object from the Registry, the Registry is not used anymore. The communication takes place directly between client and server by using sockets. So, if you can provide the bootstrapping in some other way, you can eliminate the use of Registry itself. In other words, if the client can get an object of the stub from the server somehow, that is just enough. That means if you have some facility to transport the stub object created in the server to the client machine, you don't have to use the RMIRegistry or bin/rebind/lookup methods. That sounds great. eh? Also, it should be noted that even if the client has CalcImpl_Stub.class on its machine, it cannot simply create an object of the stub because its constructor takes a RemoteRef reference as a parameter; you can get only from an object of the remote server, which is exactly what we are trying to access! To facilitate the transportation of the stub object from server to client, you can build a transport facility of your own that is based on sockets that serialize the Stub object and sends it to the clients. That will eliminate the need of the Registry at all!

I will give you an example that does not use the RMIRegistry. But, in this example, the client and server run on the same machine. You can use socket programming as I stated earlier if the client and server are on different machines, but the program will make the basic concepts clear.

Source code of CalcImpl.java:

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class CalcImpl extends UnicastRemoteObject implements Calc
   public CalcImpl()throws RemoteException

   public int add(int i,int j)throws RemoteException
      return i+j;

   public static void main(String[] args)throws Exception
      CalcImpl ci=new CalcImpl();
      RemoteRef ref=ci.getRef();
      CalcClient cc=new CalcClient(ref);

Source code of CalcClient.java:

import java.rmi.server.*;
import java.rmi.*;

public class CalcClient
   public CalcClient(RemoteRef ref)throws RemoteException
      CalcImpl_Stub stub=new CalcImpl_Stub(ref);

Both Calc.java and CalcImpl_Stub.java are the same as in the previous example. Now, compile the classes and run CalcImpl and CalcClient on two different consoles.

On console 1:

C:\test2>java CalcImpl

On console 2:

C:\test2>java CalcClient

You will get an output of 7 on your screen.

That is it!!!

Questions Revisited

Question Answer
Who actually creates an object of stubs? The server, Registry, or client? A stub is created by the Naming class at the server. For getting the stub class, it uses the Java Reflection feature (see above).
Which port is the server listening to? The port that the remote server is listening to is usually chosen arbitrarily at runtime and is decided by the JVM or underlying operating system.
Does the server listen to port 1099 (the default port of the RMI Registry)? The server usually does not listen to port 1099. Port 1099 is usually used by a special Remote object called RMIRegistry. The port that is the server's listening port is chosen arbitrarily at runtime (see above).
How will the client know to which port the server is listening? The client does *not* know which port the server is listening to, but it has a reference to an object of the stub that does know which port the server is listening to.
Is a Registry necessary for running the RMI system? The Registry is used for bootstrapping purposes only. If you have some other means for transporting the stub object created at the server machine to the client, you can do away without the Registry (see above).
Can we use RMI without rmiregistry? Same as the answer to the third question.

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date