Download Java RMI - Lecture Slides - Programming Language Technologies and Paradigms | CMSC 433 and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2009
Java RMI
2
Distributed Computing
• Programs that cooperate and communicate
over a network
- E-mail
- Web server and web client
- SETI @Home
3
Key Features of Distrib. Comp.
• Machines are not all the same
- But all adhere to same communication protocol
• Network is “slow”
- Sending a message takes a lot of time
• Network is unreliable
- Machines may join and leave with no warning
- Part of the network may fail 4
Distributing Computations
- Connecting via sockets
- E.g., project 1
- Custom protocols for each application
- RPC/DCOM/CORBA/RMI
- Make what looks like a normal function call
- Function actually invoked on another machine
- Arguments are marshalled for transport
- Value is unmarshalled on return
Remote Objects
- Objects implement a Remote interface
- A remote interface extends java.rmi.Remote
- All interface methods throw RemoteException
- Constructor throws RemoteException
- RemoteException means “something bad
happened on the network”
7 8
Remote Interfaces
9
Stubs
• Client only sees the RemoteInterface
- ConcreteObject can have other methods
• Remote objects represented using stub
- Stub sends arguments over network
- Stub receives result back from network 10
Compiling Stubs with rmic*
- RMI compiler
- *Don’t need to use rmic anymore
- Generates stub code for a class
- For 1.1, also generates skeleton class
- Stub on client side communicates with skeleton on remote side
- Skeleton not needed for 1.2+
- Generates stubs for all methods declared in the
class’ Remote interface
- Other methods don’t get a stub
13
Stub Code
• Objects contain both data and code
- When you receive a remote object, you need
the stub for that remote object
• Where does it come from?
• Solution #1: All clients have stub code on
their classpath
- Or stub code for another class with same
remote interface
14
Downloading Code
• Solution #2: Provide a codebase where stub
code for objects can be downloaded
java -Djava.rmi.server.codebase= ...
- Specifies location of classes orig. from this jvm
- URL can be, e.g., http:// or file:/
15
Getting the First Remote Object
• Can make objects available in RMI registry
- Each object has a name (that you specify)
- Registry listens on a port (1099 default)
• Naming.lookup(url) gets object from reg.
- e.g., Naming.lookup(“rmi://localhost/Chat”);
- Use to get first reference to remote object
- Don’t need to lookup objects returned by
remote methods
16
Starting an RMI Registry
• Method 1: Separate RMI registry process
- Command rmiregistry
- Run with stubs in classpath, or specify codebase
- Listens on port 1099 by default
• Method 2: Start in same JVM
- LocateRegistry.createRegistry(int port)
- Advantage: dies when your program dies
- No registries lying around on machine
19
Example: RMI Chat Server
• Server
• Client
- Participant in chat room
- Receives messages from others in room
• Connection
- Uniquely identifies a client
- Used to speak in chat room 20
Server
interface Server extends Remote { Connection logon(String name, Client c) throws RemoteException; public void notifyWhoChanged() throws RemoteException; public Map<String,Client> who() throws RemoteException; }
21
Connection
interface Connection extends Remote { / Say to everyone / void say(String msg) throws RemoteException; / ** Say to one person / void say(String who, String msg) throws RemoteException; String [] who() throws RemoteException; void logoff() throws RemoteException; } 22
Client
interface Client extends Remote { void speak(String who, String msg) throws RemoteException; void whoChanged(String [] who) throws RemoteException; }
25
Client’s Remote Object creation
ClientImpl Client c = new ClientImpl(); Hosted Remote Objects c
Client
Client object also implements extension of Remote interface 26
Client looks up Server
Server s = (Server) Naming.lookup (“//host/ChatServer”); s ServerImpl Stub Hosted Remote Objects ServerImpl
Client RMI Registry Server
ChatServer ServerImpl Stub lookup returns stub
27
After lookup finished
ClientImpl Hosted Remote Objects c s ServerImpl Stub Hosted Remote Objects ServerImpl
Client Server
28
Client Invokes Remote Method
Connection conn = s.logon(“Bill”, c); s ServerImpl Stub
Client
remote logon call String “Bill” Stub for c Method: logon … marshalled args to server process logon c ClientImpl
31
Server Returns the Result
Server
Hosted Remote Objects ServerImpl ConnectionImpl … return this as the result remote logon result Stub for conn Return value: … to client process 32
Client Receives the Result
s ServerImpl Stub
Client
Stub code for remote logon call Stub for conn Return value: … from server process logon Conn Stub conn unmarshalled return value
33
Security Manager
- When using a code base, we must download stub
code from a remote site. This is potentially risky
- Need to limit what downloaded code could do
- Must install a Security Manager before you download any code from RMI code bases
- Can use System.setSecurityManager( new RMISecurityManager()); 34
Policy Files
• In addition to security manager, need to
specify a security policy
grant { permission java.net.SocketPermission “: 1024-65535”, “connect,accept”; permission java.net.SocketPermission “:80”, “connect”; };**
• Set security policy when JVM started
- java -Djava.security.policy==