Documente Academic
Documente Profesional
Documente Cultură
Introduction
Java
Networking Distributed
Computing
Who is in Attendance?
Find
out any relevant background and interest of the audience Java Programmers? C++ Programmers? TCP/IP Gurus? Smalltalk?
Agenda
Background
APIs Q&A
and Usage
Overview
RMI
JDBC
CORBA
java.net
TCP/IP
Network OS
Copyright 1997 Alex Chaffee
What Is RMI?
Access
to Remote Objects Java-to-Java only Client-Server Protocol High-level API Transparent Lightweight
Examples of Use
Database
access Computations Any custom protocol Not for standard protocols (HTTP, FTP, etc.)
Related Technologies
RPC
CORBA
DCOM
LDAP
Client Object
TCP
Remote Object
RMI Layers
Java Virtual Machine Client Object Java Virtual Machine Remote Object
Stub
Skeleton
Transport Layer
TCP
Transport Layer
Copyright 1997 Alex Chaffee
Remote Objects
Remote
Live
Objects
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
References
Interfaces
Declare
exposed methods Implemented on client Like a proxy for the remote object
Copyright 1997 Alex Chaffee
on server receives requests from stub talks to true remote object delivers response to stub
Copyright 1997 Alex Chaffee
implements
implements
Client
Stub
Skeleton
pointers not good enough Figures out which remote object is being referenced Could span multiple virtual machines Communicates via TCP/IP
Transport Layer
Deals
with communications Connection management Dispatching messages between stub and skeleton Distributed Garbage Collection Sits on top of java.net
HTTP Tunneling
Cool:
if it cant 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
Stub
Skeleton Server
RMI Flow
1. Server Creates Remote Object Client Virtual Machine 2. Server Registers Remote Object Client Server Virtual Machine Remote Object
1
Stub
Skeleton Server
2
RMI Flow
Client Virtual Machine Client Server Virtual Machine Remote 3. Client requests object from Registry Object 4. Registry returns remote reference (and stub gets created) Stub
3 4
Skeleton Server
RMI Flow
Client Virtual Machine Client
5 6 7
Stub
Skeleton Server
5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object Fred method Registry Virtual Machine
Copyright 1997 Alex Chaffee
a Remote Interface
java.rmi.Remote
extends
java.rmi.RemoteObject or java.rmi.UnicastRemoteObject
javac
javac
AdderImpl.class (classfile)
rmic
the registry
process
running
Unix:
rmiregistry &
Windows:
start /m rmiregistry
Registry CLASSPATH
Registry
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) Copyright 1997 Alex Chaffee
a new instance of the remote object Registers it in the registry with a unique name Thats it
Server Logging
invoke
a Security Manager
Find
a registry
java.rmi.Naming
use
Lookup
the name, returns a reference Cast the reference to the appropriate Remote Interface Just use it!
Copyright 1997 Alex Chaffee
RMI URLs
rmi://host[:port]/name
default
port is 1099 Specifies hostname of registry can also use relative URLs
name
interface You must make references, arrays, etc. out of the interface type, not the implementation type You cant cast the remote reference to a normal reference So name your Remote Objects with Impl (so you dont get confused) Copyright 1997 Alex Chaffee
Parameter Passing
Primitive
passed
types
by value
Remote
passed
objects
by reference
Non-remote
passed
objects
Object Serialization
aka
Persistence 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 Magically, all non-static and non-transient data members will be serialized Actually, its not magic, its Reflection (its done with mirrors) empty interface - just a marker Copyright 1997 Alex Chaffee
object that doesnt implement Serializable Any object that would pose a security risk
e.g.
FileInputStream
Any
Thread
Any
NotSerializableException
thrown
if you try to serialize or unserialize an unserializable object maybe you subclassed a Serializable object and added some unserializable members
Incompatible Changes
If
class has members added or removed, it becomes incompatible java.io.InvalidClassException thrown if you try to deserialize an incompatible object stream
Serial Version
If
the changes were actually compatible find out the Serial Version UID of the original class
use
add
a member variable to the changed class its marked as compatible with the old
Copyright 1997 Alex Chaffee
now
class
Using readObject
if
you need to force an object to be compatible implement readObject() method to make compatible changes
private void readObject(ObjectInputStream stream) throws java.io.IOException { defaultReadObject(stream); // do compatible stuff }
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
RMI Security
Server
is untrusted Stubs could be malicious rmic is OK, but someone could customcode an evil stub: its just a .class file
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
Codebase Property
Stub
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
java.rmi.server.codebase
server
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
a thread problem with codebase With a single remote server, and multiple remote objects on that server, each with its own stub codebase One server can step on the codebase property and foul up the registration for the other server In practice, wont happen very often
More
Limitations of RMI
Java-only
but
Uses
TCP, not UDP At least two sockets per connection Untested for huge loads
is not shipped as part of Microsofts products RMI will still work in applications
include
include
java.rmi.* class files (or rmi.zip) in your codebase IE4: only if theyre signed extra download time
MessageReceiver
- receiveMessage(Message)
ChatServer
- login(MessageReceiver) - sendMessage(Message)
ChatClient
ChatServerImpl
Dispatcher
MessageQueue
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
training sessions Harold, Java Network Programming (OReilly) rmi-users mailing list (rmi-users@javasoft.com) http://www.developer.com/ (Gamelan) http://www.javaworld.com/ (magazine) http://www.stinky.com/java/ (Authors site)
Copyright 1997 Alex Chaffee
Q&A
Feedback
Request