Search This Blog

Remote Method Invocation (RMI) in Java - Example Program to Calculate Power using RMI

Remote Proceure Call (RPC) is a protocol by which a computer can call a function in another computer connected through network. The caller passes arguments through network and the callee executes the function and returns the output to the caller. In object oriented languages we use RMI (Remote Method Invocation) instead of RPC. Let us see a sample java program which uses RMI. .

Here, we call the computer that hosts the method, a server. And those computers which call this method are called clients. More precisely, RMI server and RMI clients. RMI clients call remote methods in RMI servers.

Our example program is answer to the following question:
Write a java program that uses RMI in which RMI clients can calculate powers of numbers by invoking a remote method in RMI server.



First of all, it is necessary that server and clients should be in agreement over the prototype of remote methods. That is, both server and the clients should know well what are the remote methods, what are their parameters and what these methods return. For this purpose, we use a interface which should be available on RMI server and RMI clients. Let us call it RemoteObjectInterface. The RemoteObjectInterface should extend the interface Remote which is defined in the java.rmi package. This is to indicate that this is an interface which define the characteristics of the remote object and its methods. All methods in this interface should throw a RemoteException (in java.rmi package).

See the following example for a remote interface:

MyRemoteObjectInterface.java


import java.rmi.*;
public interface MyRemoteObjectInterface extends Remote
{
public long getPower(long number,long power) throws RemoteException;
}


Compile the interface (here MyRemoteObjectInterface.java). A copy of this should be on both server and client computers. Now let us see the server side code.

Since it is only the remote server which execute the method and produce result, only the server need to have the method code. The server should have definition for the remote method. Actually everything is based on objects in java. Therefore we define a remote object which can have some data members and some member methods (including remote and local methods). Now, we define the class for remote object. Here, in our example, we define a MyRemoteObject class (MyRemoteObject.java). You may use a different name like ServerSide, Server etc. The following one is the example class at server side. First, have a look at the code. Then i'll explain.

Server Side Code

MyRemoteObject.java

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.net.*;
public class MyRemoteObject extends UnicastRemoteObject implements MyRemoteObjectInterface
{
public MyRemoteObject() throws RemoteException{}

public long getPower(long number,long power) throws RemoteException
{
return (long)Math.pow(number,power);
}

public static void main(String args[])
{
try{
System.out.println("ip="+InetAddress.getLocalHost().getHostAddress());
MyRemoteObjectInterface RemService=new MyRemoteObject();
Naming.rebind("RemotePower",RemService);
}catch(Exception ex){}

}

}


The server side class which implements the remote method should implement the remote object interface we have created. And it should also extend UnicastRemoteObject class in java.rmi.server package to act as an rmi server. The class should have an empty default constructor which throws a RemoteException. It should also implement the remote method (here getPower() method) according to the interface. In main method, It should create an object of its own, but assign it into a variable of the remote object interface type. The line MyRemoteObjectInterface RemService=new MyRemoteObject(); does the same. The main method should also rebind (or bind) this object with a name. It is to declare in the network using rmi registry that i have a remote method getPower() with the specified name. The line Naming.rebind("RemotePower",RemService); does this. We named this remote service RemotePower. We can create objects of this remote class at client side remotely and call its remote methods using this name. Compile this class at server.

Client Side code


Now we are going to see the client code. First, see the code. There is not much to explain. It is simple.

Client.java

import java.rmi.*;
import java.io.*;
public class Client{

public static void main(String args[]) throws RemoteException
{
new Client(args[0]);
}
public Client(String host)
{

try{
MyRemoteObjectInterface remobj=(MyRemoteObjectInterface)Naming.lookup("rmi://"+host+"/RemotePower");
System.out.println("Enter X and Y in x^y:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long x=Long.parseLong(br.readLine());
long y=Long.parseLong(br.readLine());
System.out.println(Long.toString(x)+"^"+Long.toString(y)+" = ");
System.out.print(Long.toString(remobj.getPower(x,y)));
}catch(Exception ex){System.out.println(ex);}

}

}


The client should create an object of the remote class and assign it to a variable of remote object interface. The line MyRemoteObjectInterface remobj=(MyRemoteObjectInterface)Naming.lookup("rmi://"+host+"/RemotePower"); doe this. In this example java rmi program, we read the server address (ip) as command line parameter. Compile this class at the client(s).

How to run:


  1. Copy MyRemoteObjectInterface  to server and all clients and compile it in all those machines.
  2. Write MyRemoteObject.java program and compile it at the server.
  3. Write Client.java program, copy it to all clients and compile it.
  4. Start rmiregistry in the server system using the following commands:
    start rmiregistry or javaw rmiregistry  in windows command prompt
    rmiregistry &  in Linux or solaris
    (NB: Do not close the rmiregistry, keep it open)
  5. Run the server program using command java MyRemoteObject
  6. Run client programs on clients using command java Client


No comments:

Post a Comment