240 likes | 284 Views
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)
E N D
Client/Server Using RMI Joe Komar Komar Associates
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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