Rmi

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Remote Method Invocation 711

Chapter 23

REMOTE METHOD INVOCATION (RMI)

Remote Method Invocation(RMI) mechanism


enables methods in one machine to call on a Java object
residing in another machine. An RMI system looks like a
client-server architecture and they differ in their
functionality compared to the normal client-server
configuration. The basic concept of RMI and the different
steps involved in creating an application using RMI are
explained in this chapter.

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.

An RMI differs from the conventional client/server in its operation. In the


conventional client/server, the request are sent through an intermediate layer, like
HTTP request, whereas in RMI, one Java object directly communicates with
another Java object in a remote machine through a remote method. In this
chapter we briefly explain how to create an application using RMI.
712 Programming in JAVA2
23.1 Creating an RMI Application
In an RMI system, the server which provides the service, register its
service and location in a register called RMI Registry. A client which needs the
service, first looks at the RMI registry and finds the location of the server which
contains the object that provides the required service. The client then calls the
method on the object residing in the remote machine. The remote object carry
out the required process and returns the result to the client. The client-server
architecture of RMI is shown in Fig.23.1.

(1) Server
RMI Registry

^
Registering (remote object)
^

^
)
(3
Finding the location st
que
of the server (2) Re
)
(4
ly
rep

Client ^

Fig.23.1 RMI Architecture.

The numbers 1 to 4 inside the parenthesis give the sequence of activities


in making a remote method call.
The steps involved to create an application using RMI are given below.
1. Create an interface.
2. Create a server class by implementing this interface.
3. Create a class to register the server with a RMI registry.
4. Create a client class that calls a method defined in the interface and
implemented in the server class.
5. Compile the classes.
6. Run the RMI compiler.
7. Start the RMI registry.
8. Run the Server program
9. Run the Client program

In the following sections each step is explained in detail. To illustrate


each step, we consider a problem. The problem is to check whether the given
number is a prime number or not. A number is called as prime number, if it
can be divided without a remainder by 1 and by that number only. The client
calls the method on the object in the remote server(object). The server checks
whether the number is prime or not and returns a reply to the client. Now we
will see how to create this client/server application.
Remote Method Invocation 713

Step 1 : Create an Interface


To create an RMI application, an interface has to be declared. This
interface contains a method which a client can make use of. This interface has
to extend Remote class defined in java.rmi package. Any class that
implements this interface can be used to create a remote object. Any method
defined in this interface becomes a remote method and it can throw
RemoteException. For our problem the Ifprime.java given in program 23.1 is
the interface.

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;
}

This interface defines a remote method Checkprime(). This method takes


an int as parameter and returns a String type. This Checkprime() method can
throw RemoteException. The next step is to create a server class.

Step 2 : Create a server class


The server class is created by implementing the interface Ifprime. This
class must extend the UnicastRemoteObject class defined in java.rmi.server
package. This inheritance is necessary to make a link with the RMI system.
When a class extends UnicastRemoteObject class, it must define a no
argument constructor that throws RemoteException. This constructor must call
the super class constructor. This process invokes code in the
UnicastRemoteObject that makes RMI linking and remote object initialization.
By this process the remote object starts listening and ready to accept calls from
clients. The program 23.2 shows how the interface is implemented.

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");
}
}

This program implements the Checkprime() method which tests whether


the given number is prime or not and returns a string message.

Step 3 Register the Server


The next step is to create a class to register the RMI server with an RMI
Registry. In our case, the registering is done in the main() method of the
program 23.2. For this, bind() method of the Naming class is called. Only
when a server is registered with the RMI registry, the client can look for the
service and identify the remote object.

Step 4 Create the Client


The next step is to create a client program. This program first locates the
remote object on which it can call the remote method. For this purpose it calls
lookup() method of the Naming class. The lookup() method requires an URL
that specifies the server hostname and the required service(object).

The URL takes the following form:


rmi://<host-name>[<service-port-no>]/<service-name>
where, host-name is the name recognized on the network, and the service-port-
no is the port on which the service is running. The default port number is
1099.
Remote Method Invocation 715

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));
}
}

Step 5 Compile the classes


We will assume that all the classes, Ifprime.java, Srvprime.java and
Cliprime.java are in the same directory, say c:\jdk1.2.4\bin\rmi. Set the classpath
and compile all the java programs using javac compiler.

Step 6 Run the RMI Compiler


Now run the RMI compiler by issuing following command.
C:\jdk1.2.4\bin\rmi>rmic Srvprime
This command will create two files Srvprime_skel.class and
Srvprime_stub.class.
716 Programming in JAVA2
Step 7 Start the RMI Registry
From now on the execution of the RMI application begins. Now start the
RMI registry by issuing the following DOS command.
C:\jdk1.2.4\bin\rmi>start rmiregistry
A new window will appear. Minimize it.

Step 9 Run the Server


Now run the server program Srvprime, by using the java command:
C:\jdk1.2.4\bin\rmi>java Srvprime

The output of running this program is shown in Fig.23.2. This is the


second active DOS Window.

Fig.23.2 Output screen for program 23.2.

Now open another DOS window and run the client program.

Step 10 Run the Client


This is the last step of this RMI application. The client program Cliprime
is run by using the java command:
C:\jdk1.2.4\bin\rmi>java Cliprime

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

Fig.23.3 Output screens for program 23.3.


Top: Initial Output Bottom: Output when the user typed 13

23.2 Running the Client from a different directory


The application can be executed, by running the client in a different
directory of the same computer. For this, copy the Ifprime.class, Cliprime.class
and Srvprime_stub.class to a new directory, say d:\APTITUDE, and run the client
program. The output screen is shown in Fig.23.4.

Fig.23.4. Output screen for program 23.3 when it is run


in another directory of the same machine.
718 Programming in JAVA2

To develop an RMI application, a remote interface is to be created,


by extending Remote class.

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.

In the next Chapter some of the utility classes are explained.

* * * * * *
Remote Method Invocation 719

Exercise–23

I. Fill up the blanks

23.1 The first step in developing an RMI application is to define an ———.


23.2 A remote interface must extend __________ class.
23.3 The classes that implement remote _______ can create remote object.
23.4 A server class has to subclass __________ class to produce a remote
object.
23.5 In order to make known about the availability of a remote object to
the clients it must be _____ to the RMI registry.
23.6 Registering of a remote server is done using ________ method of
________ class.
23.7 The client is able to locate the availability of a server using the
_________ method.

II. Write a program to the following problem

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.

* * * * * *

You might also like