Results 1 to 2 of 2

Thread: rmi

  1. #1
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Smile rmi

    Schildt: how to add two numbers using RMI

    This very simple example is taken from Chapter 24 of "Java 2: The Complete Reference" by P.Naughton and H.Schildt.

    The example is overly simplified but it still illustrates the basic steps in creating an RMI distributed program.

    This example provides step-by-step directions for building a client/server application by using RMI. The server receives a request from a client, processes it, and returns a result. In this example, the request specifies two numbers, the server adds these together and returns the sum. That is it!

    (Of course, this program is intended only to illustrate the basic RMI mechanism.)

    The next subsections provide main steps in writing an RMI program

    Define an interface that declares remote methods.

    The first file AddServerIntf.java defines the remote interface: it includes one method that accepts two double arguemnts and returns their sum. All remote interfaces must extend the interface Remote, that defines no methods: its purpose is simply to indicate that an interface uses remote methods.
    All remote methods should throw a RemoteException


    import java.rmi.*;

    public interface AddServerIntf extends Remote {
    double add(double d1, double d2) throws RemoteException;
    }

    Implement the remote interface and the server

    The second source file AddServerImpl.java implements the remote interface:

    import java.rmi.*;
    import java.rmi.server.*;

    public class AddServerImpl extends UnicastRemoteObject
    implements AddServerIntf {

    public AddServerImpl() throws RemoteException {
    }
    public double add(double d1, double d2) throws RemoteException {
    return d1 + d2;
    }
    }

    All remote objects must extend UnicastRemoteObject which provide the functionaly that is needed to make objects available from remote machines.
    The third source file AddServer.java contains the main program for the server machine. Its primary function is to update the RMI registry on that machine. This is done by using the rebind() method of the Naming class (it is in java.rmi API). That method associates a name with an object reference.

    import java.net.*;
    import java.rmi.*;

    public class AddServer {
    public static void main(String args[]) {
    try {
    AddServerImpl addServerImpl = new AddServerImpl();
    Naming.rebind("AddServer", addServerImpl);
    }
    catch(Exception e) {
    System.out.println("Exception: " + e);
    }
    }
    }

    Develop a client (an application or an applet) that uses the remote interface

    The fourth source file AddClient.java implements the client side of this distributed application. This program requires 3 command line arguments: the IP address or name of the remote server, and two numbers that are to be summed.
    The application forms an URL string using the rmi protocol, the first command line argument and the name "AddServer" that is used by naming registry of the server. The the program calls the method lookup() of the Naming class to find a reference to a remote object. All remote method invocations can then be directed to this object.

    The client program illustrates the remote call by using the method add(d1,d2) that will be invoked on the remote server machine from the local client machine where the client runs.

    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 is: " + args[1]);
    double d1 = Double.valueOf(args[1]).doubleValue();
    System.out.println("The second number is: " + args[2]);

    double d2 = Double.valueOf(args[2]).doubleValue();
    System.out.println("The sum is: " + addServerIntf.add(d1, d2));
    }
    catch(Exception e) {
    System.out.println("Exception: " + e);
    }
    }
    }

    Generate stubs and skeletons

    Compile all aforementioned Java source files and keep all generated .class files in the same directory.
    Before you can use the client and the server, you must generate the necessary stub. You may also need to generate a skeleton, if a server runs on a machine that does not have Java 2, but has an earlier version of JVM.

    In RMI, a stub is a java class that either resides on the client machine, or downloaded to the client machine dynamically from elsewhere (e.g., from the web server). The function of stub is to present the same interfaces as the remote server. Remote method calls initiated by the client are actually directed to the stub. The stub works with the other parts of the RMI system to formulate a request that is sent to the remote machine.

    A remote method may accept arguments that are simple types or objects. In the latter case, the object may have references to other objects. All of this information must be sent to the remote machine. That is, an object passed as an argument to a remote method call must be serialized and sent to the remote machine. If a response must be returned to the client, the process works in reverse. Note that the serialization and deserialization facilities are also used if objects are returned to a client.

    Skaletons are not required by Java 2, but required for the Java 1.1 RMI model. For this reason, skeletons are still necessary for compatibility between Java 1.1 and Java2. A skeleton is a Java class that resides on the server machine. It works with other parts of the 1.1 RMI system to receive requests, perform deserialization, and invoke the appropriate code on the server. It is used in this example to illustrate the compatibility.

    To generate stubs and skeletons, you use a tool called the RMI compiler:

    rmic AddServerImpl
    This command generates two new files: AddServerImpl_Skel.class (skeleton) and AddServerImpl_Stub.class (stub). If you do not need the skeleton, run rmic -v1.2 to turn on Java 2 option. (read details in the rmic manual page).


    The easiest way to try a distributed architecture is to copy all required files manually to correct machines/directories:

    Copy AddClient.class (client), AddServerImpl_Stub.class (stub), AddServerIntf.class (interface) to a directory on the client machine (e.g., to a machine that runs Windows NT).
    Copy AddServerIntf.class (interface), AddServerImpl.class (its implementation), AddServerImpl_Skel.class (skeleton), AddServerImpl_Stub.class (stub) and AddServer.class (server itself) to a directory on the server machine (e.g., to jupiter).
    Of course, RMI has techniques for dynamic class loading, they will be illustrated below in a more elaborated version of this example. For now, we would like to test the client/server without going into other details.
    Start the RMI registry

    Go to the directory on the server machine where you keep files mentioned above. Check that the CLASSPATH environment variable includes the directory in which your files are located (for example, CLASSPATH has . entry - the current directory). Then:
    If your server is on another Windows machine: start rmiregistry
    If your server is on jupiter or on another UNIX machine: rmiregistry &
    Start the server

    In the same directory:
    java AddServer
    Run the client

    Now go to your local machine.
    Suppose that you started the server on jupiter.scs.ryerson.ca Then, you can start the client on your local machine as follows:


    java AddClient jupiter.scs.ryerson.ca 567 999
    The first number is: 567.0
    The second number is: 999.0
    The sum is: 1566.0

    If you do not have an Internet connection, you can run all programs of this example on your local machine (both server and client). In this case, you can run
    java AddClient 127.0.0.1 567 999
    to use the "loop back" address (127.0.0.1) for the local machine. This will allow you to test the entire RMI mechanism without actually having to install the server on a remote computer.

  2. #2
    hasimshaikh is offline Member
    Join Date
    Apr 2014
    Location
    PUNE
    Posts
    1
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •