Date post: | 03-Apr-2018 |
Category: |
Documents |
Upload: | deepak-chaudhary |
View: | 216 times |
Download: | 0 times |
of 63
7/28/2019 RMI-3333
1/63
Copyright 1997 Alex Chaffee
Java Remote Method Invocation
(RMI)
Alexander Day Chaffee
7/28/2019 RMI-3333
2/63
Copyright 1997 Alex Chaffee
Introduction
Java
Networking
Distributed Computing
7/28/2019 RMI-3333
3/63
Copyright 1997 Alex Chaffee
Who is in Attendance?
Find out any relevant background and
interest of the audience
Java Programmers?
C++ Programmers?
TCP/IP Gurus?
Smalltalk?
7/28/2019 RMI-3333
4/63
Copyright 1997 Alex Chaffee
Agenda
Background
APIs and Usage
Q&A
7/28/2019 RMI-3333
5/63
Copyright 1997 Alex Chaffee
Overview
TCP/IP
java.net
RMI JDBC CORBA
Network OS
7/28/2019 RMI-3333
6/63
Copyright 1997 Alex Chaffee
What Is RMI?
Access to Remote Objects
Java-to-Java only
Client-Server Protocol
High-level API
TransparentLightweight
7/28/2019 RMI-3333
7/63Copyright 1997 Alex Chaffee
Examples of Use
Database access
Computations
Any custom protocol
Not for standard protocols (HTTP, FTP,
etc.)
7/28/2019 RMI-3333
8/63Copyright 1997 Alex Chaffee
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
7/28/2019 RMI-3333
9/63Copyright 1997 Alex Chaffee
Part I: RMI Concepts
7/28/2019 RMI-3333
10/63Copyright 1997 Alex Chaffee
TCP
Remote Objects (Diagram)
Java Virtual Machine
ClientObject
Java Virtual Machine
RemoteObject
7/28/2019 RMI-3333
11/63Copyright 1997 Alex Chaffee
RMI Layers
TCPRemote Reference Layer
Transport Layer
Java Virtual Machine
Client
Object
Remote Reference Layer
Transport Layer
Java Virtual Machine
Stub
Remote
Object
Skeleton
7/28/2019 RMI-3333
12/63Copyright 1997 Alex Chaffee
Remote Objects
Remote Objects
Live on server
Accessed as if they were local
7/28/2019 RMI-3333
13/63
Copyright 1997 Alex Chaffee
Registries
Name and look up remote objects
Servers can register their objects
Clients can find server objects and obtain a
remote reference
A registry is a running process on a host
machine
7/28/2019 RMI-3333
14/63
Copyright 1997 Alex Chaffee
Remote References and
InterfacesRemote References
Refer to remote objects
Invoked on client exactly like local objectreferences
Remote Interfaces
Declare exposed methods
Implemented on client
Like a proxy for the remote object
7/28/2019 RMI-3333
15/63
Copyright 1997 Alex Chaffee
Stubs and Skeletons
Stub
lives on client
pretends to be remote object
Skeleton
lives on server
receives requests from stub
talks to true remote object
delivers response to stub
7/28/2019 RMI-3333
16/63
Copyright 1997 Alex Chaffee
Remote Interfaces and Stubs
Remote Interface
Stub Remote Object(Server)
Client Skeleton
implements implements
7/28/2019 RMI-3333
17/63
Copyright 1997 Alex Chaffee
Remote Reference Layer
Local pointers not good enough
Figures out which remote object is being
referenced
Could span multiple virtual machines
Communicates via TCP/IP
7/28/2019 RMI-3333
18/63
Copyright 1997 Alex Chaffee
Transport Layer
Deals with communications
Connection management
Dispatching messages between stub and
skeleton
Distributed Garbage Collection
Sits on top of java.net
7/28/2019 RMI-3333
19/63
Copyright 1997 Alex Chaffee
HTTP Tunneling
Cool: if it cant make the connection
normally, it will tunnel through port 80
Allows clients behind firewall to makeremote calls to server
Note: does not work server -> client
7/28/2019 RMI-3333
20/63
Copyright 1997 Alex Chaffee
RMI System Architecture
Client Virtual Machine
Client
Server Virtual Machine
Stub
Remote
Object
Skeleton
Registry Virtual Machine
Fred
Server
7/28/2019 RMI-3333
21/63
Copyright 1997 Alex Chaffee
RMI Flow
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
7/28/2019 RMI-3333
22/63
Copyright 1997 Alex Chaffee
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
(and stub gets created)
3
7/28/2019 RMI-3333
23/63
Copyright 1997 Alex Chaffee
RMI Flow
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 skeleton
7. Skeleton invokes remote object
method
5 7
7/28/2019 RMI-3333
24/63
Copyright 1997 Alex Chaffee
Part II: RMI Usage
7/28/2019 RMI-3333
25/63
Copyright 1997 Alex Chaffee
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
7/28/2019 RMI-3333
26/63
Copyright 1997 Alex Chaffee
Remote Interface Example
import java.rmi.*;
public interface Adder
extends Remote{
public int add(int x, int y)
throws RemoteException;}
7/28/2019 RMI-3333
27/63
Copyright 1997 Alex Chaffee
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;
}
}
7/28/2019 RMI-3333
28/63
Copyright 1997 Alex Chaffee
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
7/28/2019 RMI-3333
29/63
Copyright 1997 Alex Chaffee
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)
7/28/2019 RMI-3333
30/63
Copyright 1997 Alex Chaffee
Registering Remote Classes
start the registry
running process
Unix:rmiregistry &
Windows:
start /m rmiregistry
7/28/2019 RMI-3333
31/63
Copyright 1997 Alex Chaffee
Registry CLASSPATH
Registry VM needs to be able to find stub
file(s)
You must set the CLASSPATH to includethe 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)
7/28/2019 RMI-3333
32/63
Copyright 1997 Alex Chaffee
Create the server
Creates a new instance of the remote object
Registers it in the registry with a unique
name
Thats it
7/28/2019 RMI-3333
33/63
Copyright 1997 Alex Chaffee
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();
}
7/28/2019 RMI-3333
34/63
Copyright 1997 Alex Chaffee
Launch the Server
% java AdderServer &
Adder bound
7/28/2019 RMI-3333
35/63
Copyright 1997 Alex Chaffee
Server Logging
invoke from command line
java
-Djava.rmi.server.logCalls=true
YourServerImpl
or enable inside program
RemoteServer.setLog(System.err);
7/28/2019 RMI-3333
36/63
Copyright 1997 Alex Chaffee
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!
7/28/2019 RMI-3333
37/63
Copyright 1997 Alex Chaffee
RMI URLs
rmi://host[:port]/name
default port is 1099
Specifies hostname ofregistry
can also use relative URLs
name only
assumes registry is on local host
7/28/2019 RMI-3333
38/63
Copyright 1997 Alex Chaffee
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);
7/28/2019 RMI-3333
39/63
Copyright 1997 Alex Chaffee
Remote Interfaces vs. Remote
ClassesRemember that the reference is to an
interface
You must make references, arrays, etc. outof the interface type, not the implementation
type
You cant cast the remote reference to anormal reference
So name your Remote Objects with Impl
(so you dont get confused)
7/28/2019 RMI-3333
40/63
Copyright 1997 Alex Chaffee
Parameter Passing
Primitive types
passed by value
Remote objects
passed by reference
Non-remote objects
passed by value
uses Java Object Serialization
7/28/2019 RMI-3333
41/63
Copyright 1997 Alex Chaffee
Object Serialization
aka Persistence
saves the state (data) of a particular instance
of an object
serialize - to save
unserialize - to load
7/28/2019 RMI-3333
42/63
Copyright 1997 Alex Chaffee
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
7/28/2019 RMI-3333
43/63
Copyright 1997 Alex Chaffee
java.io.Serializable
Objects that implement the
java.io.Serializable interface are marked as
serializableAlso subclasses
Magically, all non-static and non-transient
data members will be serializedActually, its not magic, itsReflection(its
done with mirrors)
empty interface - just a marker
7/28/2019 RMI-3333
44/63
Copyright 1997 Alex Chaffee
Not All Objects Are
SerializableAny object that doesnt implementSerializable
Any object that would pose a security riske.g. FileInputStream
Any object whose value depends on VM-
specific informatione.g. Thread
Any object that contains a (non-static, non-
transient)
unserializable object(recursively)
7/28/2019 RMI-3333
45/63
Copyright 1997 Alex Chaffee
NotSerializableException
thrown if you try to serialize or unserialize
an unserializable object
maybe you subclassed a Serializable objectand added some unserializable members
7/28/2019 RMI-3333
46/63
Copyright 1997 Alex Chaffee
Incompatible Changes
If class has members added or removed, it
becomes incompatible
java.io.InvalidClassException thrown if youtry to deserialize an incompatible object
stream
7/28/2019 RMI-3333
47/63
Copyright 1997 Alex Chaffee
Serial Version
If the changes were actually compatible
find out the Serial Version UID of the
original classuse the serialver utility
add a member variable to the changed class
protected static final long serialVersionUID =-2215190743590612933L;
now its marked as compatible with the old
class
7/28/2019 RMI-3333
48/63
Copyright 1997 Alex Chaffee
Using readObject
if you need to force an object to be
compatible
implement readObject() method to makecompatible changes
private void readObject(ObjectInputStream
stream) throws java.io.IOException
{
defaultReadObject(stream);
// do compatible stuff
}
7/28/2019 RMI-3333
49/63
Copyright 1997 Alex Chaffee
Callbacks
They just work
Pass in a remote reference to a client object
Server object can call its methods
transparently
Registry is out of the loop
7/28/2019 RMI-3333
50/63
Copyright 1997 Alex Chaffee
RMI Security
Server is untrusted
Stubs could be malicious
rmic is OK, but someone could custom-
code an evil stub: its just a .class file
7/28/2019 RMI-3333
51/63
Copyright 1997 Alex Chaffee
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
7/28/2019 RMI-3333
52/63
Copyright 1997 Alex Chaffee
Codebase Property
Stub classpaths can be confusing
3 VMs, each with its own classpath
Server vs. Registry vs. Client The RMI class loader always loads stubs from the
CLASSPATH first
Next, it tries downloading classes from a web
server
(but only if a security manager is in force)
java.rmi.server.codebase specifies which web
server
7/28/2019 RMI-3333
53/63
Copyright 1997 Alex Chaffee
Stub File Configuration
The best way to configure it is as follows.
NEVER have stub class files in ANY classpath
make the stub files accessible via a web server
set the java.rmi.server.codebase property, in
the application creating the server object, to the
web server's URL
The stubs will be downloaded from your
HTTP server on demand (Thanks to [email protected] for advice)
7/28/2019 RMI-3333
54/63
Copyright 1997 Alex Chaffee
Codebase and Thread Safety
Theres a thread problem with codebase
With a single remote server, and multiple
remote objects on that server, each with itsown stub codebase
One server can step on the codebase
property and foul up the registration for theother server
In practice, wont happen very often
More likely to happen in servlets
7/28/2019 RMI-3333
55/63
Copyright 1997 Alex Chaffee
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
7/28/2019 RMI-3333
56/63
Copyright 1997 Alex Chaffee
RMI vs. COM
Very similar
remote interfaces ~ type libraries
COM is Win32-only (for now)
7/28/2019 RMI-3333
57/63
Copyright 1997 Alex Chaffee
Sun vs. Microsoft
RMI is not shipped as part of Microsofts 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
IE4: only if theyre signed
extra download time
7/28/2019 RMI-3333
58/63
Copyright 1997 Alex Chaffee
Part III: RMI Chat Server
7/28/2019 RMI-3333
59/63
Copyright 1997 Alex Chaffee
RMI Chat Server Objects
Message
interface
MessageReceiver- receiveMessage(Message)
ChatClient
interface
ChatServer- login(MessageReceiver)
- sendMessage(Message)
ChatServerImpl
Dispatcher MessageQueueimplementsremote reference
local reference
7/28/2019 RMI-3333
60/63
Copyright 1997 Alex Chaffee
Summary
RMI is a very clean API
Easy way to write distributed programs
Wire protocol may need improvement for
large-scale problems
7/28/2019 RMI-3333
61/63
Copyright 1997 Alex Chaffee
Where to get more
informationOther training sessions
Harold,Java Network Programming
(OReilly)
rmi-users mailing list ([email protected])
http://www.developer.com/ (Gamelan)
http://www.javaworld.com/ (magazine)
http://www.stinky.com/java/ (Authors site)
7/28/2019 RMI-3333
62/63
Copyright 1997 Alex Chaffee
Q&A
7/28/2019 RMI-3333
63/63
Feedback
Request feedback of training session