Introduction

  • The RMI  stands for Remote Method Invocation.
  • It is provided in the package java.rmi.

Definition

  • The RMI is an API that provides a mechanism to create distributed application in java.
  • Java RMI is a mechanism that allows one Java Virtual Machine (JVM) running object to invoke methods on an object running on another JVM.
  • RMI provides the mechanism by which the server and the client communicate and pass information back and forth. Such an application is sometimes referred to as a distributed object application.
  • Java provides RMI,  which is a mechanism that allows one to invoke a method on an object that exists in another address space. The other address space could be on the same machine or a different one.

Features

  • The RMI allows an object to invoke methods on an object running in another JVM.
  • Stub and Skeleton are two objects that are used for communication between client and server side.
  • The RMI provides remote communication between the applications using two objects stub and skeleton.
  • The RMI mechanism is basically an object-oriented RPC mechanism.
  • A Remote class is one whose instances can be used remotely.An object of such a class can be referenced in two different ways : –
    • Within the address space where the object was constructed, the object is an ordinary object, which can be used like any other object.
    • Within other address spaces, the object can be referenced using an object handle
      While there are limitations on how one can use an object handle compared to an
      object, for the most part one can use object handles in the same way as an ordinary object.
    • A Remote class has two parts : – the class itself and the interface.
    • The Remote class itself has the following properties : –
      • It must implement a Remote interface.
      • It should extend the java.rmi.server.UnicastRemoteObject class.
      • Objects of such a class exist in the address space of the server and can be invoked remotely.
      • While there are other ways to define a Remote class, this is the simplest way to ensure that objects of a class can be used as remote objects.
      • It can have methods that are not in its Remote interface. These can only be invoked locally.
      • It is not necessary for both the Client and the Server to have access to the
        definition of the Remote class.
      • The Server requires the definition of both the Remote class and the Remote interface, but the client only uses the Remote interface.
    • The Remote interface must have the following properties : –
      • Interface must be public.
      • Interface must extend the java.rmi.Remote interface.
      • Every method in the interface must declare that it throws  java.rmi. RemoteException. It may be other exceptions also ought to be thrown.
    • All of the Remote interfaces and classes should be compiled using javac. Once this
      has been completed, the stubs and skeletons for the Remote interfaces should be
      compiled by using the rmic stub compiler. The stub and skeleton of the example
      Remote interface are compiled with the command : –
      rmic <filename.class>
  • A Remote object is an instance of a Remote class.
  • The most general feature set associated with RMI is found in the java.rmi.Remote interface.Abstract class java.rmi.server.RemoteObject supports the needed modifications to the Java object model to cope with the indirect references.

Advantage

  • RMI Handles threads and Sockets for underlying communication.
  • Server-side implementation can be changed without the knowledge of the client side.

Disadvantage

  • One of the most common problems with RMI is a failure due to security constraints.
  • Overhead during object serialization.
  • Overhead of marshaling and unmarshaling process.

RMI Architecture

  • RMI applications mainly comprised of two separate programs:- a server and a client.
    • A typical server application creates some remote objects, makes references to them
      accessible, and waits for clients to invoke methods on these remote objects.
    • A typical client application gets a remote reference to one or more remote objects in
      the server and then invokes methods on them.

  • It consists of three layers –  
    • Stub/Skeleton layer – This layer contains two objects stub and skeleton that are used for communication with the remote object (A remote object is an object whose method can be invoked from another JVM).
      • The stub object acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks : –

        1. It initiates a connection with remote Virtual Machine.
        2. It writes and transmits (marshals) the parameters to the remote Virtual Machine.
        3. It waits for the result.
        4. It reads the return value or exception.
        5. It finally, returns the value to the caller.
      • The skeleton object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks :-

        1. It reads the parameter for the remote method.
        2. It invokes the method on the actual remote object.
        3. It writes and transmits (marshals) the result to the caller.
    • Remote reference layer – invocation to single or replicated object
    • Transport layer – connection set up and management, also remote object tracking.

RMI Object Hierarchy

  • Remote Server is a base class, which encapsulates transport semantics for
    RemoteObjects.
  • Currently RMI ships with a UnicastRemoteObject(single object).
  • A server in RMI is a named service which is registered with the RMI registry, and
    listens for remote requests. For security reasons, an application can bind or unbind
    only in the registry running on the same host.

RMI Security

  • A Java program may specify a security manager that determines its security policy. A program will not have any security manager unless one is specified.
  • We can set the security policy by constructing a SecurityManager object and calling the setSecurityManager method of the System class. Certain operations require that there be a security manager.
  • For example, RMI will download a Serializable class from another machine only if there is a security manager and the security manager permits the downloading of the class from that machine. The RMI SecurityManager class defines an example of a security manager that normally permits such download. However, many Java installations have instituted security policies that are more restrictive than the default. There are good reasons for instituting such policies, and you should not override them carelessly.

Creation of Distributed Applications using RMI – 

  • There are three processes that participate in developing applications based on remote
    method invocation – 
    1. The Client is the process that is invoking a method on a remote object.
    2. The Server is the process that owns the remote object(The remote object is an
    ordinary object in the address space of the server process.)
    3. The Object Registry is a name server that relates objects with names. Objects are registered with the Object Registry. Once an object has been registered, one can use the Object Registry to obtain access to a remote object using the name of the object.
  • To develop a distributed application using RMI, the following basic steps can be taken :-
    • Design and implement the components of the distributed application.
    • Compile Sources and Generate Stubs – 
      • This is a two-step process. In the first step, we use the javac compiler to compile the source files, which contain the implementation of the remote interfaces and implementations, of the server classes and the client classes. In the second step we use the rmic compiler to create stubs for the remote objects. RMI uses a remote object’s stub class as a proxy in clients so that clients can communicate with a particular remote object.
    • Make classes network accessible –
      • In this step we have to make everything i.e. the class files associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients, accessible via a Web server.
    • Start the application –
      • Starting the application includes running the RMI remote object registry, the server, and the client.

Use of RMI

  • RMI is a pure java solution to Remote Procedure Calls (RPC) and is used to create distributed application in java.
  • We’re still using RMI in one of our business applications.

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.