Remote Method Invocation in Java

RMI(Remote Method Invocation) is used to develop distributed Java Applications. Two or more Java objects located remotely can interact with each other, using RMI.
RMI uses Sockets internally for communication between client and server machines.

RMI Server

//Filename: Arith.java
//Step 1: interface need to extend from Remote interface
//The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. //Only those methods specified in a "remote interface", an interface that extends java.rmi.Remote are available //remotely.
import java.rmi.*;
public interface Arith extends Remote{
public int add(int x,int y)throws RemoteException;
public int sub(int x,int y)throws RemoteException;
public int mul(int x,int y)throws RemoteException;
}

//Filename: ArithRemote.java
import java.rmi.*;
import java.rmi.server.*;
//Step 2: extend class from UnicastRemoteObject and implement from above Arith interface
//All the basic functionalities of RMI are actually implemented in UnicastRemoteObject class.
//UnicastRemoteObject class implements java.io.Serializable, hence does marshalling/unmarshalling
//objects
public class ArithRemote extends UnicastRemoteObject implements Arith{
ArithRemote()throws RemoteException{
}
public int add(int x,int y){return x+y;}

public int sub(int i, int j)
{
    return i-j;
}

public int mul(int x,int y)
{
    return x*y;
}
}


//Filename:MyServer.java
//
import java.rmi.*;
import java.rmi.registry.*;
public class MyServer{
public static void main(String args[]){
try{
Arith stub=new ArithRemote();

//below statement starts registry, to help clients to lookup
//5000 is port on which remote object is available for interaction
LocateRegistry.createRegistry(5000);

//register remote object(i..e stub) with a name
//client will access this remote object using this name
Naming.rebind("rmi://localhost:5000/test1",stub);


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

RMI Client

import java.rmi.*;
import java.net.MalformedURLException;

public class MyClient{
public static void main(String args[]){
try{
Arith stub=(Arith)Naming.lookup("rmi://localhost:5000/test1");
//returns Remote object


System.out.println("Added:"+stub.add(34,4));
System.out.println("Sub:"+stub.sub(34,4));
System.out.println("Mul:"+stub.mul(34,4));
}catch(RemoteException e)
{
    System.out.println("RemoteException"+e.getMessage());
}
catch(NotBoundException nbe)
{//NotBoundException is thrown, if test1 name is not binded on server
    System.out.println("NotBoundException"+nbe.getMessage());
}
catch(MalformedURLException mue)
{//MalformedURLException is thrown, if URL we have provided is not in proper format
    System.out.println("MalformedURLException"+mue.getMessage());
}
}
}

Limitations of RMI:
RMI can be used for interaction between Java objects only. RMI is not Language independent.

Web Services(RESTful or SOAP) can be used for interaction between components implemented in different Programming Languages(Eg. Java, PHP, .NET,etc…)

You may also like to read:
What is Serialization in Java?
purpose of transient keyword

3 thoughts on “Remote Method Invocation in Java”

Leave a Reply