CMSC 433 - Programming Language Technologies and Paradigms: Java RMI
CMSC 433 - Programming Language Technologies and Paradigms: Java RMI
Java RMI
Distributed Computing
Programs that cooperate and communicate
over a network
E-mail
Web server and web client
SETI @Home
Distributed Computing
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
Distributing Computations
Connecting via sockets
e.g., Logging Server examples
Custom protocols for each application
RPC/DCOM/CORBA/RMI
Make what looks like a normal function call
Function actually invoked on another machine
Arguments/return values are marshalled /
unmarshalled for transport across the network
A Simplified Example
// runs on one mach.
class ChatServerImpl implements ChatServer ... {
public void say(String s) {
System.out.println(s);
}
...
}
class Chatter { // runs on another mach.
public static void main(String args[]) {
ChatServer c = // get remote object;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
while (true) {
System.out.print(> );
c.say(br.readLine());
}
}
}
Remote Objects
Objects implement a Remote interface
A Remote interface extends java.rmi.Remote
All Remote interface methods throw
RemoteException
Constructor throws RemoteException
RemoteException means something bad
happened on the network
Remote Interfaces
Stubs
Client only sees the RemoteInterface
ConcreteObject can have other methods
Passing Arguments
To pass an argument to a Remote method or
return a result from a Remote method,
object/value must be either
A primitive type (int, double, etc.),
Serializable (e.g., String), or
Remote (i.e., implement a sub-interface of
Remote)
Stub Code
Classes contain both data and code
When you receive a Remote object, you need
the stub for that object
Downloading Code
Solution #2: Provide a codebase where stub
code for objects can be downloaded
java -Djava.rmi.server.codebase=<url> ...
Specifies location of code for classes that
originate in this JVM
URL - can be http://, file:/, etc.
Client
Represents a participant in chat room
Receives messages from others in room
Connection
Links client to Server
Used to speak in chat room
Server
interface Server extends Remote {
Connection logon(String name, Client c)
throws RemoteException;
public Map<String,Client> getUsers()
throws RemoteException;
}
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 [] getUsers()
throws RemoteException;
void logoff()
throws RemoteException;
}
Client
interface Client extends Remote {
void wasSaid(String who, String msg)
throws RemoteException;
void usersChanged(String [] who)
throws RemoteException;
}
ServerImpl
Server
ChatServer
ServerImpl
Stub
ServerImpl
Server
RMI Registry
Client
ClientImpl
Hosted
Remote
Objects
returns
stub
lookup
ChatServer
Hosted
Remote
Objects
ServerImpl
Stub
ServerImpl
s
Client
ServerImpl
Stub
RMI Registry
Server
Hosted
Remote
Objects
ClientImpl
ServerImpl
s
Client
ServerImpl
Stub
Server
ClientImpl
marshalled args
to server process
Method: logon
Stub for c
String Adam
Client
ServerImpl
Stub
logon
Hosted
Remote
Objects
Method: logon
Stub for c
String Bill
ClientImpl
Bill
Stub c
Server
unmarshalled arguments
ServerImpl
Hosted
Remote
Objects
ConnectionImpl
call logon
ClientImpl
Bill
Stub c
Server
ServerImpl
Hosted
Remote
Objects
ConnectionImpl
Return value:
Stub for conn
to client process
Server
remote
logon result
ServerImpl
conn
Client
ServerImpl
Stub
logon
Conn Stub
unmarshalled return value
Security Manager
When using a codebase, 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 codebases
Can use
System.setSecurityManager(
new RMISecurityManager());
Policy Files
In addition to security manager, need to
specify a security policy, e.g.,
grant {
permission java.net.SocketPermission *:
1024-65535, connect,accept;
permission java.net.SocketPermission *:80,
connect;
};
Debugging Tips
See:
http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/
logging.html
-Djava.rmi.server.logCalls=true
-Dsun.rmi.server.logLevel=VERBOSE
-Dsun.rmi.loader.logLevel=VERBOSE