Remote Method Interface:
Client calls remote method on the stub. So the Client is like making a local call.
The stub contacts the server, transfers information about the call and wait for a return for the server. Then stub unpacks the information and return vales to the client objects. A stub is to do the low-level communication.
The server has a skeleton. The skeleton receives the request and unpacks the information for the call and then invokes the real method on the service object. Then the skeleton gets the return values and packs it up and ships it back to the client.
Note: Skeleton is not used since Java 1.2. Stub can be generated automatically since Java 5. You don't need to call rmic.
Simple example of a client calling remote method in the server:
Serve Side:
1. Make a Server Remote Interface that is exposed to the Client. Every method throwsRemoteException. Every argument and return values are primitives or Serializable.
2. Implement the Server extending UnicastRemoteObject and implements the Server Remote Interface. The constructor should declares a RemoteException. Register the service with RMI registry using Naming.rebind("ServiceName", serviceObject)
3. Generate the stubs and skeletons using rmic ServerImpl, generating ServerImpl_Stub.class and ServerImpl_Skel.class.
4. Start the RMI registery using rmiregistry.
5. Start the Server.
Client Side:
1. Lookup on the RMI register using
Naming.lookup("rmi://127.0.0.1/ServiceName");
2. RMI registry return the stub object. You must have stub class so that JVM can deserialize it. You can copy the ServerImpl_Stub.class from the Server.
Server server = (Server)Naming.lookup("rmi://127.0.0.1/ServiceObject");
3. Invoke a method on the stub.
server.doThing();