Remote Method Invocation (RMI) Basado en: Fundamentals of RMI. Short Course. JGuru.
Java Remote Method Invocation (RMI) ). Distributed Systems a collection of independent computers...
-
Upload
kenneth-stevenson -
Category
Documents
-
view
219 -
download
1
Transcript of Java Remote Method Invocation (RMI) ). Distributed Systems a collection of independent computers...
Java Remote MethodInvocation (RMI)
)
Distributed Systems
� a collection of independent computers thatappears to its users as a single coherentsystem
Models of Distribution
� Message passing� Distributed objects
� Event-based architectures� Space-based paradigms
Distributed Object Model
� Views a distributed system as a series ofinteracting objects
� Based on some underlying messagepassing protocol invisible to theprogrammer
� Three main technologies: RMI, CORBAand DCOM
Distributed Object Computing
� Enable any object in the local system to directly interactwith 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 asthey 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 ofa method that exists in another addressspace
� Java-to-Java only� Client-Server Protocol� High-level API
� Transparent� Lightweight
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
RMI
� Client – the process that invokes a method on aremote object
� Server – the process that owns the remoteobject
� Object Registry – a name server that relatesobjects with names� Objects are registered with the Object Registry, undera unique name.
� The Object Registry is used to obtain access toremote objects, using their names.
Remote Objects (Diagram)Java Virtual Machine
ClientObject
Client
TCP
Java Virtual Machine
RemoteObject
Server
RMI LayersJava Virtual Machine
ClientObject
Stub
Remote Reference Layer
Transport Layer
11/18/2003
TCP
Java Virtual Machine
RemoteObject
Skeleton
Remote Reference Layer
Transport Layer
RMI Architecture in the OSI modelApplication Layer
Presentation Layer
Session Layer
Transport Layer
Network Layer
Data-link layerPhysical Layer
User Application
Stub Skeleton
Remote Reference Layer
TCP
IP
Hardware Interface
Network
Remote Objects
� Remote Objects� Live on server
Java Virtual MachineClientObject
Stub
Remote Reference Layer
Transport Layer
Client
Java Virtual MachineRemoteObject
Skeleton
Remote Reference LayerTCPTransport Layer
Server
� Accessed as if they were local
Stubs and Skeletons
� Stub� lives on client
Java Virtual MachineClientObject
Stub
Remote Reference Layer
Transport Layer
Client
Java Virtual MachineRemoteObject
Skeleton
Remote Reference LayerTCPTransport 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 anddelivers response to stub
� Stubs and skeletons are not written by the programmer!They are generated by a special compiler “rmic”
Stubs and Skeletons
� Stub – responsibilities� Initiate remote calls
Java Virtual MachineClientObject
Stub
Remote Reference Layer
Transport Layer
Client
Java Virtual MachineRemoteObject
Skeleton
Remote Reference LayerTCPTransport Layer
Server
� Marshals arguments to be sent
� Informs the remote reference layer that a callshould be invoked on the server
� Unmarshals a return value (or exception)
� Informs the remote reference layer that thecall is complete
Stubs and Skeletons
� Skeleton – responsibilities
Java Virtual MachineClientObject
Stub
Remote Reference Layer
Transport Layer
Client
Java Virtual MachineRemoteObject
Skeleton
Remote Reference LayerTCPTransport Layer
Server
� Unmarshals incoming arguments� Calls the actual remote object implementation� Marshals return values for transport to theclient
� Marshaling – definition� The process of converting native
programming language data types to a formatsuitable for transmission across a network
Remote Interfaces and Stubs
Remote Interface
implements implements
Remote ObjectClient Stub Skeleton(Server)
11/18/2003
Remote Interfaces
� Declare exposed methods – the methodsthat can be called from remote locations
� Extend java.rmi.Remote� The remote object implements thisinterface
� Act like a proxy for the remote object
Remote Reference Layer
Java Virtual MachineClientObject
Stub
Remote Reference Layer
Transport Layer
Client
Java Virtual MachineRemoteObject
Skeleton
Remote Reference LayerTCPTransport Layer
Server
� Sets up connections to remote addressspaces
� Manages connections� Listens for incoming calls� Communicates via TCP/IP
Object Registries
� Name and look up remote objects� Remote objects register by name
� Clients obtain a remote reference to theremote object� A registry is a running process on thesame host as the RMI server
HTTP Tunneling
� Cool: if it can’t make the connectionnormally, it will tunnel through port 80� Allows clients behind firewall to makeremote calls to server
� Note: does not work server -> client
RMI System ArchitectureClient Virtual Machine
Client
Stub
Client
“Fred”
Server Virtual Machine
RemoteObject
SkeletonServer
Server
Registry
Registry Virtual Machine
RMI Flow1. Server Creates Remote Object
2. Server Registers Remote ObjectClient
Stub
“Fred”
Server Virtual Machine
RemoteObject
1
SkeletonServer
2
Registry Virtual Machine
RMI FlowClient Virtual Machine
Client
Stub
3 4
Server Virtual Machine
3. Client requests object from Registry4. Registry returns remote reference
SkeletonServer
“Fred”
Registry Virtual Machine
RMI FlowClient Virtual Machine
Client
5
Stub6
Server Virtual Machine
RemoteObject
7
SkeletonServer
5. Client invokes stub method6. Stub talks to skeleton
7. Skeleton invokes remote objectmethod
Registry Virtual Machine
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 theRemote Interface
� extends java.rmi.RemoteObject� or java.rmi.UnicastRemoteObject
Remote Interface Example
import java.rmi.*;
public interface Adder
extends Remote
{
public int add(int x, int y)
throws RemoteException;
}
ECE 451:Introduction to Parallel11/18/2003
Remote Class Exampleimport 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;}
}
Inheritance Diagram in JavaObject Remote
RemoteObject
RemoteStub RemoteServer
UnicastRemoteObject
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)
javacAdder.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 Parallel11/18/2003 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 thedirectory containing the stub file
� An easy way to check CLASSPATH is to use the javap command, supplyinga 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 thejava.rmi.server.codebase System property (morelater)
Create the server
� Creates a new instance of the remoteobject
� Registers it in the registry with a name� That’s it
ECE 451:Introduction to Parallel11/18/2003 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();
}
Launch the Server
% java AdderServer &
Adder bound
ECE 451:Introduction to Parallel11/18/2003 and Distributed Programming
Server Logging
� invoke from command linejava
-Djava.rmi.server.logCalls=trueYourServerImpl
� or enable inside programRemoteServer.setLog(System.err);
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 appropriateRemote Interface
� Just use it!
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.setSecurityManager(
new RMISecurityManager());
Adder a = (Adder)Naming.lookup("adder");
int sum = a.add(2,2);
System.out.println("2+2=" + sum);
Remote Interfaces vs. RemoteClasses
� Remember that the reference is to an interface� You must make references, arrays, etc. out ofthe interface type, not the implementation type� You can’t cast the remote reference to a normalreference
� So name your Remote Objects with “Impl” (soyou don’t get confused)
Parameter Passing� All parameters are passed by value
� Primitive types� passed by
value
� Objects� passed by value
� use Java Object Serialization
Object Serialization
� saves the state (data) of a particularinstance 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 olddata
java.io.Serializable
� Objects that implement thejava.io.Serializable interface are marked asserializable
� Also subclasses� empty interface - just a marker – no needto implement any special methods
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
RMI Security Managers� AppletSecurityManager
� stub can only do what an applet can
do
� RMISecurityManager� disables all functions except class definition andaccess
� A downloaded class is allowed to make a connectionif the connection was initiated via the RMItransport.
� None� Stub loading disabled
� Stubs still work if they are in local classpath
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
Sun vs. Microsoft
� RMI is not shipped as part of Microsoft’sproducts
� 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 yourcodebase
� extra download time
RMI Chat Server ObjectsMessage
interface
MessageReceiver- receiveMessage(Message)
ChatClient
implements
interface
ChatServer- login(MessageReceiver)- sendMessage(Message)
ChatServerImpl
Dispatcherremote reference MessageQueuelocal reference
Summary
� RMI is a very clean API
� Easy way to write distributed programs� Wire protocol may need improvement forlarge-scale problems