Java Remote Method Invocation RMI Distributed Systems a

Java Remote Method Invocation (RMI) )

Distributed Systems a collection of independent computers that appears to its users as a single coherent system

Models of Distribution Message passing Distributed objects Event-based architectures Space-based paradigms

Distributed Object Model Views a distributed system as a series of interacting objects Based on some underlying message passing protocol invisible to the programmer Three main technologies: RMI, CORBA and DCOM

Distributed Object Computing Enable any object in the local system to directly interact with an object on a remote host Goals: Let any object reside anywhere in the network, and allow an application to interact with these objects in the same way as they do with a local object. Provide the ability to construct an object on one host and transmit it to another host. Enable an agent on one host to create an object on another host.

What Is RMI? A mechanism that allows the invocation of a method that exists in another address space Java-to-Java only Client-Server Protocol High-level API Transparent Lightweight

Related Technologies RPC (“Distributed Common Object Model”) Developed by Microsoft Access to Win 32 objects LDAP (“Common Object Request Broker Architecture”) Developed by OMG Access to non-Java objects (as well as Java) DCOM Developed by Sun Platform-specific CORBA (“Remote Procedure Calls”) (“Lightweight Directory Access Protocol”) Finding resources on a network

RMI Client – the process that invokes a method on a remote object Server – the process that owns the remote object Object Registry – a name server that relates objects with names Objects are registered with the Object Registry, under a unique name. The Object Registry is used to obtain access to remote objects, using their names.

Remote Objects (Diagram) Java Virtual Machine Client Object Remote Object Client TCP Server

RMI Layers Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer 11/18/2003 TCP Transport Layer

RMI Architecture in the OSI model Application Layer Presentation Layer User Application Stub Skeleton Session Layer Remote Reference Layer Transport Layer TCP Network Layer IP Data-link layer Physical Layer Hardware Interface Network

Java Virtual Machine Remote Objects Remote Object Stub Skeleton Remote Reference Layer Transport Layer Client Remote Live Java Virtual Machine Client Objects on server Accessed as if they were local TCP Remote Reference Layer Transport Layer Server

Java Virtual Machine Stubs and Skeletons Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer TCP Remote Reference Layer Transport Layer Client Stub lives on client pretends to be remote object - a proxy for the remote object Skeleton Server lives on server receives requests from stub, talks to the remote object and delivers response to stub Stubs and skeletons are not written by the programmer! They are generated by a special compiler “rmic”

Java Virtual Machine Stubs and Skeletons Remote Object Stub Skeleton Remote Reference Layer Transport Layer Client Stub Java Virtual Machine Client Object TCP Remote Reference Layer Transport Layer Server – responsibilities Initiate remote calls Marshals arguments to be sent Informs the remote reference layer that a call should be invoked on the server Unmarshals a return value (or exception) Informs the remote reference layer that the call is complete

Java Virtual Machine Stubs and Skeletons Remote Object Stub Skeleton Remote Reference Layer Transport Layer Client Skeleton Java Virtual Machine Client Object TCP Remote Reference Layer Transport Layer Server – responsibilities Unmarshals incoming arguments Calls the actual remote object implementation Marshals return values for transport to the client Marshaling The – definition process of converting native programming language data types to a format suitable for transmission across a network

Remote Interfaces and Stubs Remote Interface implements Client 11/18/2003 Stub implements Skeleton Remote Object (Server)

Remote Interfaces Declare exposed methods – the methods that can be called from remote locations Extend java. rmi. Remote The remote object implements this interface Act like a proxy for the remote object

Java Virtual Machine Remote Reference Layer Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer TCP Client Sets up connections to remote address spaces Manages connections Listens for incoming calls Communicates via TCP/IP Remote Reference Layer Transport Layer Server

Object Registries Name and look up remote objects Remote objects register by name Clients obtain a remote reference to the remote object A registry is a running process on the same host as the RMI server

HTTP Tunneling Cool: if it can’t make the connection normally, it will tunnel through port 80 Allows clients behind firewall to make remote calls to server Note: does not work server -> client

RMI System Architecture Client Virtual Machine Server Virtual Machine Client Remote Object Skeleton Stub Server Client “Fred” Registry Virtual Machine Registry

RMI Flow 1. Server Creates Remote Object 2. Server Registers Remote Object Client Server Virtual Machine Remote Object 1 Skeleton Stub 2 “Fred” Registry Virtual Machine Server

RMI Flow Server Virtual Machine Client 3. Client requests object from Registry 4. Registry returns remote reference Skeleton Stub 3 4 “Fred” Registry Virtual Machine Server

RMI Flow Client Virtual Machine Server Virtual Machine Client Remote Object 5 7 6 Stub Skeleton 5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object method Registry Virtual Machine Server

Part II: RMI Usage 11/18/2003 and Distributed Programming

Creating Remote Objects Define a Remote Interface extends java. rmi. Remote Define a class that implements the Remote Interface extends java. rmi. Remote. Object or java. rmi. Unicast. Remote. Object

Remote Interface Example import java. rmi. *; public interface Adder extends Remote { public int add(int x, int y) throws Remote. Exception; } ECE 451: Introduction to Parallel 11/18/2003

Remote Class Example import java. rmi. *; import java. rmi. server. *; public class Adder. Impl extends Unicast. Remote. Object implements Adder { public Adder. Impl() throws Remote. Exception { } public int add(int x, int y) throws Remote. Exception { return x + y; } }

Inheritance Diagram in Java Object Remote. Stub Remote. Server Unicast Remote. Object

Compiling Remote Classes Compile the Java class javac reads . java file produces. class file Compile the Stub and Skeleton rmic reads . class file produces _Skel. class and _Stub. class

Compiling Remote Classes (Diagram) Adder. java (interface) javac Adder. Impl. java (remote class) javac Adder. class (interface classfile) Adder. Impl. class (classfile) Adder. Impl_Skel. class (skeleton classfile) rmic Adder. Impl_Stub. class (stub classfile) 11/18/2003 ECE 451: Introduction to Parallel and Distributed Programming

Registering Remote Classes start the registry running process Unix: rmiregistry & Windows: start /m rmiregistry

Registry CLASSPATH Registry VM needs to be able to find stub file(s) You must set the CLASSPATH to include the directory containing the stub file An easy way to check CLASSPATH is to use the javap command, supplying a fully package qualified class name. It uses the current CLASSPATH to find and print the interface to a class. Or, your server needs to specify the java. rmi. server. codebase System property (more later)

Create the server Creates a new instance of the remote object Registers it in the registry with a name That’s it 11/18/2003 ECE 451: Introduction to Parallel and Distributed Programming

RMI Server Example try { Adder. Impl adder = new Adder. Impl(); Naming. rebind("adder", adder); System. out. println("Adder bound"); } catch (Remote. Exception re) { re. print. Stack. Trace(); } catch (Malformed. URLException me) { me. print. Stack. Trace(); }

Launch the Server % java Adder. Server & Adder bound 11/18/2003 ECE 451: Introduction to Parallel and Distributed Programming

Server Logging invoke from command line java -Djava. rmi. server. log. Calls=true Your. Server. Impl or enable inside program Remote. Server. set. Log(System. err);

Creating an RMI Client Install to Find a Security Manager protect from malicious stubs a registry use java. rmi. Naming Lookup the name, returns a reference Cast the reference to the appropriate Remote Interface Just use it!
![RMI URLs rmi: //host[: port]/name default port is 1099 Specifies hostname of registry can RMI URLs rmi: //host[: port]/name default port is 1099 Specifies hostname of registry can](http://slidetodoc.com/presentation_image_h2/b5e37debdc81be29b089b31d6523cdab/image-39.jpg)
RMI URLs rmi: //host[: port]/name default port is 1099 Specifies hostname of registry can also use relative URLs name only assumes registry is on local host

RMI Client Example System. set. Security. Manager( new RMISecurity. Manager()); Adder a = (Adder) Naming. lookup("adder"); int sum = a. add(2, 2); System. out. println("2+2=" + sum);

Remote Interfaces vs. Remote Classes Remember that the reference is to an interface You must make references, arrays, etc. out of the interface type, not the implementation type You can’t cast the remote reference to a normal reference So name your Remote Objects with “Impl” (so you don’t get confused)

Parameter Passing All parameters are passed by value Primitive passed types by value Objects passed by value use Java Object Serialization

Object Serialization saves the state (data) of a particular instance of an object serialize - to save unserialize - to load

Java Serialization writes object as a sequence of bytes writes it to a Stream recreates it on the other end creates a brand new object with the old data

java. io. Serializable Objects that implement the java. io. Serializable interface are marked as serializable Also subclasses empty interface - just a marker – no need to implement any special methods

RMI Security Server is untrusted Stubs could be malicious rmic is OK, but someone could customcode an evil stub: it’s just a. class file

RMI Security Managers Applet. Security. Manager stub can only do what an applet can do RMISecurity. Manager disables all functions except class definition and access A downloaded class is allowed to make a connection if the connection was initiated via the RMI transport. None Stub loading disabled Stubs still work if they are in local classpath

Limitations of RMI Java-only but Uses you can use JNI on the server TCP, not UDP At least two sockets per connection Untested for huge loads

Sun vs. Microsoft RMI is not shipped as part of Microsoft’s products RMI will still work in applications include java. rmi. * class files in your classpath download rmi. zip from ftp. microsoft. com RMI will work in applets include java. rmi. * class files (or rmi. zip) in your codebase extra download time

RMI Chat Server Objects Message interface Message. Receiver - receive. Message(Message) Chat. Server - login(Message. Receiver) - send. Message(Message) Chat. Client implements remote reference local reference Chat. Server. Impl Dispatcher Message. Queue

Summary RMI is a very clean API Easy way to write distributed programs Wire protocol may need improvement for large-scale problems
- Slides: 51