Transcript File

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 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
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
Java Virtual Machine
Client
Object
Remote
Object
Client
TCP
Server
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
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
Object
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
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
Server
2
“Fred”
Registry Virtual Machine
RMI Flow
Client Virtual Machine
Client
Server Virtual Machine
3. Client requests object from Registry
4. Registry returns remote reference
Skeleton
Stub
3
Server
4
“Fred”
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
method
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 the
Remote 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 Parallel
11/18/2003
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;
}
}
Inheritance Diagram in Java
Object
Remote
RemoteObject
RemoteStub
RemoteServer
Unicast
RemoteObject
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
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
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 {
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
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);
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 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. 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
 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
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
interface
MessageReceiver
ChatServer
- receiveMessage(Message)
- login(MessageReceiver)
- sendMessage(Message)
ChatClient
implements
remote reference
local reference
ChatServerImpl
Dispatcher
MessageQueue
Summary
 RMI
is a very clean API
 Easy way to write distributed programs
 Wire protocol may need improvement for
large-scale problems