How to send data over remote method in java?


RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.

RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.rmi.

To write an RMI Java application, you would have to follow the steps given below −

Step1 − Define the Remote Interface

A remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface. Therefore, you need to create an interface that extends the predefined interface, java.rmi.Remote.

Example

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
   void printMsg() throws RemoteException;
}

Step 2 − Develop the Implementation Class (Remote Object)

We need to implement the remote interface created in the earlier step. (We can write an implementation class separately or we can directly make the server program implement this interface.) Therefore, provide implementation to all the abstract methods of the remote interface.

Example

public class ImplExample implements Hello {
   public void printMsg() {
      System.out.println("This is an example RMI program");
   }
}

Step3 − Develop the Server Program

An RMI server program should implement the remote interface or, extend the implementation class. Here, we should create a remote object and bind it to the RMIregistry. Therefore, develop a server program as shown below −

Example

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
   public Server() {}
   public static void main(String args[]) {
      try {
         ImplExample obj = new ImplExample();
         Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
         Registry registry = LocateRegistry.getRegistry();
         registry.bind("Hello", stub);
         System.err.println("Server ready");
      } catch (Exception e) {
         System.err.println("Server exception: " + e.toString());
         e.printStackTrace();
      }
   }
}

Step4 − Develop the Client Program

Write a client program in it, fetch the remote object and invoke the required method using this object.

Example

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
   private Client() {}
   public static void main(String[] args) {
      try {
         Registry registry = LocateRegistry.getRegistry(null);
         Hello stub = (Hello) registry.lookup("Hello");
         stub.printMsg();
      } catch (Exception e) {
         System.err.println("Client exception: " + e.toString());
         e.printStackTrace();
      }
   }
}

Step5 − Compile the Application

To compile the application −

  • Compile the Remote interface.

  • Compile the implementation class.

  • Compile the server program.

  • Compile the client program.

Step6 − Execute the Application

  • Start the rmi registry using the following command.

Start rmiregistry

            This will start an rmi registry on a separate window.

  • Run the server class file as shown below −

  • Run the client class file as shown below −

  • Verification − As soon you start the client, you would see the following output in the server.

Updated on: 14-Oct-2019

459 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements