1 / 23

Client/Server Using RMI

Client/Server Using RMI. Joe Komar. Remote Method Invocation (RMI). Can invoke methods on a remote object Integrates a distributed object model ORB compliant Extends security allowing dynamic downloading of stub classes Passes local objects by value (serialization)

rodneyh
Télécharger la présentation

Client/Server Using RMI

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. Client/Server Using RMI Joe Komar Komar Associates

  2. Remote Method Invocation (RMI) • Can invoke methods on a remote object • Integrates a distributed object model • ORB compliant • Extends security allowing dynamic downloading of stub classes • Passes local objects by value (serialization) • Passes remote objects by reference Komar Associates

  3. RMI Process (ten steps to success) • Define an interface -- extends java.rmi.Remote, all methods throw java.rim.Remote.Exception • Implement the interface -- server class that extends java.rmi.UnicastRemoteObject • Compile the server class • Run the stub compiler -- rmic classname • Generates stub for client and skeleton for server Komar Associates

  4. RMI Process (ten steps to success) • Start the RMI registry (non-persistent naming service) -- rmiregistry • Start the serve objects -- load server classes and create instances of remote objects • Register your remote objects with the registry -- use java.rmi.Naming class methods Komar Associates

  5. RMI Process (ten steps to success) • Write your client code -- use java.rmi.Naming class to locate remote object • Compile the client code • Start the client Komar Associates

  6. RMI Interface // CountRmi Interface public interface CountRMI extends java.rmi.Remote { int sum() throws java.rmi.RemoteException; void sum(int _val) throws java.rmi.RemoteException; public int increment() throws java.rmi.RemoteException; } Komar Associates

  7. RMI Interface Implementation // CountRMIImpl.java, CountRMI implementation import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class CountRMIImpl extends UnicastRemoteObject implements CountRMI { private int sum; Komar Associates

  8. RMI Interface Implementation public CountRMIImpl(String name) throws RemoteException { super(); try { Naming.rebind(name, this); sum = 0; } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } } Komar Associates

  9. RMI Interface Implementation public int sum() throws RemoteException { return sum; } public void sum(int val) throws RemoteException { sum = val; } public int increment() throws RemoteException { sum++; return sum; } } Komar Associates

  10. RMI Server // CountRMIServer.java import java.rmi.*; import java.rmi.server.*; public class CountRMIServer { public static void main(String args[]) { System.setSecurityManager(new RMISecurityManager()); try { CountRMIImpl myCount = new CountRMIImpl("my CountRMI"); System.out.println("CountRMI Server ready."); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } } } Komar Associates

  11. RMI Client // CountRMIClient.java RMI Count client import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; public class CountRMIClient { public static void main(String args[]) { // Create and install the security manager System.setSecurityManager(new RMISecurityManager()); Komar Associates

  12. RMI Client try { CountRMI myCount = (CountRMI)Naming.lookup("rmi://" + args[0] + "/" + "my CountRMI"); System.out.println("Setting Sum to 0"); myCount.sum(0); long startTime = System.currentTimeMillis(); System.out.println("Incrementing"); int count = new Integer(args[1]).intValue(); for (int i = 0 ; i < count ; i++ ) { myCount.increment(); } Komar Associates

  13. RMI Client long stopTime = System.currentTimeMillis(); System.out.println("Avg Ping = " + ((stopTime - startTime)/(float)count) + " msecs"); System.out.println("Sum = " + myCount.sum()); } catch(Exception e) { System.err.println("System Exception" + e); } System.exit(0); } } Komar Associates

  14. RMI Example Output On the Server: Count RMI Server ready. On the Client: Setting sum to 0 Incrementing Avg Ping = 14.83 msecs Sum = 500 Komar Associates

  15. RMI Hands On • There are four files in \\Nt431\Public\komar\rmi that you need to copy to your diskette • On one system find the IP name (Start/Run WINIPCFG) • Copy the files to a temporary directory on that “server” • Use this directory as your home directory for the steps on the next page Komar Associates

  16. RMI Hands On • Compile the “...Server” java program which should compile all the classes needed for the server • Run the RMI compiler using the “…Impl” file: • rmic CountRMIImpl • Copy the “…_Stub.class” file to the diskette • Run the RMI registry (start rmiregistry) • Run the server (java CountRMIServer) Komar Associates

  17. RMI Hands On • Take your diskette to a second machine and do these steps…. • Compile the “…Client.java” program • Run the client using the IP name as the first parameter and an iteration integer as the second: • java CountRMIClient s152.sci1.stthomas.edu 500 Komar Associates

  18. RMI Related classes and interfaces • RemoteException -- superclass for all RMI exceptions. All remote methods must throw a RemoteException • Remote -- interface that flags remote objects (contains no methods) • RemoteObject -- remote version of the Java root Object class • RemoteServer -- class to define methods to create server objects and export them Komar Associates

  19. RMI Related classes and interfaces • UnicastRemoteObject -- class implements a remote server object • object only exists as long as the process that created it • requires a TCP connection based transport • client and server use a stream protocol to communicate • RemoteStub -- class is superclass for all client stubs Komar Associates

  20. RMI Related classes and interfaces • Registry interface -- methods let you update entries in a registry • LocateRegistry class -- overloaded getRegistry() static methods to find a registry • Naming class -- retrieve and define remote objects using URL syntax: • rmi://host:port/name (port usually 1099) • Client uses lookup() method, host uses rebind() method Komar Associates

  21. RMI Related classes and interfaces • RMISecurityManager class -- simple security manager for RMI objects (Applets have their own security classes) • RMIClassLoader class -- load stubs and skeletons via a URL Komar Associates

  22. RMI and CORBA (Common Object Request Broker Architecture) • Moving toward one way in cooperation with OMG (Object Management Group consortium)… • RMI over IIOP (Internet InterORB Protocol) -- will be offered by JavaSoft • RMI/IDL (Interface Definition Language) -- use Java syntax to specify CORBA interfaces Komar Associates

  23. RMI Assignment • Create the Java code needed to do the following using RMI: • Perform addition, subtraction, multiplication, or division by executing a remote objects method • That remote method will accept three parameters (character operator, double val1, double val2) and return the resulting double Komar Associates

More Related