RMI

Java technologies allow building the distributed applications.  Application that has been distributed on different machines in the form of executable pieces is called distributed. All the executable pieces work as one application that is transparent for external user. It means external user does not fell that application on which he/she is working is not on the system on which he/she is working.

Introduction

Java provides a feature that enables developer to develop the distributed application in the form of objects those are distributed on the different machines and this is accomplished by the RMI. RMI stands for Remote Method Invocation. RMI allows invoking the method of an object that is located on the remote JVM in the current object of program. That RMI provides the facility of object-to object communication.

Object may be located on the different machine or in the single machine but the JVM in which they are executing will deferent. There is remote interface that contains declarations of the methods of remote object and to call these methods client object uses this interface to declare the remote interface type reference variable to hold the object reference of the remote object.

RMI application is the combination of two different applications. One application is known as server and second is the client application.  Both applications run on the deferent Virtual machines.

Scenario of working is very simple.

       A server program runs on its machine and creates some remote objects. 
       Server makes the references of these objects to access remotely.
       After this, server waits for client to invoke methods on these objects.
       Client program obtains a reference of a remote object reside of the server.
       And then invokes the method of that object.

Remote object is the object that implements the Remote interface directly or indirectly. The object that implements the remote interface will have following characteristics:

A remote interface extends the interface java.rmi.Remote
Each method of the interface throws java.rmi.RemoteException ,.


 When this object is used by the client, original copy of this object is not passed for client java virtual machine. RMI passes a remote stub for the remote object. The stub acts as the local representative or a proxy for the remote object and client use it for the remote reference. The client invokes the method on the stub object, which is responsible to deliver the task for remote object.

            A stub for the remote object has the same behavior that is it implements same interfaces that the remote object implements.  Only those method defined by the remote interface are available to be called from the client Java virtual Machine.

Logical Architecture


Here stub and Skelton are two objects that work for intercepting the calls made by the client application object and the result returned by the remote objects.  It transfers the data to the remote reference by marshaling and receives data from remote reference by demarshaling. Marshaling convert the data or object being transfer into byte stream and demarshaling refers to converting byte stream into data or into object.

Remote reference layer is used to manage the reference to remote object. It connects client to remote object use the reference containing by it. Client side the remote reference is received by the lookup method and it is hold by the reference variable type of remote interface.

Locating the remote object

Java provides the RMI naming service that is used to store the entry of the references of remote objects having the name through which they will be identified across the different Java virtual machines. It contains the hash table having the key value for each object. Client use particular key value to get the reference for specified object. RMI has the implementation of the interface java.rmi.registry.Registry that is the naming class. The rmi registry runs on the machine and accepts queries from the client on the port by default 1099.

There is a method of class Naming i.e. lookup. Lookup method is used to locate the object reference of the specified name. It accepts an argument having the full information of the remote object host and the remote object. It has contains
The host name of the remote object, Port address of the remote application and Name of the object entry into registry as shown below.
 
//host:port/name





Client invokes the method lookup that takes the complete name of the remote object including the host name and port address on which located it. Then rmiregistry is used to find the object reference.

If name found into the rmi registry, the reference value associated by that name is returned. Client store this reference into the reference variable and later use it to invoke the method on the remote object.

To set the reference into the rmiregistry, bind or rebind methods of class Naming are used to bind the object reference to particular name.
Example 1
Example 2

No comments:

Popular Posts