Cup 13: RMI: Special Topics: Java
Cup 13: RMI: Special Topics: Java
Remote Method
Invocation
Remote - Objects exist on
different systems (Distributed or
Client-Server Computing)
Remote Method - A method
belonging to a class called a
server
RMI - A method in a client class
calls a method in a server class
Jul 7, 2016
13-2
RMI Registry
A separate application running on
machine A (providing remote objects)
Links the client and server together
Server starts and registers in the RMI
Registry of machine A
Client on machine B contacts RMI
Registry via a socket with a request
If allowed, a reference to the remote
object is returned
Jul 7, 2016
13-3
Stubs
The client's reference to a remote object
allows method calls across the internet
Arguments and return values are passed via
object serialization
A Skeleton object is created on the server
machine to perform the actual method calls
and communication with the server object
The Security Manager supervises the
communication between skeleton and server
Jul 7, 2016
13-4
RMISecurityManager
RMI interactions are governed by
some security manager
Client Applets use the Applet Security
Manager
Client Applications require installation of
an RMI Security Manager
A default manager is provided in java.rmi
A bootstrap process is typically used to install
the security manager before running an
application
Jul 7, 2016
13-5
Arguments and
Parameters
RMI requires copying
Passing an
the object to the remote
object to a
machine where it is
local method
reconstructed (in the
is
skeleton)
accomplishe
d by copying The skeleton invokes
the remote method,
the
passing a reference to
reference to
the reconstructed object
the object
Jul 7, 2016
13-6
13-7
The QuoteProtocol
This is the interface we will implement
import java.rmi.*;
public interface QuoteProtocol extends Remote
{
public String getQuote(int qnum)
throws RemoteException;
public int getMaxQuoteNumber()
throws RemoteException;
}
Jul 7, 2016
13-8
Jul 7, 2016
13-9
Implement
QuoteProtocol
Q is a private array of Strings
containing the quotes to be served
(declaration omitted)
public String getQuote(int qnum){
return Q[qnum]; //no check for limit - sloppy!
}
public int getMaxQuoteNumber(){
return Q.length-1;
}
Jul 7, 2016
13-10
Remote Bootstrap
public static void main(String[] args) {
System.setSecurityManager
(new RMISecurityManager());
try {
QuoteOfTheDay qod = new QuoteOfTheDay();
Naming.rebind("quoteoftheday", qod);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
Jul 7, 2016
13-11
Jul 7, 2016
13-12
Registering a Remote
Object
Install a Security Manager
RMISecurityManager
13-13
13-14
Getting in a Bind
With the Registry process active, you
can now register (bind) remote objects
java QuoteOfTheDay
Runs main, installs security manager,
instantiates remote object and registers it
This will work only on a LAN due to a bug
13-15
RMI Client
The client must instantiate the stub for
the remote object
The remote interface datatype is used
for this purpose
import java.rmi.*;
import java.rmi.registry.*;
public class Quote{
static QuoteProtocol qod;
Jul 7, 2016
13-16
13-17
Invoking a Remote
Method
The Stub object allows the client to invoke
any method in the remote interface
try {
int num = qod.getMaxQuoteNumber()
String someQuote = qod.getQuote(num);
System.out.println("Quote " + num + " is: " + aQuote);
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
Jul 7, 2016
13-18
An Applet Client
Set up the RMI access in the
Applet's Start method
regName = "rmi://" +
getCodeBase().getHost() + "/quoteoftheday";
qod = (QuoteProtocol)Naming.lookup(regName);
MQN = qod.getMaxQuoteNumber();
Jul 7, 2016
13-19