Java Remote Method Java Remote Method Invocation (RMI) Invocation (RMI)

squawkpsychoticSoftware and s/w Development

Dec 2, 2013 (3 years and 8 months ago)

76 views

11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Java Remote Method
Java Remote Method
Invocation (RMI)
Invocation (RMI)
Viraj Bhat
Viraj Bhat
(
(
virajb@caip.rutgers.edu
virajb@caip.rutgers.edu
)
)
Cristina Schmidt
Cristina Schmidt
(cristins@caip.rutgerts.edu)
(cristins@caip.rutgerts.edu)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Distributed Systems
Distributed Systems


a collection of independent computers that
a collection of independent computers that
appears to its users as a single coherent
appears to its users as a single coherent
system
system
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Models of Distribution
Models of Distribution


Message passing
Message passing


Distributed objects
Distributed objects


Event
Event
-
-
based architectures
based architectures


Space
Space
-
-
based paradigms
based paradigms
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Distributed Object Model
Distributed Object Model


Views a distributed system as a series of
Views a distributed system as a series of
interacting objects
interacting objects


Based on some underlying message
Based on some underlying message
passing protocol invisible to the
passing protocol invisible to the
programmer
programmer


Three main technologies:
Three main technologies:
RMI
RMI
, CORBA
, CORBA
and DCOM
and DCOM
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Distributed Object Computing
Distributed Object Computing


Enable any object in the local system to directly interact
Enable any object in the local system to directly interact
with an object on a remote host
with an object on a remote host


Goals:
Goals:


Let any object reside anywhere in the network, and allow an
Let any object reside anywhere in the network, and allow an
application to interact with these objects in the same way as
application to interact with these objects in the same way as
they do with a local object.
they do with a local object.


Provide the ability to construct an object on one host and
Provide the ability to construct an object on one host and
transmit it to another host.
transmit it to another host.


Enable an agent on one host to create an object on another
Enable an agent on one host to
create an object on another
host.
host.
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
What Is RMI?
What Is RMI?


A mechanism that allows the invocation of
A mechanism that allows the invocation of
a method that exists in another address
a method that exists in another address
space
space


Java
Java
-
-
to
to
-
-
Java
Java
only
only


Client
Client
-
-
Server Protocol
Server Protocol


High
High
-
-
level API
level API


Transparent
Transparent


Lightweight
Lightweight
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Examples of Use
Examples of Use


Database access
Database access


Computations
Computations


Any custom protocol
Any custom protocol


Not
Not
for standard protocols (HTTP, FTP,
for standard protocols (HTTP, FTP,
etc.)
etc.)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Related Technologies
Related Technologies


RPC
RPC
(“Remote Procedure Calls”)
(“Remote Proc
edure Calls”)


Developed by Sun
Developed by Sun


Platform
Platform
-
-
specific
specific


CORBA
CORBA
(“Common Object Request Broker Architecture”)
(“Common Object Request Broker Architecture”)


Developed by OMG
Developed by OMG


Access to non
Access to non
-
-
Java objects (as well as Java)
Java objects (as well as Java)


DCOM
DCOM
(“Distributed Common Object Model”)
(“Distributed Common Object Model”)


Developed by Microsoft
Developed by Microsoft


Access to Win32 objects
Access to Win32 objects


LDAP
LDAP
(“Lightweight Directory Access Protocol”)
(“Lightweight Directory Access Protocol”)


Finding resources on a network
Finding resources on a network
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Part I: RMI Concepts
Part I: RMI Concepts
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI
RMI


Client
Client


the process that invokes a method on a
the process that invokes a method on a
remote object
remote object


Server
Server


the process that owns the remote
the process that owns the remote
object
object


Object Registry
Object Registry


a name server that relates
a name server that relates
objects with names
objects with names


Objects are registered with the Object Registry, under
Objects are registered with the Object Registry, under
a unique name.
a unique name.


The Object Registry is used to obtain access to
The Object Registry is used to obtain access to
remote objects, using their names.
remote objects, using their names.
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Objects (Diagram)
Remote Objects (Diagram)
TCP
Java Virtual Machine
Client
Object
Java Virtual Machine
Remote
Object
Client
Server
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Layers
RMI Layers
TCP
Remote Reference Layer
Transport Layer
Java Virtual Machine
Client
Object
Remote Reference Layer
Transport Layer
Java Virtual Machine
Stub
Remote
Object
Skeleton
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Architecture in the OSI model
RMI Architecture in the OSI model
User Application
Stub
Skeleton
Remote Reference Layer
TCP
IP
Hardware Interface
Network
Application Layer
Presentation Layer
Session Layer
Transport Layer
Network Layer
Data-link layer
Physical Layer
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
TCP
Rem
ote Reference
Layer
Transport Layer
Java Virtual
Machine
Client
Object
Remote Reference Layer
Transport Layer
Java Vi
rtual Machine
Stub
Remote
Object
Skeleton
Client
Server
Remote Objects
Remote Objects


Remote Objects
Remote Objects


Live on server
Live on server


Accessed as if they were local
Accessed as if they were local
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Stubs and Skeletons
Stubs and Skeletons
TCP
Remote Reference Layer
Transport Layer
Java Virt
ual Machine
Client
Object
Rem
ote Reference
Layer
Transport Layer
Java Virtual Machine
Stub
Remote
Object
Skeleton
Client
Server


Stub
Stub


lives on client
lives on client


pretends to be remote object
pretends to be remote object
-
-
a proxy for the remote object
a proxy for the remote object


Skeleton
Skeleton


lives on server
lives on server


receives requests from stub, talks to the remote object and
receives requests from stub, talks to the remote object and
delivers response to stub
delivers response to stub


Stubs and skeletons are not written by the programmer!
Stubs and skeletons are not written by the programmer!
They are generated by a special compiler “rmic”
They are generated by a special compiler “rmic”
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Stubs and Skeletons
Stubs and Skeletons
TCP
Remote Reference Layer
Transport Layer
Java Virt
ual Machine
Client
Object
Rem
ote Reference
Layer
Transport Layer
Java Virtual Machine
Stub
Remote
Object
Skeleton
Client
Server


Stub
Stub


responsibilities
responsibilities


Initiate remote calls
Initiate remote calls


Marshals arguments to be sent
Marshals arguments to be sent


Informs the remote reference layer that a call
Informs the remote reference layer that a call
should be invoked on the server
should be invoked on the server


Unmarshals a return value (or exception)
Unmarshals a return value (or exception)


Informs the remote reference layer that the
Informs the remote reference layer that the
call is complete
call is complete
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Stubs and Skeletons
Stubs and Skeletons
TCP
Remote Reference Layer
Transport Layer
Java Virt
ual Machine
Client
Object
Rem
ote Reference
Layer
Transport Layer
Java Virtual Machine
Stub
Remote
Object
Skeleton
Client
Server


Skeleton
Skeleton


responsibilities
responsibilities


Unmarshals incoming arguments
Unmarshals incoming arguments


Calls the actual remote object implementation
Calls the actual remote object implementation


Marshals return values for transport to the
Marshals return values for transport to the
client
client


Marshaling
Marshaling


definition
definition


The process of converting native
The process of converting native
programming language data types to a format
programming language data types to a format
suitable for transmission across a network
suitable for transmission across a network
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Interfaces and Stubs
Remote Interfaces and Stubs
Remote Interface
Stub
Remote Object
(Server)
Client
Skeleton
implements
implements
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Interfaces
Remote Interfaces


Declare exposed methods
Declare exposed methods


the methods
the methods
that can be called from remote locations
that can be called from remote locations


Extend
Extend
java.rmi.Remote
java.rmi.Remote


The remote object implements this
The remote object implements this
interface
interface


Act like a proxy for the remote object
Act like a proxy for the remote object
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Reference Layer
Remote Reference Layer
TCP
Rem
ote Reference
Layer
Transport Layer
Java Virtual
Machine
Client
Object
Remote Reference Layer
Transport Layer
Java Vi
rtual Machine
Stub
Remote
Object
Skeleton
Client
Server


Sets up connections to remote address
Sets up connections to remote address
spaces
spaces


Manages connections
Manages connections


Listens for incoming calls
Listens for incoming calls


Communicates via TCP/IP
Communicates via TCP/IP
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Object Registries
Object Registries


Name and look up remote objects
Name and look up remote objects


Remote objects register by name
Remote objects register by name


Clients obtain a remote reference to the
Clients obtain a remote reference to the
remote object
remote object


A registry is a
A registry is a
running process
running process
on the
on the
same host as the RMI server
same host as the RMI server
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
HTTP Tunneling
HTTP Tunneling


Cool: if it can’t make the connection
Cool: if it can’t make the connection
normally, it will tunnel through port 80
normally, it will tunnel through port 80


Allows clients behind firewall to make
Allows clients behind firewall to make
remote calls to server
remote calls to server


Note: does not work server
Note: does not work server
-
-
> client
> client
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI System Architecture
RMI System Architecture
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote
Object
Skeleton
Registry Virtual Machine
“Fred”
Server
Server
Client
Registry
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Flow
RMI Flow
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote
Object
Skeleton
Registry Virtual Machine
“Fred”
Server
1
2
1. Server Creates Remote Object
2. Server Registers Remote Object
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Flow
RMI Flow
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote
Object
Skeleton
Registry Virtual Machine
“Fred”
Server
4
3. Client requests object from Registry
4. Registry returns remote reference
3
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Flow
RMI Flow
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote
Object
Skeleton
Registry Virtual Machine
“Fred”
Server
6
5. Client invokes stub method
6. Stub talks to skeleton
7. Skeleton invokes remote object
method
5
7
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Part II: RMI Usage
Part II: RMI Usage
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Creating Remote Objects
Creating Remote Objects


Define a Remote Interface
Define a Remote Interface


extends java.rmi.Remote
extends java.rmi.Remote


Define a class that implements the
Define a class that implements the
Remote Interface
Remote Interface


extends java.rmi.RemoteObject
extends java.rmi.RemoteObject


or java.rmi.UnicastRemoteObject
or java.rmi.UnicastRemoteObject
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Interface Example
Remote Interface Example
import java.rmi.*;
import java.rmi.*;
public interface Adder
public interface Adder
extends Remote
extends Remote
{
{
public int add(int x, int y)
public int add(int x, int y)
throws RemoteException;
throws RemoteException;
}
}
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Class Example
Remote Class Example
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.server.*;
public class AdderImpl
public class AdderImpl
extends UnicastRemoteObject
extends UnicastRemoteObject
implements Adder
implements Adder
{
{
public AdderImpl() throws RemoteException
public AdderImpl() throws RemoteException
{
{
}
}
public int add(int x, int y)
public int add(int x, int y)
throws RemoteException
throws RemoteException
{
{
return x + y;
return x + y;
}
}
}
}
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Inheritance Diagram in Java
Inheritance Diagram in Java
Object
Unicast
RemoteObject
RemoteObject
RemoteServer
RemoteSt
ub
Remote
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Compiling Remote Classes
Compiling Remote Classes


Compile the Java class
Compile the Java class


javac
javac


reads .java file
reads .java file


produces .class file
produces .class file


Compile the Stub and Skeleton
Compile the Stub and Skeleton


rmic
rmic


reads .class file
reads .class file


produces _Skel.class and _Stub.class
produces _Skel.class and _Stub.class
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Compiling Remote Classes
Compiling Remote Classes
(Diagram)
(Diagram)
Adder.java
(interface)
Adder.class
(interface classfile)
javac
AdderImpl.java
(remote class)
AdderImpl.class
(classfile)
javac
rmic
AdderImpl_Skel.class
(skeleton classfile)
AdderImpl_Stub.class
(stub classfile)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Registering Remote Classes
Registering Remote Classes


start the registry
start the registry


running process
running process


Unix:
Unix:
rmiregistry &
rmiregistry &


Windows:
Windows:
start /m rmiregistry
start /m rmiregistry
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Registry CLASSPATH
Registry CLASSPATH


Registry VM needs to be able to find stub file(s)
Registry VM needs to be able to find stub file(s)


You must set the CLASSPATH to include the
You must set the CLASSPATH to include the
directory containing the stub file
directory containing the stub file


An easy way to check CLASSPATH is to use the
An easy way to check CLASSPATH is to use the
javap
javap
command, supplying
command, supplying
a fully package qualified class name. It uses the current CLASSP
a fully package qualified class name. It uses the c
urrent CLASSP
ATH to find
ATH to find
and print the interface to a class.
and print the interface to a class.


Or, your server needs to specify the
Or, your server needs to specify the
java.rmi.server.codebase System property (more
java.rmi.server.codebase System property (more
later)
later)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Create the server
Create the server


Creates a new instance of the remote
Creates a new instance of the remote
object
object


Registers it in the registry with a name
Registers it in the registry with a name


That’s it
That’s it
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Server Example
RMI Server Example
try {
try {
AdderImpl adder =
AdderImpl adder =
new
new
AdderImpl();
AdderImpl();
Naming.
Naming.
rebind
rebind
("adder", adder);
("adder", adder);
System.out.println("Adder bound");
System.out.println("Adder bound");
}
}
catch (RemoteException re) {
catch (RemoteException re) {
re.printStackTrace();
re.printStackTrace();
}
}
catch (MalformedURLException me) {
catch (MalformedURLException me) {
me.printStackTrace();
me.printStackTrace();
}
}
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Launch the Server
Launch the Server
% java AdderServer &
% java AdderServer &
Adder bound
Adder bound
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Server Logging
Server Logging


invoke from command line
invoke from command line
java
java
-
-
Djava.rmi.server.logCalls=true
Djava.rmi.server.logCalls=true
YourServerImpl
YourServerImpl


or enable inside program
or enable inside program
RemoteServer.setLog(System.err);
RemoteServer.setLog(System.err);
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Creating an RMI Client
Creating an RMI Client


Install a Security Manager
Install a Security Manager


to protect from malicious stubs
to protect from malicious stubs


Find a registry
Find a registry


use java.rmi.Naming
use java.rmi.Naming


Lookup the name, returns a reference
Lookup the name, returns a reference


Cast the reference to the appropriate
Cast the reference to the appropriate
Remote Interface
Remote Interface


Just use it!
Just use it!
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI URLs
RMI URLs
rmi://
rmi://
host
host
[:
[:
port
port
]/
]/
name
name


default port is 1099
default port is 1099


Specifies hostname of
Specifies hostname of
registry
registry


can also use relative URLs
can also use relative URLs


name only
name only


assumes registry is on local host
assumes registry is on local host
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Client Example
RMI Client Example
System.setSecurityManager(
System.setSecurityManager(
new RMISecurityManager());
new RMISecurityManager());
Adder a = (Adder)
Adder a = (Adder)
Naming.
Naming.
lookup
lookup
("adder");
("adder");
int sum = a.add(2,2);
int sum = a.add(2,2);
System.out.println("2+2=" + sum);
System.out.println("2+2=" + sum);
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Remote Interfaces vs. Remote
Remote Interfaces vs. Remote
Classes
Classes


Remember that the reference is to an
Remember that the reference is to an
interface
interface


You must make references, arrays, etc. out of
You must make references, arrays, etc. out of
the
the
interface
interface
type, not the implementation type
type, not the implementation type


You can’t cast the remote reference to a normal
You can’t cast the remote reference to a normal
reference
reference


So name your Remote Objects with “
So name your Remote Objects with “
Impl
Impl
” (so
” (so
you don’t get confused)
you don’t get confused)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Parameter Passing
Parameter Passing


All parameters are passed by value
All parameters are passed by value


Primitive types
Primitive types


passed by value
passed by value


Objects
Objects


passed by value
passed by value


use Java Object Serialization
use Java Object Serialization
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Object Serialization
Object Serialization


saves the state (data) of a particular
saves the state (data) of a particular
instance of an object
instance of an object


serialize
serialize
-
-
to save
to save


unserialize
unserialize
-
-
to load
to load
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Java Serialization
Java Serialization


writes object as a sequence of bytes
writes object as a sequence of bytes


writes it to a Stream
writes it to a Stream


recreates it on the other end
recreates it on the other end


creates a brand new object with the old
creates a brand new object with the old
data
data
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
java.io.Serializable
java.io.Serializable


Objects that implement the
Objects that implement the
java.io.Serializable interface are marked as
java.io.Serializable interface are marked as
serializable
serializable


Also subclasses
Also subclasses


empty interface
empty interface
-
-
just a marker
just a marker


no need
no need
to implement any special methods
to implement any special methods
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Not All Objects Are Serializable
Not All Objects Are Serializable


Any object that doesn’t implement Serializable
Any object that doesn’t implement Serializable


Any object that would pose a security risk
Any object that would pose a security risk


e.g. FileInputStream
e.g. FileInputStream


Any object whose value depends on VM
Any object whose value depends on VM
-
-
specific information
specific information


e.g. Thread
e.g. Thread


Any object that
Any object that
contains
contains
a
a
(non
(non
-
-
static, non
static, non
-
-
transient)
transient)
unserializable object
unserializable object
(recursively)
(recursively)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
NotSerializableException
NotSerializableException


thrown if you try to serialize or unserialize
thrown if you try to serialize or unserialize
an unserializable object
an unserializable object


maybe you subclassed a Serializable
maybe you subclassed a Serializable
object and added some unserializable
object and added some unserializable
members
members
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Incompatible Changes
Incompatible Changes


If class has members added or removed, it
If class has members added or removed, it
becomes
becomes
incompatible
incompatible


java.io.InvalidClassException thrown if you
java.io.InvalidClassException thrown if you
try to deserialize an incompatible object
try to deserialize an incompatible object
stream
stream
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Serial Version
Serial Version


If the changes were actually compatible
If the changes were actually compatible


find out the Serial Version UID of the
find out the Serial Version UID of the
original
original
class
class


use the
use the
serialver
serialver
utility
utility


add a member variable to the
add a member variable to the
changed
changed
class
class
protected static final long serialVersionUID =
protected static final long serialVersionUID =
-
-
2215190743590612933L;
2215190743590612933L;


now it’s marked as compatible with the old class
now it’s marked as compatible with the old class
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Using readObject
Using readObject


if you need to force an object to be
if you need to force an object to be
compatible
compatible


implement readObject() method to make
implement readObject() method to make
compatible changes
compatible changes
private void readObject(ObjectInputStream
private void readObject(ObjectInputStream
stream) throws java.io.IOException
stream) throws java.io.IOException
{
{
defaultReadObject(stream);
defaultReadObject(stream);
// do compatible stuff
// do compatible stuff
}
}
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Callbacks
Callbacks


The server calls a remote method on the
The server calls a remote method on the
client => the server becomes the client,
client => the server becomes the client,
and the client is now the server
and the client is now the server


The client registers a notification method
The client registers a notification method
with the server
with the server


Server invokes the method (“calls back”)
Server invokes the method (“calls back”)
to notify the client
to notify the client


Registry is out of the loop
Registry is out of the loop
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Security
RMI Security


Server is untrusted
Server is untrusted


Stubs could be malicious
Stubs could be malicious


rmic is OK, but someone could custom
rmic is OK, but someone could custom
-
-
code an evil stub: it’s just a .class file
code an evil stub: it’s just a .class file
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Security Managers
RMI Security Managers


AppletSecurityManager
AppletSecurityManager


stub can only do what an applet can do
stub can only do what an applet can do


RMISecurityManager
RMISecurityManager


disables all functions except class definition and
disables all functions except class definition and
access
access


A downloaded class is allowed to make a connection
A downloaded class is allowed to make a connection
if the connection was initiated via the RMI
if the connection was initiated via the RMI
transport.
transport.


None
None


Stub loading disabled
Stub loading disabled


Stubs still work if they are in local classpath
Stubs still work if they are in local classpath
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Codebase Property
Codebase Property


Stub classpaths can be confusing
Stub classpaths can be confusing


3 VMs, each with its own classpath
3 VMs, each with its own classpath


Server vs. Registry vs. Client
Server vs. Registry vs. Client


The RMI class loader always loads stubs from the
The RMI class loader always loads stubs from the
CLASSPATH first
CLASSPATH first


Next, it tries downloading classes from a web
Next, it tries downloading classes from a web
server
server


(but only if a security manager is in force)
(but only if a security manager is in force)


java.rmi.server.codebase specifies which web
java.rmi.server.codebase specifies which web
server
server
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Stub File Configuration
Stub File Configuration


The best way to configure it is as follows.
The best way to configure it is as follows.


NEVER have stub class files in ANY classpath
NEVER have stub class files in ANY classpath


make the stub files accessible via a web server
make the stub files accessible via a web server


set the java.rmi.server.codebase property, in the
set the java.rmi.server.codebase property, in the
application creating the server object, to the web
application creating the server object, to the web
server's URL
server's URL


The stubs will be downloaded from your HTTP
The stubs will be downloaded from your HTTP
server on demand
server on demand


(Thanks to Kevin.Conner@UK.Sun.COM for advice)
(Thanks to Kevin.Conner@UK.Sun.COM for advice)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Codebase and Thread Safety
Codebase and Thread Safety


There’s a thread problem with codebase
There’s a thread problem with codebase


With a single remote server, and multiple
With a single remote server, and multiple
remote objects on that server, each with its
remote objects on that server, each with its
own stub codebase
own stub codebase


One server can step on the codebase property
One server can step on the codebase property
and foul up the registration for the other
and foul up the registration for the other
server
server


In practice, won’t happen very often
In practice, won’t happen very often


More likely to happen in servlets
More likely to happen in servlets
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Limitations of RMI
Limitations of RMI


Java
Java
-
-
only
only


but you can use JNI on the server
but you can use JNI on the server


Uses TCP, not UDP
Uses TCP, not UDP


At least two sockets per connection
At least two sockets per connection


Untested for huge loads
Untested for huge loads
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI vs. COM
RMI vs. COM


Very similar
Very similar


remote interfaces ~ type libraries
remote interfaces ~ type libraries


COM is Win32
COM is Win32
-
-
only (for now)
only (for now)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Sun vs. Microsoft
Sun vs. Microsoft


RMI is not shipped as part of Microsoft’s
RMI is not shipped as part of Microsoft’s
products
products


RMI will still work in applications
RMI will still work in applications


include java.rmi.* class files in your classpath
include java.rmi.* class files in your classpath


download rmi.zip from ftp.microsoft.com
download rmi.zip from ftp.microsoft.com


RMI will work in applets
RMI will work in applets


include java.rmi.* class files (or rmi.zip) in your
include java.rmi.* class files (or rmi.zip) in your
codebase
codebase


extra download time
extra download time
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
RMI Chat Server Objects
RMI Chat Server Objects
Message
interface
MessageReceiver
-
receiveMessage(Message)
ChatClient
interface
ChatServer
-
login(MessageReceiver)
-
sendMessage(Message)
ChatServerImpl
Dispatcher
MessageQueue
implements
remote reference
local reference
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Summary
Summary


RMI is a very clean API
RMI is a very clean API


Easy way to write distributed programs
Easy way to write distributed programs


Wire protocol may need improvement for
Wire protocol may need improvement for
large
large
-
-
scale problems
scale problems
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
Where to get more information
Where to get more information


Core Java Volume II by Cay S Horstmann and
Core Java Volume II by Cay S Horstmann and
Gray Cornell
Gray Cornell


Harold,
Harold,
Java Network Programming
Java Network Programming
(O’Reilly)
(O’Reilly)


rmi
rmi
-
-
users mailing list
users mailing list
(rmi
(rmi
-
-
users@javasoft.com)
users@javasoft.com)


http://www.developer.com/ (Gamelan)
http://www.developer.com/ (Gamelan)


http://www.javaworld.com/ (magazine)
http://www.javaworld.com/ (magazine)


http://www.stinky.com/java/ (original Author’s
http://www.stinky.com/java/ (original Author’s
site)
site)
11/18/2003
11/18/2003
ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel
and Distributed Programming
and Distributed Programming
My sincere thanks
My sincere thanks


Alex Chaffee’s Purple Technology and his
Alex Chaffee’s Purple Technology and his
RMI slides which I have used in my
RMI slides which I have used in my
Presentation
Presentation