Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Remote Method Invocation (RMI) Viraj Bhat ([email protected]) Cristina Schmidt ([email protected]) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Distributed Systems a collection of independent computers that appears to its users as a single coherent system 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Models of Distribution Message passing Distributed objects Event-based architectures Space-based paradigms 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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. 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Examples of Use Database access Computations Any custom protocol Not for standard protocols (HTTP, FTP, etc.) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Related Technologies RPC (“Distributed Common Object Model”) Developed by Microsoft Access to Win32 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Part I: RMI Concepts 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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. 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Remote Objects (Diagram) Java Virtual Machine Java Virtual Machine Client Object Remote Object TCP Client 11/18/2003 Server ECE 451:Introduction to Parallel and Distributed Programming RMI Layers Java Virtual Machine Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Remote Reference Layer Transport Layer 11/18/2003 TCP ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Java Virtual Machine Remote Objects Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer Client Remote Objects Live on server Accessed as if they were local 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Remote Reference Layer TCP Transport Layer Server Java Virtual Machine Stubs and Skeletons Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer Remote Reference Layer TCP 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” 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Java Virtual Machine Stubs and Skeletons Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Reference Layer TCP Transport Layer Server Stub – 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Java Virtual Machine Stubs and Skeletons Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Reference Layer TCP Transport Layer Server Skeleton – responsibilities Unmarshals incoming arguments Calls the actual remote object implementation Marshals return values for transport to the client Marshaling – definition The process of converting native programming language data types to a format suitable for transmission across a network 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Remote Interfaces and Stubs Remote Interface implements Client 11/18/2003 Stub implements Skeleton ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Java Virtual Machine Remote Reference Layer Java Virtual Machine Client Object Remote Object Stub Skeleton Remote Reference Layer Transport Layer Remote Reference Layer TCP Client Sets up connections to remote address spaces Manages connections Listens for incoming calls Communicates via TCP/IP 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI System Architecture Client Virtual Machine Server Virtual Machine Client Remote Object Skeleton Stub Server Server Client “Fred” 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Registry Virtual Machine Registry RMI Flow 1. Server Creates Remote Object Client Virtual Machine 2. Server Registers Remote Object Client Server Virtual Machine Remote Object 1 Skeleton Stub Server 2 “Fred” 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Registry Virtual Machine RMI Flow Client Virtual Machine Client Server Virtual Machine Remote 3. Client requests object from Registry Object 4. Registry returns remote reference Skeleton Stub 3 Server 4 “Fred” 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Registry Virtual Machine RMI Flow Client Virtual Machine Server Virtual Machine Client Remote Object 5 7 6 Stub Skeleton Server 5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object “Fred” method 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Registry Virtual Machine Part II: RMI Usage 11/18/2003 ECE 451:Introduction to Parallel 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.RemoteObject or java.rmi.UnicastRemoteObject 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Remote Interface Example import java.rmi.*; public interface Adder extends Remote { public int add(int x, int y) throws RemoteException; } 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Remote Class Example import java.rmi.*; import java.rmi.server.*; public class AdderImpl extends UnicastRemoteObject implements Adder { public AdderImpl() throws RemoteException { } public int add(int x, int y) throws RemoteException { return x + y; } } 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Inheritance Diagram in Java Object Remote RemoteObject RemoteStub RemoteServer Unicast RemoteObject 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Compiling Remote Classes (Diagram) Adder.java (interface) javac AdderImpl.java (remote class) javac Adder.class (interface classfile) AdderImpl.class (classfile) AdderImpl_Skel.class (skeleton classfile) rmic AdderImpl_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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 { AdderImpl adder = new AdderImpl(); Naming.rebind("adder", adder); System.out.println("Adder bound"); } catch (RemoteException re) { re.printStackTrace(); } catch (MalformedURLException me) { me.printStackTrace(); } 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Launch the Server % java AdderServer & Adder bound 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Server Logging invoke from command line java -Djava.rmi.server.logCalls=true YourServerImpl or enable inside program RemoteServer.setLog(System.err); 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Creating an RMI Client Install a Security Manager Find a registry to protect from malicious stubs use java.rmi.Naming Lookup the name, returns a reference Cast the reference to the appropriate Remote Interface Just use it! 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client Example System.setSecurityManager( new RMISecurityManager()); Adder a = (Adder) Naming.lookup("adder"); int sum = a.add(2,2); System.out.println("2+2=" + sum); 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Parameter Passing All parameters are passed by value Primitive types passed by value Objects passed by value use Java Object Serialization 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Object Serialization saves the state (data) of a particular instance of an object serialize - to save unserialize - to load 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Not All Objects Are Serializable Any object that doesn’t implement Serializable Any object that would pose a security risk Any object whose value depends on VMspecific information e.g. FileInputStream e.g. Thread Any object that contains a (non-static, non-transient) unserializable object (recursively) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming NotSerializableException thrown if you try to serialize or unserialize an unserializable object maybe you subclassed a Serializable object and added some unserializable members 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Incompatible Changes If class has members added or removed, it becomes incompatible java.io.InvalidClassException thrown if you try to deserialize an incompatible object stream 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Serial Version If the changes were actually compatible find out the Serial Version UID of the original class use the serialver utility add a member variable to the changed class protected static final long serialVersionUID = 2215190743590612933L; now it’s marked as compatible with the old class 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Using readObject if you need to force an object to be compatible implement readObject() method to make compatible changes private void readObject(ObjectInputStream stream) throws java.io.IOException { defaultReadObject(stream); // do compatible stuff } 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Callbacks The server calls a remote method on the client => the server becomes the client, and the client is now the server The client registers a notification method with the server Server invokes the method (“calls back”) to notify the client Registry is out of the loop 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Security Managers AppletSecurityManager RMISecurityManager stub can only do what an applet can do 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 11/18/2003 Stub loading disabled Stubs still work if they are in local classpath ECE 451:Introduction to Parallel and Distributed Programming Codebase Property Stub classpaths can be confusing The RMI class loader always loads stubs from the CLASSPATH first Next, it tries downloading classes from a web server 3 VMs, each with its own classpath Server vs. Registry vs. Client (but only if a security manager is in force) java.rmi.server.codebase specifies which web server 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Stub File Configuration The best way to configure it is as follows. NEVER have stub class files in ANY classpath make the stub files accessible via a web server set the java.rmi.server.codebase property, in the application creating the server object, to the web server's URL The stubs will be downloaded from your HTTP server on demand (Thanks to [email protected] for advice) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Codebase and Thread Safety There’s a thread problem with codebase With a single remote server, and multiple remote objects on that server, each with its own stub codebase One server can step on the codebase property and foul up the registration for the other server In practice, won’t happen very often More likely to happen in servlets 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Limitations of RMI Java-only but you can use JNI on the server Uses TCP, not UDP At least two sockets per connection Untested for huge loads 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI vs. COM Very similar remote interfaces ~ type libraries COM is Win32-only (for now) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming 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 11/18/2003 include java.rmi.* class files (or rmi.zip) in your codebase extra download time ECE 451:Introduction to Parallel and Distributed Programming RMI Chat Server Objects Message interface interface MessageReceiver ChatServer - receiveMessage(Message) - login(MessageReceiver) - sendMessage(Message) ChatClient implements remote reference local reference 11/18/2003 ChatServerImpl Dispatcher ECE 451:Introduction to Parallel and Distributed Programming MessageQueue Summary RMI is a very clean API Easy way to write distributed programs Wire protocol may need improvement for large-scale problems 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming Where to get more information Core Java Volume II by Cay S Horstmann and Gray Cornell Harold, Java Network Programming (O’Reilly) rmi-users mailing list ([email protected]) http://www.developer.com/ (Gamelan) http://www.javaworld.com/ (magazine) http://www.stinky.com/java/ (original Author’s site) 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming My sincere thanks Alex Chaffee’s Purple Technology and his RMI slides which I have used in my Presentation 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming