Skip to content

Java Programming

RMI (Remote Method Invocation) in Java

Distributed applications are a vital necessity of our times. For creating distributed applications in Java we use RMI (Remote Method Invocation) and EJB (Enterprise Java Beans). We can access files by calling the methods from any machine (known as Remote Machine) on Internet.

Java was designed with the distributed environment and it can be transmitted over Internet. Java also supports various level network connectivity through its classes in java.net.package (e.g. we have URL class in java.net.package that allows a java application to access and open remote objects on the internet). Java is known as a distributed language because Java program can compiled to one machine, can be easily transferred and executed to another machine because it provides us with the facility of Byte code. This is why Java is specially designed for internet users which uses the remote computers for executing Java programs on local machine after transferring the Java programs from remote computers.

RMI allows Java applications to call object methods that are located remotely by sharing resources and processing load across systems. It allows any Java object type to be used, even if the client or server has never encountered it before. RMI helps client and server to dynamically load new Java objects as required. RMI provides a process for object function calls between Java Virtual Machines (JVM’s).  As we all know that JVM can be located on separate computers and one JVM can invoke methods of an object stored in another JVM.

As for example web applications where a developer writes a service that performs some useful function or action and developer also regularly updates this service i.e. adding new features or improving the old features. Another developer wishes to use the service provided then he can access it using RMI. However it is difficult for the developer (creating or publishing the services) to supply to developer, which wants to consume the service with an update every time. RMI provide a easy solution for this problem as RMI can dynamically load new classes and objects. Developer consuming the services can let RMI handle the updates automatically from the developer publishing the services. As this developer just have to places its new classes in a web directory, where RMI would be able to read or fetch the updates as and when required.

RMI in Java

RMI in Java

From the above diagram it is clear that client must contact RMI registry first and request the name of the service as the developer consuming the services won’t know the actual location of the service, but he knows how he can contact other developer’s registry. This will allow him to point in the direction of the service he wants to call.

As this service will change regularly, so developer consuming the service doesn’t have a copy of the class, but the client will automatically fetched the new classes from a webserver where these two developers are sharing their classes. The new class will be loaded into the memory and will be available for use by the client. No extra code is needed to fetch the class.

Java RMI is very useful for invoking methods of remote objects as it allows one Java Virtual Machine to invoke methods of another and to share any Java object type.

Steps to write the RMI Program:

  • Create the remote Interface.
  • Provide the implementation of the remote interface.
  • Compile the implementation class and create the stub and skeleton objects using the rmic tool.
  • Start the registry service by rmi registry tool
  • Create and start the remote application
  • Create and start the client application

1. Creating the remote interface

import java.rmi.*;

public interface AddingThreeNumbers extends Remote{

public int addThreeNumbers(int firstNumber,int secondNumber, int thirdNumber)throws RemoteException;

}

2. Provide the implementation of the remote interface

import java.rmi.*;

import java.rmi.server.*;

public class AddingNumbersRemote extends UnicastRemoteObject implements AddingThreeNumbers{

AddingNumbersRemote()throws RemoteException{

super();

}

public int addThreeNumbers (int firstNumber,int secondNumber, int thirdNumber){

return firstNumber+secondNumber+thirdNumber;

}

}

3. Create the stub and sekeleton objects using the rmi tool

rmic AdderRemote

4. Start the registry service by rmi registry tool

rmiregistry 5000

5. Create and run the server application

RMI services need to be hosted in a server process. The Naming class provides methods to get and store the remote object. The Naming class provides 5 methods.

  1. public static java.rmi.Remote lookup(java.lang.String) throws java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException; it returns the reference of the remote object.
  2. public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException; it binds the remote object with the given name.
  3. public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException; it destroys the remote object which is bound with the given name.
  4. public static void rebind(java.lang.String, java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException; it binds the remote object to the new name.
  5. public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException; it returns an array of the names of the remote objects bound in the registry.

In this example, we are binding the remote object by the name remoteobjectexample.

import java.rmi.*;

  1. import java.rmi.registry.*;

public class RMIServerForTesting{

public static void main(String args[]){

try{

AddingThreeNumbers  stubForAddingThreeNumbers   = new AddingNumbersRemote();

Naming.rebind(“rmi://localhost:5000/remoteobjectexample”, stubForAddingThreeNumbers);

}catch(Exception e){System.out.println(e);}

}

}

6. Create and run the client application

import java.rmi.*;

public class RMIClientForTesting{

public static void main(String args[]){

try {

AddingThreeNumbers  stubAddThreeNumber=( AddingThreeNumbers)Naming.lookup(“rmi://localhost:5000/remoteobjectexample “);

System.out.println(stubAddThreeNumber.add(39, 48, 58));

}catch(Exception e){}

}

}

Hope this tutorial helps in understanding and writing your first RMI program. Please share your views.

Be Sociable, Share!
    The following two tabs change content below.

    Basant Singh

    Basant is a talented software developer who is very engaged in web & cloud platforms on a daily basis. He is a contributor to codingthis.com reporting on various news and troubleshooting on various platforms. Basant is very strong in SQL and a great asset to the CodingThis community.

    Latest posts by Basant Singh (see all)