15 - RMI/EJBCSC4071 Distributed Objects Java Remote Method Invocation Enterprise Java Beans.
JAVA RMI (Remote Method Invocation)
-
Upload
julian-rowe -
Category
Documents
-
view
70 -
download
1
description
Transcript of JAVA RMI (Remote Method Invocation)
JAVA RMI (Remote Method Invocation)
SNU IDB Lab.Dong-Hyuk Im
2007.05.07
2
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
3
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
4
What is RMI?
Supports for distributed object in java language
Allows object to invoke methods on remote objects using local invocation
Implementation of remote interface
object A object Bskeleton
Requestproxy for B
Reply
CommunicationRemote Communication module modulereference module
for B’s class& dispatcher
remote
Client Server
Remote reference module
5
Distributed Computing with RMI
Is relatively easy to use Supports communication between
different VMs, potentially across the network
Allows to develop distributed Java programs with the same syntax and semantics used for non-distributed programs
6
Principle of RMI
RMI separates: Definition of behaviour Implementation of that behaviour
Each of them is allowed to run on different JVMs
Interfaces: define definition Classes: define implementation
7
Principle of RMI
2 classes that implement the same interface Service implementation on server Service proxy on client
Client program makes method calls to proxy
RMI sends request to remote JVM Return values are sent back to proxy /
client program
8
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
9
RMI Architecture
The RMI architecture defines how objects behave how and when exceptions can occur how memory is managed how parameters are passed to and returned
from remote methods
10
RMI Architecture
3 abstract layers:
Advantages of layer architecture: Implementation of layers independent from each other Each layer can be enhanced / replaced without affecting
rest of the system
11
Stub & Skeleton Layer
SkeletonObject
Stub Object
Server Object
Returnvalue
Methodinvocationon client
Methodinvocationon server
Marshalledparameters
Marshalledreturn value
para
met
ers
12
Stub
Represents the remote service implementation in the client (is a proxy)
Marshalls parameters : Encoding parameters
Primitive Type (integer, Byte, … ) : copy by value Reference Type (String, Object, …) : object copy
Information block from stub to skeleton Remote object’s identifier Parameters / the ID of method
Unmarshalls return value or exception
13
Skeleton
Helper class on server Generated for RMI to use Communicates with stub across the link Reads parameters for the method call
from the link Makes the call to the service object Accepts the return value, writes it back to
the stub
14
Remote Reference Layer
Exists in both the RMI client and server Provides a constant interface to the stubs
and skeletons Manages communication between
stubs/skeleton Manages references to remote objects
Threading, garbage collection …
Manages reconnection strategies if an object should become unavailable
15
Transport Layer
Stream-based network connections that use TCP/IP
Deals with communications For interoperability, RMI may use the OMG
Internet Inter-ORB Protocol (IIOP)
16
RMI Layers
TCPRemote Reference Layer
Transport Layer
Java Virtual Machine
Client Object
Remote Reference Layer
Transport Layer
Java Virtual Machine
Stub
Remote Object
Skeleton
17
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
18
Naming Remote Objects
How does a client find an RMI remote service? Clients find remote services by using a naming
or directory service, running on a well known host and port number
RMI can use different directory services, e.g. the
Java Naming and Directory Service (JNDI) includes simple service called RMI Registry
(rmiregistry, default on port 1099)
19
RMI Flow (1/4)
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote Object
Skeleton
Registry Virtual Machine
“ Fred”
Server
20
RMI Flow (2/4)
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote Object
Skeleton
Registry Virtual Machine
“ Fred”
Server
1
2
1. Server Creates Remote Object2. Server Registers Remote Object
21
RMI Flow (3/4)
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote Object
Skeleton
Registry Virtual Machine
“ Fred”
Server
4
3. Client requests object from Registry4. Registry returns remote reference(and stub gets created)
3
22
RMI Flow (4/4)
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote Object
Skeleton
Registry Virtual Machine
“ Fred”
Server
6
5. Client invokes stub method6. Stub talks to skeleton7. Skeleton invokes remote object method
5 7
23
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
24
Creating Remote Object (1/2)
Define a Remote Interface extends java.rmi.Remote
interface Adder extends Remote{ public int add(int x, int y) throws RemoteException}
25
Creating Remote Object (2/2)
Define a class that implements the Remote Interface extends java.rmi.RemoteObject or java.rmi.UnicastRemoteObjectclass AdderImpl extends UnicastRemoteObject implements
Adder{ public AdderImpl() throws RemoteException { } public int add(int x, int y) throws RemoteException { return x + y; }}
26
Inheritance Diagram
Remote
UnicastRemoteObject
Adder
AdderImpl
RemoteObject
RemoteServer
Object
ImplementationExtension
27
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
28
Compiling Remote Classes (Diagram)
Adder.java(interface)
Adder.class(interface classfile)
javac
AdderImpl.java(remote class)
AdderImpl.class(classfile)
javacrmic
AdderImpl_Skel.class(skeleton classfile)
AdderImpl_Stub.class(stub classfile)
29
Registering Remote Classes
Start the registry running process
Unix: rmiregistry &
Windows: start /m rmiregistry
30
Registering Remote Classes
Remote object code in server
Remote reference code in client
// ServerAdderImpl a1 = new AdderImpl(“Add”);Naming.bind(“Add”, a1);
// ClientString url = “rmi://hostName/”;Adder a = (Adder) Naming.lookup(url + “Add”);
31
RMI Security
RMI Security Server is untrusted Stubs could be malicious
RMISecurityManager Protect from malicious stubs A downloaded class is allowed to make a
connection if the connection was initiated via the RMI transport// ClientSystem.setSecurityManager(
new RMISecurityManager());
32
RMI Client Example
// ClientSystem.setSecurityManager(
new RMISecurityManager());
String url = “rmi://hostName/”;Adder a = (Adder) Naming.lookup(url + “Add”);
int sum = a.add(2,2);System.out.println("2+2=" + sum);
33
RMI Program RMI “Adder” Adder.java
AdderImpl.java
AdderImpl.class
AdderImpl_Stub.class AdderImpl_Skel.class
AdderClient.java AdderServer.javarmic AdderImpl
implement
javac AdderImple.java
javac AdderClient.java Adder.java javac Adder.java AdderServer.java
Adder.class
AdderClient.class AdderServer.class
Adder.class
ClientServer
AdderImpl.class
generates
34
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
35
Parameters in RMI
Primitive types Passed by value
Remote objects Passed by reference : references to remote
object can be passed and returned from method calls
Non-remote objects Passed by value Uses Java Object Serialization :
java.io.Serializable
36
Distributed Garbage Collection
Reference counting The server keeps track of which clients have
requested When a reference is made, the server marks
the object as “dirty” When a client drops the reference, it is marked
as “clean” Each reference has a lease with a specified time
When the lease term expires : defaults to 10 minutes The reference is considered to be dead The remote object may be garbage collected
37
Distributed Garbage Collection
Process P2
Skeleton (maintains reference counter)
Proxy
Remote Object
Process P1
Proxy +1
+1
38
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
39
RMI : Benefits
Enables use of Design Patterns Use the full power of object oriented
technology in distributed computing (pass behavior and use OO design patterns)
Safe and Secure RMI uses built-in Java security mechanisms
Easy to Write/Easy to Use A remote interface is an actual Java interface
Distributed Garbage Collection Collects remote server objects that are no
longer referenced by any client in the network
40
Limitation of RMI
Tied only to platforms with Java support Can only operate with Java systems - no
support for legacy systems written in C++, Fortran, Cobol, and others future languages
Uses TCP, not UDP
41
RMI vs CORBA
Comparing RMI and CORBA doesn't reveal an optimum solution - one is not "better" than the other
CORBA Language-neutral Interoperability
RMI Specifically for Java Best for Java distributed object computing
42
Contents
Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference
43
References
“CORE JAVA”, Gary Cornell, Cay S. Horstmann
“JAVA RMI”, William Grosso RMI tutorial
http://java.sun.com/docs/books/tutorial/rmi/index.html
Java Remote Invocation (RMI) http://java.sun.com/javase/6/docs/technotes/g
uides/rmi/index.html