ICS 123 Java RMI ICS 123 Richard N

  • Slides: 29
Download presentation
ICS 123 Java RMI ICS 123 Richard N. Taylor and Eric M. Dashofy* UC

ICS 123 Java RMI ICS 123 Richard N. Taylor and Eric M. Dashofy* UC Irvine http: //www. isr. uci. edu/classes/ics 123 s 02/ * with the usual thanks to David Rosenblum

Topic 14 Java RMI What Is Java RMI? ICS 123 Java RMI = Java

Topic 14 Java RMI What Is Java RMI? ICS 123 Java RMI = Java Remote Method Invocation • A distributed interoperability technology for Java – Packages java. rmi, java. rmi. server and java. rmi. registry – Tools rmic and rmiregistry • Java’s answer to CORBA – Java is the interface definition language – Key limitation: both client and server must be written in Java • Part of Enterprise Java. Beans – Along with JDBC (Java Data. Base Connectivity), Java Security, Java Messaging Services, Java Transactions Services, Java IDL, other APIs – like CORBA’s common object services • Detailed documentation is available at 2 http: //java. sun. com/

Client/Server Programming with Java RMI (I) Define Topic 14 Java RMI ICS 123 an

Client/Server Programming with Java RMI (I) Define Topic 14 Java RMI ICS 123 an extension of interface java. rmi. Remote – Declare the methods the remote object exports – Declare each such method to throw java. rmi. Remote. Exception Define a subclass of java. rmi. server. Unicast. Remote. Object that also implements the extension of java. rmi. Remote defined in step 1 Write a server program that creates an instance of the class defined in step 2 – Use synchronized wherever necessary to make server thread-safe – The server program must register the instance with a registry service – Default registry server is started by running rmiregistry 3

Client/Server Programming with Java RMI (II) Topic 14 Java RMI ICS 123 Compile server

Client/Server Programming with Java RMI (II) Topic 14 Java RMI ICS 123 Compile server program with javac, and then use rmic to generate stub and skeleton classes for the remote object – Stub and skeleton take care of all networking – Limitation of rmic: the remote object class cannot be nested Write a client program that uses the remote object – The client uses class Naming to obtain a reference to the remote object, using the interface name from step 1 (not the class name) – The client invokes methods on the remote object through this reference Run 4 the server and then run the client

Client/Server Execution with Java RMI (I) Client’s machine 5 Topic 14 Java RMI ICS

Client/Server Execution with Java RMI (I) Client’s machine 5 Topic 14 Java RMI ICS 123 Server Registry Server’s machine

Client/Server Execution with Java RMI (II) Topic 14 Java RMI ICS 123 Remote Object

Client/Server Execution with Java RMI (II) Topic 14 Java RMI ICS 123 Remote Object Client’s machine 6 Server Registry Server’s machine

Client/Server Execution with Java RMI (III) Topic 14 Java RMI ICS 123 skeleton Remote

Client/Server Execution with Java RMI (III) Topic 14 Java RMI ICS 123 skeleton Remote Object Client Server Naming. rebind(…) Client’s machine 7 Registry Server’s machine

Client/Server Execution with Java RMI (IV) Topic 14 Java RMI ICS 123 skeleton Remote

Client/Server Execution with Java RMI (IV) Topic 14 Java RMI ICS 123 skeleton Remote Object Client stub Naming. lookup(…) Client’s machine 8 Server Registry Server’s machine

Client/Server Execution with Java RMI (V) Remote Method Invocation Topic 14 Java RMI ICS

Client/Server Execution with Java RMI (V) Remote Method Invocation Topic 14 Java RMI ICS 123 skeleton Remote Object Client’s machine 9 stub Server Registry Server’s machine

Topic 14 Java RMI Default RMI Registry ICS 123 • Server on machine. com

Topic 14 Java RMI Default RMI Registry ICS 123 • Server on machine. com registers object with local RMI registry using some string name Naming. rebind(“remote. Obj”, reference. To. Remote. Obj) • Client refers to objects with URL – Local client: rmi: ///remote. Obj – Remote client: rmi: //machine. com/remote. Obj – If client code contains Naming. lookup(“rmi: //” + obj. Name) » obj. Name would be “/remote. Obj” for local object » obj. Name would be “machine. com/remote. Obj” for remote object • Registry is bound to port 1099 by default – Accepts optional port number as argument – Non-default port number must then be added to URL 10

Example: Air Traffic Control Topic 14 Java RMI ICS 123 • Air traffic control

Example: Air Traffic Control Topic 14 Java RMI ICS 123 • Air traffic control is divided into several sectors • Each sector is managed by a different controller • Airplanes check in with a controller and then periodically send the controller position reports • The controller analyzes position reports and tells an airplane when its position is too close to other planes 11

Air Traffic Control: ATC Exception Topic 14 Java RMI ICS 123 package ATC; public

Air Traffic Control: ATC Exception Topic 14 Java RMI ICS 123 package ATC; public class ATCException extends Exception { public ATCException(String msg) { super(msg); } } • For server-specific exceptions • Different from Remote. Exception, which is thrown as a result of RMI failure 12

Air Traffic Control: Position Report Data Topic 14 Java RMI ICS 123 package ATC;

Air Traffic Control: Position Report Data Topic 14 Java RMI ICS 123 package ATC; public class public int Position. Report implements java. io. Serializable { Latitude; Longitude; Heading; Airspeed; public Position. Report(int la, int lo, int h, int a) { Latitude = la; Longitude = lo; Heading = h; Airspeed = a; } } • Java objects can be passed in RMIs, as long as they are serializable – Usually accomplished with an implementation of (empty) interface Serializable – Stub and skeleton take care of serialization/deserialization 13

Air Traffic Control: Remote Controller’s Interface Topic 14 Java RMI ICS 123 package ATC;

Air Traffic Control: Remote Controller’s Interface Topic 14 Java RMI ICS 123 package ATC; import java. rmi. *; public interface Controller. Interface extends Remote { public void Check. In(int flight) throws Remote. Exception; public void Current. Position(int flight, Position. Report pos) throws Remote. Exception, ATCException; } 14

Air Traffic Control: Remote Controller Class (I) Topic 14 Java RMI ICS 123 package

Air Traffic Control: Remote Controller Class (I) Topic 14 Java RMI ICS 123 package ATC; import java. net. *; // for Inet. Address import java. rmi. *; import java. rmi. server. *; public class Controller extends Unicast. Remote. Object implements Controller. Interface { protected String my. Name; public Controller(String port, String name) throws Remote. Exception { super(); try { my. Name = "//" + Inet. Address. get. Local. Host(). get. Host. Name() + ": " + port + "/" + name; Naming. rebind(my. Name, this); } catch (Exception ex) { System. err. println("Exception " + ex); } } … } 15

Air Traffic Control: Remote Controller Class (II) Topic 14 Java RMI ICS 123 public

Air Traffic Control: Remote Controller Class (II) Topic 14 Java RMI ICS 123 public class Controller extends Unicast. Remote. Object implements Controller. Interface { … public void Check. In(int flight) throws Remote. Exception { record existence of new flight } public void Current. Position(int flight, Position. Report pos) throws Remote. Exception, ATCException { if (pos. latitude < -90 || pos. latitude > 90 || pos. longitude < -180 || pos. longitude > 180 || pos. heading < 1 || pos. heading > 360 || pos. airspeed < 50 || pos. airspeed > 700) throw new ATCException("flight " + String. value. Of(flight) + ": invalid position"); else if ( flight is too close to other airplanes ) throw new ATCException("flight " + String. value. Of(flight) + ": dangerous position"); } } 16

Air Traffic Control: ATC Server Topic 14 Java RMI ICS 123 package ATC; import

Air Traffic Control: ATC Server Topic 14 Java RMI ICS 123 package ATC; import java. rmi. *; public class ATCServer { public static void main(String[] args) { try { Controller c = new Controller(args[0], args[1]); } catch (Exception ex) { System. err. println("Exception " + ex); System. err. println("usage: java ATCServer port# controller. Name"); } } } 17

Air Traffic Control: Airplane Client (I) Topic 14 Java RMI ICS 123 package ATC;

Air Traffic Control: Airplane Client (I) Topic 14 Java RMI ICS 123 package ATC; import java. rmi. *; public class Airplane { protected int my. Flight. Number; protected String my. Controller; protected Position. Report my. Position = new Position. Report(34, -118, 180, 350); public static void main(String[] args) { try { my. Flight. Number = Integer. parse. Int(args[0]); my. Controller = args[1]; Controller. Interface c = // The interface, not the class! (Controller. Interface)Naming. lookup("rmi: //" + my. Controller); c. Check. In(my. Flight. Number); … } } 18

Air Traffic Control: Airplane Client (II) Topic 14 Java RMI ICS 123 public class

Air Traffic Control: Airplane Client (II) Topic 14 Java RMI ICS 123 public class Airplane { … public static void main(String[] args) { try { … for (; ; ) { c. Current. Position(my. Flight. Number, my. Position); java. lang. Thread. sleep(200); update position } } catch (Remote. Exception ex) { System. err. println("Remote. Exception " + ex); } catch (ATCException ex) { System. err. println("ATCException " + ex); } catch (Exception ex) { System. err. println("Exception " + ex); System. err. println("usage: java Airplane flight# controller. Name"); } } } 19

Air Traffic Control: GNUmakefile CLASSFILES=ATC/ATCException. class  ATC/ATCServer. class  ATC/Airplane. class  ATC/Controller.

Air Traffic Control: GNUmakefile CLASSFILES=ATC/ATCException. class ATC/ATCServer. class ATC/Airplane. class ATC/Controller. Interface. class ATC/Position. Report. class REMOTECLASS=ATC/Controller. class RMIFILES=ATC/Controller_Skel. class ATC/Controller_Stub. class. SUFFIXES: . java. class all: $(CLASSFILES) $(RMIFILES) : $(REMOTECLASS) rmic ATC. Controller mv -f *. class ATC. java. class : javac $< 20 Topic 14 Java RMI ICS 123

Topic 14 Java RMI Compiling the Example elysees 1332> ls ATC/ GNUmakefile elysees 1333>

Topic 14 Java RMI Compiling the Example elysees 1332> ls ATC/ GNUmakefile elysees 1333> ls ATCException. java Controller. java ATCServer. java Controller. Interface. java Airplane. java Position. Report. java elysees 1334> gmake all +elysees+ javac ATC/ATCException. java +elysees+ javac ATC/ATCServer. java +elysees+ javac ATC/Airplane. java +elysees+ rmic ATC. Controller +elysees+ mv -f Controller_Skel. class Controller_Stub. class ATC elysees 1335> ls ATCException. class Controller. java ATCException. java Controller. Interface. class ATCServer. class Controller. Interface. java ATCServer. java Controller_Skel. class Airplane. class Controller_Stub. class Airplane. java Position. Report. class Controller. class Position. Report. java elysees 1336> 21 ICS 123

Running the Example: Local Client and Server Topic 14 Java RMI ICS 123 Server

Running the Example: Local Client and Server Topic 14 Java RMI ICS 123 Server on elysees (using the default registry port number): elysees [1] elysees [2] elysees 1352> rmiregistry & 5398 1353> java ATCServer 1099 Los. Angeles & 5407 1354> Client on elysees: elysees 1355> java ATC. Airplane 100 /Los. Angeles ATCException ATCException: flight 100: invalid position elysees 1356> java ATC. Airplane 100 elysees. ics. uci. edu/Los. Angeles ATCException ATCException: flight 100: invalid position elysees 1357> 22

Running the Example: Remote Client and Server Topic 14 Java RMI ICS 123 Server

Running the Example: Remote Client and Server Topic 14 Java RMI ICS 123 Server on elysees (using a non-default registry port number): elysees [1] elysees [2] elysees 1352> rmiregistry 1033 & 5398 1353> java ATCServer 1033 Los. Angeles & 5407 1354> Client on octavian: octavian 1356> java ATC. Airplane 100 /Los. Angeles Remote. Exception java. rmi. Connect. Exception: Connection refused to host: [octavian. ics. uci. edu: 1099]; nested exception is: java. net. Connect. Exception: Connection refused octavian 1357> java ATC. Airplane 100 elysees. ics. uci. edu/Los. Angeles Remote. Exception java. rmi. Connect. Exception: Connection refused to host: [elysees. ics. uci. edu: 1099]; nested exception is: java. net. Connect. Exception: Connection refused octavian 1358> java ATC. Airplane 100 elysees. ics. uci. edu: 1033/Los. Angeles ATCException ATCException: flight 100: invalid position octavian 1359> 23

Topic 14 Java RMI Other Possible Architectures ICS 123 • A program or remote

Topic 14 Java RMI Other Possible Architectures ICS 123 • A program or remote object can act as both a client and a server • Example: 3 -Tiered Client/Server Client Program Server Program Legacy Program Remote Business Object 24 Remote Data Object

Principal Similarities Between CORBA and RMI Topic 14 Java RMI ICS 123 • Suitable

Principal Similarities Between CORBA and RMI Topic 14 Java RMI ICS 123 • Suitable for distributed, object-oriented, client/server systems • Synchronous interaction via remote procedure call (RPC) • RPC implemented via client stubs and server skeletons, which hide networking and data representation • Objects can be both client and server 25

Principal Differences Between CORBA and RMI Topic 14 Java RMI ICS 123 • CORBA

Principal Differences Between CORBA and RMI Topic 14 Java RMI ICS 123 • CORBA – – – Independent of implementation language Operation invocations can be formed statically or dynamically Object implementation binding can be static or dynamic The CORBA object adapters define various object execution semantics Many integrated object services are available – – – Requires objects to be programmed in Java Objects are platform independent Operation invocations are formed statically Mapping of names to objects is static Many object services are being defined within Enterprise Java. Beans Mobile code support • RMI 26

Sidebar Discussion: Dynamic Proxies in Java Topic 14 Java RMI ICS 123 • Introduced

Sidebar Discussion: Dynamic Proxies in Java Topic 14 Java RMI ICS 123 • Introduced in J 2 SEv 1. 3 (JDK 1. 3) • Created using the java. lang. reflect. Proxy class • Allows dynamic creation of objects that implement an arbitrary interface class without writing code • All methods are called through a single invoke() method, which you implement: – public Object invoke(Object proxy, Method method, Object[] args) throws Throwable • You can get a proxy with a call like this: – Action. Listener listener = (Action. Listener)Proxy. create. Proxy(my. Invocation. Handler, Action. Listener. class); » Some detail omitted for clarity 27

Why are Dynamic Proxies useful? Topic 14 Java RMI ICS 123 • A generic

Why are Dynamic Proxies useful? Topic 14 Java RMI ICS 123 • A generic “listener” for Java events – Consider a generic debugging framework for Swing events that can print every event to the screen » Don’t need to create a new implementation for each type of listener (Action. Listener, Window. Listener, Mouse. Listener, etc. ) • Dynamically create stubs for an RMI-like middleware – This is implemented in a middleware called LJM (Lightweight Java Middleware), which ships with Arch. Studio 3 – LJM allows you to do basically what RMI does except without having to use rmic – LJM does not currently support mobile code aspects of RMI 28

Topic 14 Java RMI Discussion ICS 123 • How would we extend the example

Topic 14 Java RMI Discussion ICS 123 • How would we extend the example to allow a Controller to give an Airplane instructions for avoiding a dangerous position? • How would we extend the example to allow the Controller to indicate an ATC exception condition outside of a call to Current. Report()? • How would we extend the example to allow the Controller to tell the Airplane to check in with a different Controller? 29