Crash course of Java RMI

Here is a simple crash course of RMI api provided by Java.

  • RMI is basically implementation of Remote Procedural Call
  • It differs from CORBA which is used for communication between two different languages.
  • These days we have a new implementation of RMI which is RMI/IIOP (RMI over Internet Inter-ORB protocol). This implementation internally uses CORBA.
Server Client
Create and interface that extends java.rmi.Remote interface provided by Java api. Create a client which has information about the Remote interface that server has created. But it does not have any clue about the implementation of that interface.
Create a class which implements that interface to act as a remote object and also extends java.rmi.server.UnicastRemoteObject. By extending this makes sure that all the things that need to be done behind the scene to work as a remote object are performed by UnicastRemoteObject. This class will override a constructor which throws java.rmi.RemoteException because super class does the same. Using JNDI it will lookup for the registered objects and will access the required method.
Create a server that binds this remote object with the RMI registry using JNDI.
Create stub and skeleton of remote object using rmic command.
Start the registry using rmi registry command
Run the server object.

Important things that needs to be considered:

(1) Security is one thing that can stop you from accessing Remote object in that case you can play with the Security manager or can create a simple policy file with contents as shown below:

grant codeBase "file:C:/RMI Tutorial/-" {

permission java.security.AllPermission;

};
Then run the Java class using following command
java -Djava.security.manager -Djava.security.policy=policy-file RemoteServer
(2)    If any method of the Remote Interface has input/output parameters as an Object. That object must be Serializable. That’s where serialization comes in to picture!

Share on : Twitter, Facebook or Google+