Upload
swarupkulkarni
View
1.908
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
INTRODUCTION TO RMI
By
Swarup Kulkarni(MCA-II, VIT, Pune)
2
REMOTE METHOD INVOCATION RMI - Remote Method Invocation Allows to invoke a method of a Java object that
executes on another machine. Important feature, because it allows to build
distributed applications. Lets discus a simple Client/Server application
using RMI. The server receives a request from client and
processes it In this req. two numbers are specified Server add these numbers and returns it
3/11/2009R
MI - Sw
arup Kulkarni
3
STEP 1: ENTER AND COMPILE THE SOURCE CODE We use four source files - First source file:
RmiDemoIntf.java Defines remote interface, a remote method, All remote interfaces must inherit Remote interface
Remote interface is a part of java.rmi.remote Defines no members , purpose is to indicate that interface uses remote
methods.All remote methods can throw RemoteException
3/11/2009R
MI - Sw
arup Kulkarni
4
STEP 1: (CONTINUED..)
The first source file - Code:
RmiDemoIntf.javaimport java.rmi.*;public interface RmiDemoIntf extends Remote {
double add(double d1, double d2) throws RemoteException;
}
3/11/2009R
MI - Sw
arup Kulkarni
5
STEP 1: (CONTINUED..)
The second source file:RmiDemoImpl.java
Implements Remote interface implementation of the add( ) method is
straightforward All remote objects must extend UnicastRemoteObject
UnicastRemoteObject provides functionality that is needed to make objects
available from remote machines
3/11/2009R
MI - Sw
arup Kulkarni
6
STEP 1: (CONTINUED..) The second source file - Code:
RmiDemoImpl.javaimport java.rmi.*;import java.rmi.server.*;public class RmiDemoImpl extends
UnicastRemoteObject implements RmiDemoIntf {
public double add(double d1, double d2) throws
RemoteException {return d1 + d2;
}}
3/11/2009R
MI - Sw
arup Kulkarni
7
STEP 1: (CONTINUED..)
The third source file:RmiServer.java
Contains main program for the server machine This modifies the RMI registry of the machine
using rebind() method of Naming class(found in java.rmi)
Naming.rebind()• Associates name with the object reference• First argument names server as “RmiServer”• Second argument is reference to object of
RmiDemoImpl
3/11/2009R
MI - Sw
arup Kulkarni
8
STEP 1: (CONTINUED..) The third source file - Code:
RmiServer.javaimport java.net.*;import java.rmi.*;public class RmiServer{
public static void main(String args[]) {try {
RmiDemoImpl obj_RmiDemoImpl = new RmiDemoImpl();Naming.rebind(“RmiServer", obj_RmiDemoImpl);
}catch(Exception e) {
System.out.println("Exception: " + e);}
}}
3/11/2009R
MI - Sw
arup Kulkarni
9
STEP 1: (CONTINUED..) The forth source file:
RmiClient.java Implements client side of this distributed
application. Requires thee main things – IP address or name of
server, and the two numbers to be added. We create a string that follows URL syntax. The URL includes IP or Name of the server and the
string “RmiServer”. The program invokes lookup() method from
Naming class which accepts rmi URL as argument and returns a reference of object of RmiDemoIntf .
Now all the remote method invocations can be directed to this object.
3/11/2009R
MI - Sw
arup Kulkarni
10
STEP 1: (CONTINUED..)
The forth source file - Code:RmiClient.java
import java.rmi.*;public class RmiClient {
public static void main(String args[]) {try {
double d1 = 24.73;double d2 = 11.12;String RmiDemoURL = "rmi://" + “127.0.0.1” +
"/RmiServer";//127.0.0.1 is loopback address replace it with server IP/nameRmiDemoIntf obj =
(RmiDemoIntf)Naming.lookup(RmiDemoURL);
3/11/2009R
MI - Sw
arup Kulkarni
11
STEP 1: (CONTINUED..)
RmiClient.java(Contd..)System.out.println("The first number is: " + d1);System.out.println("The second number is: " + d2);System.out.println("The sum is: " +
RmiDemoIntf.add(d1, d2)); }//end of try
catch(Exception e) {System.out.println("Exception: " + e);
}//end of catch
}//end of main
}//end of class
3/11/2009R
MI - Sw
arup Kulkarni
12
STEP 2: GENERATE STUBS AND SKELETONS Before using client and server we have to generate stub and
skeleton. Stub resides on client machine. Remote method calls initiated by client are directed to stub. A remote method can have simple arguments or objects as
arguments. Objects may have reference to other objects, to send whole
information to server the objects in arguments must be serialized.
Skeletons are not required by Java 2.(Needed for compatibility with java1.1)
Skeleton resides on sever machine, when it receives request, it performs deserialization and invokes appropriate method.
In case of response to client, the process works reverse.
3/11/2009R
MI - Sw
arup Kulkarni
13
STEP 2: (CONTINUED..) How to generate stub and Skeleton?
Use a tool called RMI Compiler, which is invoked from command line:
rmic RmiDemoImpl;This command generates two new files:
RmiDemoImpl_Skel.class (skeleton) and RmiDemoImpl_Stub.class (stub).
Before using rmic make sure that CLASSPATH is set to include current directories.
rmic by default generates stub as well as skeleton, we have option to suppress it.
3/11/2009R
MI - Sw
arup Kulkarni
14
STEP 3: INSTALL FILES ON CLIENT AND SERVER Copy RmiDemo.class, RmiDemoImpl_Stub.class, and
RmiDemoImpl.class on the client machine.
Copy RmiDemontf.class, RmiDemoImpl.class, RmiDemompl_Skel.class, RmiDemompl_Stub.class, and RmiDemo.class on the server machine.
3/11/2009R
MI - Sw
arup Kulkarni
15
STEP 4: START THE RMI REGISTRY ON THE SERVER The Java 2 SDK provides a program called rmiregistry,
which executes on server.
The rmiregistry maps names to object reference.
First, check that the CLASSPATH environment variable includes the directory in which your files are located and start RMI Registry:
start rmiregistry
When this command runs, it creates a new window. Leave that window open until the experimentation finishes.
3/11/2009R
MI - Sw
arup Kulkarni
16
STEP 5: THE EXPERIMENT The server code is started from the command line, as
shown here:java RmiServer
Recall that the RmiServer code instantiates RmiDemoImpl and registers that object with the name “RmiServer”.
The client code is started from command line as shown here:
java RmiClient Output of progarm:
The first number is: 24.73The second number is: 11.12The sum is: 35.85
3/11/2009R
MI - Sw
arup Kulkarni
17
3/11/2009R
MI - Sw
arup Kulkarni
THANK YOU