1 / 25

Internet Software Development

Internet Software Development. Remote Method Invocation Paul Krause. Lecture 10 - RMI. Introduction to RMI Simple Example - “DivideServer” Demo of this example Review a more complex example - “StudentEnrollment”. What is RMI?.

mahsa
Download Presentation

Internet Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internet Software Development Remote Method Invocation Paul Krause

  2. Lecture 10 - RMI • Introduction to RMI • Simple Example - “DivideServer” • Demo of this example • Review a more complex example - “StudentEnrollment”

  3. What is RMI? • A high-level networking technology for distributed Java applications • Allows a Java object executing on one machine to remotely invoke methods in an object executing on a second machine • Such method calls have the appearance of operating on objects in the same program

  4. Server Machine Client Machine Client Stub Java Virtual Machine and RMI System Server Skeleton Java Virtual Machine and RMI System C S Skeletons and Stubs

  5. Stub • Presents same interfaces as remote object • Works with JVM and RMI to serialise arguments in the remote method call • Receives results from the remote method call and returns them to the client object

  6. Skeleton • Receives the remote method call and associated arguments • Works with JVM and RMI to deserialize arguments in the remote method call • Invokes appropriate methods in the remote object • Receives return value (if any) from the method call, serializes it and returns it to the client • No longer needed in Java 2

  7. Referencing remote objects • A Registry object maintains a mapping from Server names to remote object references • We will use the example “DivideServer” • The remote object is named via a URL using the RMI protocol: • rmi://host:port/server • host - IP address or name of server machine • port - optional port number of the Registry on above • server - name of the remote server • e.g. rmi://127.0.0.1/DivideServer

  8. Key Classes and Interfaces • Remote Interface • Naming Class • RemoteObject Class • RemoteServer Class • UnicastRemoteObject Class • RemoteException Class

  9. Remote Interface • In the java.rmi package • Contains no constants or methods • It is used to designate which interfaces are remote - • remote interfaces must extend java.rmi.Remote • remote objects must implement a remote interface

  10. Naming Class • In the java.rmi package • Three methods to associate names with remote objects (used at the Server side): • static void bind(String name, Remote robj) • static void rebind(String name, Remote robj) • static void unbind(String name) • One method to obtain a name for a remote object (used at the Client side): • static Remote lookup(String url)

  11. Remote Objects • RemoteObject • extends java.lang.Object to provide correct behaviour for remote objects • RemoteServer • an abstract class that extends RemoteObject. Defines the methods needed to create and export remote objects • UnicastRemoteObject • a concrete subclass of RemoteServer. Extend this class to make a remote object

  12. Simple Client/Server Application • Client program takes three arguments: • IP address or name of remote server • two numbers • The two numbers are passed to the server • The server divides one into the other and returns the result • From: JavaBeans Programming from the Ground Up, Joseph O’Neil and Herb Schildt

  13. DivideServer.java import java.rmi.*; public interface DivideServer extends Remote { double divide(double d1, double d2) throws RemoteException; }

  14. Remote Interfaces • The remote interface must extend Remote • Remote methods can throw a RemoteException • We now require a server that implements the interface • By convention, we name the implementation of DivideServer • DivideServerImpl • Remote objects must extend UnicastRemoteObject

  15. DivideServerImpl.java import java.rmi.*;import java.rmi.server.*;public class DivideServerImpl extends UnicastRemoteObject implements DivideServer { public DivideServerImpl( ) throws RemoteException { } public double divide(double d1, double d2) throws RemoteException { return d1/d2; }}

  16. DivideServerApp.java • This is the main program for the server side • Its primary function is to bind the name “DivideServer” to an instance of DivideServerImpl • Note that we need to start this server program running before the client program is invoked

  17. DivideServerApp.java • Core part is: DivideServerImpl divideServerImpl; divideServerImpl = new DivideServerImpl(); Naming.rebind("DivideServer", divideServerImpl);

  18. DivideServerApp.java import java.net.*; import java.rmi.*; public class DivideServerApp { public static void main(String args[]) { try { DivideServerImpl divideServerImpl; divideServerImpl = new DivideServerImpl(); Naming.rebind("DivideServer", divideServerImpl); } catch(Exception ex) { ex.printStackTrace(); } } }

  19. DivideClient.java • Looks up the object that is bound to the RMI URL • The lookup method of the Naming class returns an object of type Remote • This must then be cast to a DivideServer in order to be able to access the divide method

  20. DivideClient.java DivideServer divideServer; divideServer = (DivideServer)Naming.lookup(divideServerURL); // Invoke remote method and display results double result = divideServer.divide(d1, d2); System.out.println("The result is: " + result);

  21. DivideClient.java import java.rmi.*; public class DivideClient { public static void main(String args[]) { try { // Make rmi URL to name DivideServer String divideServerURL; divideServerURL = "rmi://" + args[0] + "/DivideServer"; // Obtain reference to that remote object DivideServer divideServer; divideServer = (DivideServer)Naming.lookup(divideServerURL);

  22. DivideClient.java // Display numbers 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(); // Invoke remote method and display results double result = divideServer.divide(d1, d2); System.out.println("The result is: " + result); } catch(Exception ex) { ex.printStackTrace(); } } }

  23. Executing the Application • Use javac to compile all files • Open a command prompt and change to the working directory • Use the rmic compiler to produce a stub class for the remote server class: • rmic -v1.2 DivideServerImpl • Copy all files to the client machine • exercise: You don’t need all of them on the client. Which ones do you need

  24. Executing the Application • open a command prompt on the server and start the RMI registry: • rmiregistry • open a second command prompt on the server machine and run the server application to bind the remote server object to the registry • java DivideApp • Start the client (again, use a command prompt): • java DivideClient server1 8 2

  25. The result • You should get the following output at the client’s command prompt: The first number is: 8 The second number is: 2 The result is: 4.0 • Notes: • Use IP address 127.0.0.1 if experimenting on a single machine • On Windows 2000 you can find out the IP address of a machine using hostname and ipconfig/all

More Related