• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction To Rmi
 

Introduction To Rmi

on

  • 1,735 views

 

Statistics

Views

Total Views
1,735
Views on SlideShare
1,733
Embed Views
2

Actions

Likes
0
Downloads
40
Comments
0

1 Embed 2

http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Introduction To Rmi Introduction To Rmi Presentation Transcript

    • Introduction To RMI
      By
      Swarup Kulkarni
      (MCA-II, VIT, Pune)
    • 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/2009
      2
      RMI - Swarup Kulkarni
    • 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/2009
      3
      RMI - Swarup Kulkarni
    • Step 1: (Continued..)
      The first source file - Code:
      RmiDemoIntf.java
      import java.rmi.*;
      public interface RmiDemoIntf extends Remote {
      double add(double d1, double d2) throws RemoteException;
      }
      3/11/2009
      4
      RMI - Swarup Kulkarni
    • 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/2009
      5
      RMI - Swarup Kulkarni
    • Step 1: (Continued..)
      The second source file - Code:
      RmiDemoImpl.java
      import 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/2009
      6
      RMI - Swarup Kulkarni
    • 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/2009
      7
      RMI - Swarup Kulkarni
    • Step 1: (Continued..)
      The third source file - Code:
      RmiServer.java
      import 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/2009
      8
      RMI - Swarup Kulkarni
    • 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/2009
      9
      RMI - Swarup Kulkarni
    • 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/name
      RmiDemoIntf obj = (RmiDemoIntf)Naming.lookup(RmiDemoURL);
      3/11/2009
      10
      RMI - Swarup Kulkarni
    • 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/2009
      11
      RMI - Swarup Kulkarni
    • 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/2009
      12
      RMI - Swarup Kulkarni
    • 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/2009
      13
      RMI - Swarup Kulkarni
    • 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/2009
      14
      RMI - Swarup Kulkarni
    • 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/2009
      15
      RMI - Swarup Kulkarni
    • 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.73
      The second number is: 11.12
      The sum is: 35.85
      3/11/2009
      16
      RMI - Swarup Kulkarni
    • 3/11/2009
      17
      RMI - Swarup Kulkarni
      Thank You