RMI & Proxy Design Pattern
Q12. Explain the RMI architecture.
Ans: Java Remote Method Invocation (RMI) provides a way for a Java program on one machine to communicate with objects residing in different JVMs (i.e. different processes or address spaces). The important parts of the RMI architecture are the stub class, object serialization and the skeleton class. RMI uses a layered architecture where each of the layers can be enhanced without affecting the other layers. The layers can be summarized as follows:
1. Application Layer: The client and server program.
2. Stub & Skeleton Layer: Intercepts method calls made by the client. It redirects these calls to a remote RMI service.
3. Remote Reference Layer: Sets up connections to remote address spaces, manages connections, and understands how to interpret and manage references made from clients to the remote service objects.
4. Transport layer: Based on TCP/IP connections between machines in a network. It provides basic connectivity, as well as some firewall penetration strategies.
Design pattern: RMI stub classes provide a reference to a skeleton object located in a different address space on the same or different machine. This is a typical example of a proxy design pattern (i.e. remote proxy), which makes an object executing in another JVM appear like a local object. In JDK 5.0 and later, the RMI facility uses dynamic proxies instead of generated stubs, which makes RMI easier to use.
RMI runtime steps (as shown in the diagram above) involved are:
Step 1: Start RMI registry and then RMI server. Bind the remote objects to the RMI registry.
Step 2: The client process will look up the remote object from the RMI registry.
Step 3: The lookup will return the stub to the client process from the server process.
Step 4: The client process will invoke method calls on the stub. The stub calls the skeleton on the server process through the RMI reference manager.
Step 5: The skeleton will execute the actual method call on the remote object and return the result or an exception to the client process via the RMI reference manager and the stub.
Q13. What are stubs and skeletons?
Ans. STUB: Stub is a client side proxy; the purpose of which is marshalling the data. It acts as a proxy for 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.
Marshalling: It is the process of converting the java code (source code) into network oriented stream (bit-blobs stream)
SKELETON: Skeleton is a server side proxy; the purpose of which is converting the network oriented stream into java program (human readable format) i.e unmarshalling.
RMI Process
All network-related code is put in the stub and skeleton so that the client and server won't have to deal with the network and sockets in their code. The stub implements the same Remote interface (as in an interface that extends java.rmi.Remote) that the server implements. So, the client can call the same methods in the stub that it wants to call in this server. But, the functions in the stub are filled with network-related code, not the actual implementation of the required function. For example, if a server implements an add(int,int) function, the stub also will have an add(int,int) function, but it won't contain the actual implementation of the addition function; instead, it will contain the code to connect to the remote skeleton, to send details about the function to be invoked, to send the parameters, and to get the results back.
So, this will be the situation:
Client <--->stub <---> [NETWORK] <---> skeleton <---> Server
The client speaks to the stub, the stub speaks to the skeleton through the network, the skeleton speaks to the server, the server executes the required function, and the results are also passed in the same way. So, you will have four separate programs corresponding to each of these entities (this means four class files).
Later, in JDK 1.2, the skeletons were incorporated into the server itself so that there are no separate entities as skeletons. In other words, the skeletons's functionality was incorporated into the server itself. So, the scenario became like this:
Client <---> stub <---> [NETWORK] <---> Server_with_skeleton_functionality
Q14. What design pattern does RMI use?
Ans. RMI. Stubs and skeletons work as proxy objects and thus RMI is based on proxy design pattern.
Q15. What is a remote object and what is RMI server?
Ans: A remote object is one whose methods can be invoked from another JVM (i.e. another process). A remote object class MUST implement the Remote interface i.e java.rmi.Remote.
A RMI Server is an application that creates a number of remote objects. An RMI Server is responsible for
1. Creating an instance of the remote object (e.g. CarImpl instance = new CarImpl()).
2. Exporting the remote object.
3. Binding the instance of the remote object to the RMI registry.
Q16. How will you pass parameters in RMI?
Ans:
1. Primitive types are passed by value (e.g. int, char, boolean etc).
2. References to remote objects (i.e. objects which implement the Remote interface) are passed as remote references that allow the client process to invoke methods on the remote objects.
3. Non-remote objects are passed by value using object serialization. These objects should allow them to be serialized by implementing the java.io.Serializable interface
The client process initiates the invocation of the remote method by calling the method on the stub. The stub (client side proxy of the remote object) has a reference to the remote object and forwards the call to the skeleton (server side proxy of the remote object) through the reference manager by marshalling the method arguments. During marshalling, each object is checked to determine whether it implements java.rmi.Remote interface. If it does then the remote reference is used as the marshalled data otherwise the object is serialized into byte streams and sent to the remote process where it is de-serialized into a copy of the local object. The skeleton converts this request from the stub into the appropriate method call on the actual remote object by un-marshalling the method arguments into local stubs on the server (if they are remote reference) or into local copy (if they are sent as serialized objects).
Q17. What are the services provided by the RMI Object?
Ans: In addition to its remote object architecture, RMI provides some basic object services, which can be used in a distributed application. These services are
1. Object naming/registry service: RMI servers can provide services to clients by registering one or more remote objects with its local RMI registry.
2. Object activation service: It provides a way for server (i.e. remote) objects to be started on an as-needed basis. Without the remote activation service, a server object has to be registered with the RMI registry service.
3. Distributed garbage collection: It is an automatic process where an object, which has no further remote references, becomes a candidate for garbage collection.