RMI-Remote Method Invocation.
Remote Method Invocation allows a java object that executes on one machine to invoke a method of java object that executes on another machine.This is an important feature,because it allows you to build distributed applications.
Example:
CLIENT SERVER APPLICATION BUILDING -USING RMI.
The server receives a request from a client ,process it and returns a result .
In this example the request specifies two numbers the server add these together and returns the SUM.
RMI-uses four java files
>interface.
>interface implementation.
>server.
>client.
Programmer must specify the above four steps while developing the RMI application.
1.)AddServerIntf.java
2.)AddServerImpl.java
3.)AddServer.java
4.)AddClient.java
AddServerIntf.java
Defines the Remoter Inerface that is provided by the server,here (in this program)it contains one mehtod that accepts two double arguments and return their sum.
* All Remote interfaces must extends fro "Remote"interface
* "Remote" interface -part of java.rmi.Remote //package
* it defines no members(Remote).
import java.rmi.Remote;
import java.rmi.*;
public interface AddServerIntf extends Remote
{
double add(double d1,double d2) throws RemoteException;
}
AddServerImpl.java
It implements the remote interface.The implementation of the add() method is straight forward.All remote object must extend 'UnicastRemoteObject',which provides functionality that is needed to make objects available from remote machines.
import java.rmi.*;
import java.rmi.server.*;
public calss AddServerImpl extends UnicastRemoteObject implements AddSeverIntf
{
public AddServerImpl()throws RemoteException
{}
public double add(double d1,double d2)throws Remote Exception
{
return d1+d2;
}
}
AddServer.java
The source file contains the main program for the server machine.It's primary function is to update the RMI registry on that machine . This is done by 'rebind' of 'Naming' class (java.rmi).
This method associates a name with an object reference the first argument to the rebind() method is string that names the server as "AddSever" , It's second argument is a reference to an instance of "AddSeverImpl"
import java.net.*;
import jva.rmi.*;
public class AddSever
{
try
{
AddServerImpl addserverimpl =new AddServerImpl();
Naming.rebind("AddServer",addserverimpl);
}
catch(Exception e)
{
System.out.println("Exception"+e.toString());
}
}
AddClient.java
* Implements the client side of this application.
* Requires three cmd line arguments .
* first -ip address or name of server machine.
* second and third arguments are 2-nos that are to be summed
* it begins by forming a string that follows the URL syntax.
* This URL uses the RMI protocol (java.rmi).
* The string includes the ip address or name of the server and the string "AddServer"
* This program then invokes the "lookup()" method of the naming class.
* "lookup()" accepts one argument , the rmi URL, and returns to an object of type AddServerIntf.
* All remote method invocations can then be directed to this object.
The program continues by displaying its arguments and then invokes the remote 'add()' method .The sum is returned from this method and is then printed.
import java.rmi.*;
public class AddClient
{
public static void main(String args[])
{
try {
String addserverurl="rmi://"+args[0]+"AddServer";
AddServerIntf addserverintf=(AddServerIntf)Naming.lookup(addserverurl);
System.out.println("The First Number :"+args[1]);
double d1=Double.valueOf(args[1].doubleValue();
System.out.println("The Second Number :"+args[2]);
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("\nThe Sum is :"+addserverintf.add(d1,d2));
}
catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}
COMPILING PROCEDURES :
1. SAVE ALL THE FILES WITH ITS CORRESSPONDING NAME WITH IN A SAME DIRECTORY.
2. COMPILE ALL THE JAVA FILES
EX:
C:/RMI>JAVAC *.JAVA
3. CLEAR ALL THE ERRORS IF U FIND ANYTHING.
4. AFTER SUCCESSFULL COMPILATION YOU WILL GET THE CLASS FILES FOR ALL THE JAVA FILES
5. CREATING STUB AND SKELETON
EX:
C:/RMI>RMIC AddServerIntf.class
//you can get the stub and skeleton files after executing the above cmd.
6. STARTING RMI REGISTRY.
EX:
C:/RMI>START RMIREGISTRY
7. STARTING THE RMI SERVER
EX:
C:/RMI>JAVA AddServer
8. STARTNG THE RMI CLIENT
EX:
C:/RMI>JAVA AddClient sysname 10 20
OUTPUT:
C:/RMI>JAVA AddClient sysname 10 20
The First Number : 10
The second Number : 20
The Sum is : 30
MORE DETAILS
HOW TO CREAE RMI SYSTEM.
BOOK REFERENCE
JAVA COMPLETE REFERENCE 5TH EDITION BY HERBERT SCHILDT
No comments:
Post a Comment