Download presentation
Presentation is loading. Please wait.
Published byKenneth Stevenson Modified over 9 years ago
1
Java Remote Method Invocation (RMI) )
2
Distributed Systems a collection of independent computers that appears to its users as a single coherent system
3
Models of Distribution Message passing Distributed objects Event-based architectures Space-based paradigms
4
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
5
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.
6
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
7
Related Technologies RPC (“Remote Procedure Calls”) Developed by Sun Platform-specific CORBA (“Common Object Request Broker Architecture”) Developed by OMG Access to non-Java objects (as well as Java) DCOM (“Distributed Common Object Model”) Developed by Microsoft Access to Win32 objects LDAP (“Lightweight Directory Access Protocol”) Finding resources on a network
8
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.
9
Remote Objects (Diagram) Java Virtual Machine Client Object Client TCP Java Virtual Machine Remote Object Server
10
RMI Layers Java Virtual Machine Client Object Stub Remote Reference Layer Transport Layer 11/18/2003 TCP Java Virtual Machine Remote Object Skeleton Remote Reference Layer Transport Layer
11
RMI Architecture in the OSI model Application Layer Presentation Layer Session Layer Transport Layer Network Layer Data-link layer Physical Layer User Application StubSkeleton Remote Reference Layer TCP IP Hardware Interface Network
12
Remote Objects Remote Objects Live on server Java Virtual Machine Client Object Stub Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Object Skeleton Remote Reference Layer TCP Transport Layer Server Accessed as if they were local
13
Stubs and Skeletons Stub lives on client Java Virtual Machine Client Object Stub Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Object Skeleton Remote Reference Layer TCP Transport Layer Server pretends to be remote object - a proxy for the remote object Skeleton 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”
14
Stubs and Skeletons Stub – responsibilities Initiate remote calls Java Virtual Machine Client Object Stub Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Object Skeleton Remote Reference Layer TCP Transport Layer Server 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
15
Stubs and Skeletons Skeleton – responsibilities Java Virtual Machine Client Object Stub Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Object Skeleton Remote Reference Layer TCP Transport Layer Server 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
16
Remote Interfaces and Stubs Remote Interface implements Remote Object ClientStubSkeleton (Server) 11/18/2003
17
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
18
Remote Reference Layer Java Virtual Machine Client Object Stub Remote Reference Layer Transport Layer Client Java Virtual Machine Remote Object Skeleton Remote Reference Layer TCP Transport Layer Server Sets up connections to remote address spaces Manages connections Listens for incoming calls Communicates via TCP/IP
19
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
20
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
21
RMI System Architecture Client Virtual Machine Client Stub Client “Fred” Server Virtual Machine Remote Object Skeleton Server Server Registry Registry Virtual Machine
22
RMI Flow 1. Server Creates Remote Object 2. Server Registers Remote Object Client Stub “Fred” Server Virtual Machine Remote Object 1 Skeleton Server 2 Registry Virtual Machine
23
RMI Flow Client Virtual Machine Client Stub 3434 Server Virtual Machine 3. Client requests object from Registry 4. Registry returns remote reference Skeleton Server “Fred” Registry Virtual Machine
24
RMI Flow Client Virtual Machine Client 5 Stub 6 Server Virtual Machine Remote Object 7 Skeleton Server 5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object method Registry Virtual Machine
25
Part II: RMI Usage 11/18/2003and Distributed Programming
26
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
27
Remote Interface Example import java.rmi.*; public interface Adder extends Remote { public int add(int x, int y) throws RemoteException; } ECE 451:Introduction to Parallel 11/18/2003
28
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; } }
29
Inheritance Diagram in Java ObjectRemote RemoteObject RemoteStubRemoteServer Unicast RemoteObject
30
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
31
Compiling Remote Classes (Diagram) Adder.java (interface) javac Adder.class (interface classfile) javac AdderImpl_Skel.class (skeleton classfile) AdderImpl.java (remote class) AdderImpl.class (classfile) rmic AdderImpl_Stub.class (stub classfile) ECE 451:Introduction to Parallel 11/18/2003and Distributed Programming
32
Registering Remote Classes start the registry running process Unix: rmiregistry & Windows: start /m rmiregistry
33
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)
34
Create the server Creates a new instance of the remote object Registers it in the registry with a name That’s it ECE 451:Introduction to Parallel 11/18/2003and Distributed Programming
35
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(); }
36
Launch the Server % java AdderServer & Adder bound ECE 451:Introduction to Parallel 11/18/2003and Distributed Programming
37
Server Logging invoke from command line java -Djava.rmi.server.logCalls=true YourServerImpl or enable inside program RemoteServer.setLog(System.err);
38
Creating an RMI Client Install a Security Manager to protect from malicious stubs Find a registry use java.rmi.Naming Lookup the name, returns a reference Cast the reference to the appropriate Remote Interface Just use it!
39
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
40
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);
41
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)
42
Parameter Passing All parameters are passed by value Primitive types passed by value Objects passed by value use Java Object Serialization
43
Object Serialization saves the state (data) of a particular instance of an object serialize - to save unserialize - to load
44
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
45
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
46
RMI Security Server is untrusted Stubs could be malicious rmic is OK, but someone could custom- code an evil stub: it’s just a.class file
47
RMI Security Managers AppletSecurityManager stub can only do what an applet can do RMISecurityManager 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
48
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
49
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
50
RMI Chat Server Objects Message interface MessageReceiver - receiveMessage(Message) ChatClient implements interface ChatServer - login(MessageReceiver) - sendMessage(Message) ChatServerImpl Dispatcher remote reference MessageQueue local reference
51
Summary RMI is a very clean API Easy way to write distributed programs Wire protocol may need improvement for large-scale problems
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.