Download Java Remote Method Invocation (RMI) in Distributed Computing and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!
CMSC 433 – Programming Language
Technologies and Paradigms
Fall 2008
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
Different Approaches to
Distributed Computation
- 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
7
Remote Objects
• Object should
- Extend java.rmi.server.UnicastRemoteObject
- Constructor declared to throw RemoteException
- Implement a remote interface
- A remote interface extends java.rmi.Remote
- All methods in a remote interface throw RemoteException - “Something bad happened on the network”
- Side note: actually, don’t need to extend
UnicastRemoteObject, but it’s much easier
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*
- 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+
- And 1.2+ generates position-independent code
- Use -v1.2 if you want
- Generates stubs for all methods declared in the
class’ Remote interface
- Other methods don’t get a stub
- *Don’t need to use rmic anymore
13
Stub Code
• Objects contain both data and code
- When you receive a remote object, you need
the stub for that remote object
• 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 code base where
stub code for objects can be downloaded
java -Djava.rmi.server.codebase= ...
- Specifies location of classes originating 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
Server
interface Server extends Remote { Connection logon(String name, Client c) throws RemoteException; } 20
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; }
21
Client
interface Client extends Remote { void said(String who, String msg) throws RemoteException; void whoChanged(String [] who) throws RemoteException; } 22
Server’s Remote Object creation
ServerImpl Server s = new ServerImpl(); Hosted Remote Objects s
Server
Object added to table because it implements extension of Remote interface
25
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 26
After lookup finished
ClientImpl Hosted Remote Objects c s ServerImpl Stub Hosted Remote Objects ServerImpl
Client Server
27
Invokes remote Server method
Connection conn = s.logon(“Bill”, c); s ServerImpl Stub
Client
Stub code for remote logon call String “Bill” Stub for c Method: logon … to server process logon c ClientImpl 28
Receives remote call
Server
(Skeleton) code for remote logon call String “Bill” Stub for c Method: logon … from client process Hosted Remote Objects ServerImpl “Bill” ClientImpl Stub c unmarshalled arguments
31
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 32
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());
33
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=