Does a stub possess a server-side role also? Practically all the literature on RMI, including the RMI Specification document, talks only about the role of a stub on the client side. For instance, once I generate a stub class and copy it over to the client side, can it now be safely deleted from the server side?
A stub for a remote object acts as a client's local representative or proxy for the remote object. The caller invokes a method on the local stub which is responsible for carrying out the method call on the remote object. In RMI, a stub for a remote object implements the same set of remote interfaces that a remote object implements. When a stub's method is invoked, it does the following: (i) initiates a connection with the remote VM containing the remote object; (ii) marshals (writes and transmits) the parameters to the remote VM; (iii) waits for the result of the method invocation; (iv) unmarshals (reads) the return value or exception returned; (v) returns the value to the caller.The Specification document says practically nothing about the role of a stub class on the server side. The fact is that the stub of an implementation class has a server side role also. This is immediately apparent if after copying over the stub to the client side you try to delete it from the server side. The Java runtime would then throw an exception with the error message: "Stub class not found."
On the server side, the stub class is needed to construct stub objects corresponding to the remote objects. It is the stub objects that get transmitted over to the client side over the TCP link. When a client invokes a method on a remote server object, the method is actually invoked on the locally available version of the stub object corresponding to the remote server object.