The RMI architecture is outlined below:
When an instantiated object located on a client, wishes to access the methods of a remote object, it performs the following operations:
- It locates the remote object through a naming service: RMI registry
- Dynamically obtains a virtual image of the remote object (called stub). The stub has the same interface as the remote object.
- The stub converts the remote method invocation into a sequence of bytes (this process is called serialization) and then forwards them to the server instantiating the object as a data stream.
- The skeleton instantiated on the server "de-serialize" the data sent by the stub and call the method locally.
- The skeleton retrieves the data returned by the method (basic type, object or exception) then serialize them.
- The stub deserialized data from the skeleton and transmits them to the object making the remote method invocation.
To create an application with RMI, proceed as follows:
- Define the remote class. It must derive from java.rmi.server.UnicastRemoteObject (using Socket and SocketServer classes, allowing communication via TCP).
- Define the interface for the remote class. It must implement the java.rmi.Remote interface and declare the public methods of the global object. In addition, these methods be able to generate an exception of the following type: java.rmi.RemoteException.
- Create classes for the stub and skeleton using the rmic command.
- Launch the RMI registry and the application server, that is to say, instantiate the remote object.
- Create a client program able to access methods of an object on the server using the following method: Naming.lookup().
- Compile the client application.
- Instantiate the client.
Original document published on CommentcaMarche.net.