0% found this document useful (0 votes)
67 views

Cup 13: RMI: Special Topics: Java

The document discusses Remote Method Invocation (RMI) in Java. RMI allows a client to call methods on remote objects residing in a server. It describes the key components of RMI including remote objects, stubs, skeletons, the RMI registry, and security. The RMI registry acts as a directory to lookup remote objects. Stubs and skeletons handle passing parameters and return values between remote method calls. The document provides an example of implementing a simple quote server and client using RMI.

Uploaded by

pavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Cup 13: RMI: Special Topics: Java

The document discusses Remote Method Invocation (RMI) in Java. RMI allows a client to call methods on remote objects residing in a server. It describes the key components of RMI including remote objects, stubs, skeletons, the RMI registry, and security. The RMI registry acts as a directory to lookup remote objects. Stubs and skeletons handle passing parameters and return values between remote method calls. The document provides an example of implementing a simple quote server and client using RMI.

Uploaded by

pavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Special Topics: Java

Cup 13: RMI

Dr. Tim Margush


Department of Mathematics and Computer Science
The University of Akron

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

Tim Margush, Univers

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

Tim Margush, Univers

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

Tim Margush, Univers

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

Tim Margush, Univers

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

Tim Margush, Univers

13-6

A Simple RMI Quote


Server
Remote Interface
Defines the public methods of the
remote object

The remote object will implement


this interface
Remote methods must throw
RemoteException, in case an error
occurs during the RMI process
Jul 7, 2016

Tim Margush, Univers

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

Tim Margush, Univers

13-8

The Remote Object


import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
//UnicastRemoteObject provides server-type stuf
public class QuoteOfTheDay
extends UnicastRemoteObject
implements QuoteProtocol {
//RMI requires this default constructor
public QuoteOfTheDay()
throws RemoteException { }

Jul 7, 2016

Tim Margush, Univers

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

Tim Margush, Univers

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

Tim Margush, Univers

13-11

Creating the Stub and


Skeleton
After compiling the class...
javac QuoteOfTheDay.java

Create the stub and skeleton


rmic QuoteOfTheDay
QuoteOfTheDay_Stub.class
QuoteOfTheDay_Skel.class
(be sure CLASSPATH gives access to java's
Classes.zip and the QuoteOfTheDay.class
file)

Jul 7, 2016

Tim Margush, Univers

13-12

Registering a Remote
Object
Install a Security Manager
RMISecurityManager

Instantiate the remote object


remObjRef = new objRemote()

Bind the remote object to the registry


Naming.rebind("someName", remObjRef)

The RMI Registry must be running


before binding can occur
Jul 7, 2016

Tim Margush, Univers

13-13

Starting the Registry


The RMI Registry is an application
found in the jdk's bin directory
Run it as a background process
rmiregistry [port]
default port is 1099
Be sure the CLASSPATH variable for
this session includes the location of
the Stub and Skeleton!
Jul 7, 2016

Tim Margush, Univers

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

Workaround - use this switch to fully


qualify the hostname of the server:
java -Djava.rmi.server.hostname=someHost.edu QuoteOfTheDay
Jul 7, 2016

Tim Margush, Univers

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

Tim Margush, Univers

13-16

Instantiating the Stub


Naming.lookup finds the registered object by
name, instantiates a Stub, and returns a
reference it
try {
String regName = "rmi://130.101.87.25/quoteoftheday";
qod = (QuoteProtocol) Naming.lookup(regName);
} catch (Exception ex) {
System.out.println("Cannot get remote object: " + ex);
}
Jul 7, 2016

Tim Margush, Univers

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

Tim Margush, Univers

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

Tim Margush, Univers

13-19

You might also like