Remote Method Invocation (RMI) is the process of activating a method on a remotely running object. RMI offers location transparency in the sense that it gives the feel that a method is executed on a locally running object.
The RMI architecture is based on one important principle: the definition of behavior and the implementation of that behavior are separate concepts. RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs.
The RMI is built on three layers.
a. Stub and Skeleton layer
This layer lies just beneath the view of the developer. This layer intercepts method calls made by the client to the interface reference variable and redirects these calls to a remote RMI Service.
b. Remote Reference Layer.
This layer understands how to interpret and manage references made from clients to the remote service objects. The connection is a one-to-one (unicast) link.
c. Transport layer
This layer is based on TCP/IP connections between machines in a network. It provides basic connectivity, as well as some firewall penetration strategies.
The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. Methods that are to be invoked remotely must be identified in Remote Interface. All Remote methods should throw RemoteException.
The Naming class provides methods for storing and obtaining references to remote objects in the remote object registry.
Binding is a process of associating or registering a name for a remote object that can be used at a later time to look up that remote object. A remote object can be associated with a name using the Namingclass's bind or rebind methods.
bind method(String name) binds the specified name to a remote object while rebind(String name) method rebinds the specified name to a new remote object,any existing binding for the name is replaced.
AlreadyBoundException is thrown by bind(String name) method when aremote object is already registered with the registry with the samename.
Note: rebind method doesn't throw AlreadyBoundException because it replaces the existing binding with same name.
Using list method of Naming Class.
a. Compile the source files.
b. Generate the stubs using rmic.
c. Start the rmiregistry.
d. Start the RMIServer.
e. Run the client program.
A stub for a remote object acts as a client's local representative orproxy for the remote object. The caller invokes a method onthe local stub which is responsible for carrying out the method all on the remote object. When a stub's method is invoked, it does the following:
a. initiates a connection with the remote JVM containing the remote object.
b. marshals (writes and transmits) the parameters to the remote JVM.
c. waits for the result of the method invocation
d. unmarshals (reads) the return value or exception returned
e. returns the value to the caller.
Skeleton was the server side component of stub. But skeleton has been deprecated from JDK1.2 onwards and its not required anymore.
DGC is Distributed Garbage Collection. RMI uses DGC for automatic garbage collection. Since RMI involve remote object references across JVM's, garbage collection is a bit tricky. DGC uses a reference counting algorithm to provide automatic memory management for remote objects.
RMISecurityManager provides a security manager for use by RMI applications that use downloaded code. RMI's class loader will not download any classes from remote locations if no security manager hasbeen set. The following statment sets it:
System.setSecurityManager(new RMISecurityManager());
When an Object is "Passed by Value", it means a copy of the object is passed. So even if changes are made to that Object/datatype, it doesn't affect the original value.
When an Object is "Passed by Refernce", it means the object is not passed but a reference of the object is passed. so any changes made in the external method gets reflected in all places.
A so-called marker interface is a Java interface which doesn't actually define any fields or methods. It is just used to "mark" Java classes which support a certain capability -- the class marks itself as implementing the interface. For example, the java.rmi.Remote and java.lang.Cloneable interface.
When objects in memory are to be passed across a network to another host or persisted to storage, their in-memory representation must be converted to a suitable out-of-memory format. This process is called marshalling, and converting back to an in memory representation is called demarshalling.
Serialization is a way of "flattening", "pickling" or "freeze-drying" objects so that they can be stored on disk, and later read back and reconstituted, with all the links between objects intact. Deserialisation is the reverse process of converting a object from flattened state to live object.
Yes. Those methods behave as normal java methods operating within the JVM.
JRMP(java remote method protocol)
The UnicastRemoteObject class provides support for point-to-point active object references using TCP streams. Objects that require remote behavior should extend UnicastRemoteObject.
Exports the remote object to make it available to receive incoming calls, using the particular supplied port. If port not specified receives calls from any anonymous port.
Java RMI-IIOP provides a mechanism to narrow the the Object you have received from from your lookup, to the appropriate type. This is done through the javax.rmi.PortableRemoteObject class and, more specifically, using the narrow() method.
a. MalFormedURLException
b. NotBoundException
c. RemoteException