Professional Documents
Culture Documents
Rmi
Rmi
Rmi
Chapter 23
We have seen in the previous chapters how Java methods were called on
objects residing in the same machine. Java also provides a mechanism to call a
method of an object residing at a remote machine. This mechanism is known as
Remote Method Invocation(RMI). The remote machine executes the method and
returns a result. The object that calls the method acts like a client and the
object in the remote machine that executes the method and returns the result
acts like a server. Thus, using Java an application distributed across several
machines can be developed.
(1) Server
RMI Registry
^
Registering (remote object)
^
^
)
(3
Finding the location st
que
of the server (2) Re
)
(4
ly
rep
Client ^
Program 23.1
/* This is an interface that extends the class
Remote. Any class that implements Remote can
create a remote object. This interface is the first
step to create a RMI application.
somasundaramk@yahoo.com
*/
import java.rmi.*;
public interface Ifprime extends Remote{
String Checkprime(int n)throws RemoteException;
}
Program 23.2
/* This is a server program that acts as
a remote object and the Checkprime method in it
will check if the argument passed to it is a
prime number or not and return an appropriate message.
somasundaramk@yahoo.com
*/
import java.rmi.*;
714 Programming in JAVA2
import java.rmi.server.*;
public class Srvprime extends UnicastRemoteObject implements
Ifprime {
int i;
boolean test = true;
public Srvprime() throws RemoteException{;}
public String Checkprime(int gn){
for(i=2;i<gn ;i++)
if( gn%i ==0 )
return gn + " is not a prime number";
return gn+" is a prime number";
}
public static void main(String args[]){
try{
Srvprime svp = new Srvprime();
Naming.bind("Srvprime",svp);
System.out.println("Srvprime bound");
}catch(Exception e){
System.out.println("Error in Srvprime");
}
System.out.println("Srvprime End");
}
}
In our example the client is given in program 23.3. It first finds the
address of the local host as we are going to keep the remote object also in the
same machine. Then it constructs the URL. Then it calls lookup() method and
gets a reference for the remote object. Then it reads an int type supplied by the
user and calls the Checkprime() method to test whether the given number is
prime or not.
Program 23.3
/* This is a client program of RMI application.
In this a user is asked to input an integer.
Checkprime method of the remote method is called to
check whether the given number is prime or not.
somasundaramk@yahoo.com
*/
import java.rmi.*;
import java.net.*;
import java.io.*;
public class Cliprime {
public static void main(String args[])throws Exception{
Ifprime ifp;
String answer="";
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(ins);
InetAddress ia= InetAddress.getLocalHost();
String ip = ia.toString().substring(ia.toString().indexOf('/')+1);
String url = "rmi://"+ip+"/Srvprime";
System.out.println("Check wether the number is prime or not");
System.out.println("Type an int to test for Prime:");
ifp = (Ifprime)Naming.lookup(url);
String sn= br.readLine();
int n = Integer.parseInt(sn);
System.out.println( ifp.Checkprime(n));
}
}
Now open another DOS window and run the client program.
This is the third active DOS window. The output screen after running the
Cliprime is shown in Fig.23.3(top). The output screen, when the user types the
number 13 is shown in Fig.23.3(bottom).
Remote Method Invocation 717
G
A server class is created by implementing the remote interface and
extending the UnicastRemoteObject class.
A remote object is to be registered to an RMI registry, using bind()
method of Naming class.
An RMI Client identifies the remote object using lookup() method
of Naming class.
After reading this chapter you should have learned the following concepts:
Ü How an RMI functions
Ü How to create a remote interface
Ü How to create an RMI server class
Ü How to create an RMI client
Ü How to compile and run an RMI application in the same computer.
* * * * * *
Remote Method Invocation 719
Exercise–23
23.8 It is required to sort the given double type array in descending order.
The client will pass the given array as parameter to the remote method.
The remote object must sort the array and return the sorted array to
the client. Write an RMI application for this.
* * * * * *