JAVA RMI Example
JAVA RMI Example
JAVA RMI Example
The aim of this tutorial is to explore JAVA RMI. At the end of this
lass you
should be able to write,
ompile and run some simple JAVA RMI programs.
We will use a shared whiteboard as an example (pp. 194-200). This is a
distributed program that allows a group of users to share a
ommon view of
a drawing surfa
e
ontaining graphi
al obje
ts, su
h as re
tangles, lines and
ir
les, ea
h of whi
h has been drawn by one of the users.
The whiteboard server has a version number (an integer) that it in
rements
ea
h time a new shape arrives. This version number is atta
hed to the new
shape. The server provides operations allowing
lients to enquire about its
version number and the version number of ea
h shape, so that they may avoid
fet
hing shapes that they already have.
NOTICE: we will implement a text-only version of this example, be
ause
we are mainly interested in the
ommuni
ation between remote obje
ts, not in
the graphi
al details.
Prerequisites
We assume familiarity with Java. You should be able to write,
ompile
You need a working JDK installation. Again, you should know how to use
and CS1PR2).
Graphi al obje ts will be stored using the JAVA lass Ve tor() from java.util.*.
You need a networked PC. Any PC in the labs is ne, Linux or Windows.
We rst de
lare a
lass for the obje
ts that will be drawn on the whiteboard. As
we are not going to implement graphi
al intera
tions, we do not import graphi
al
pa
kages. However, this should be the le where to store all the graphi
al
information, like
olours,
oordinates, et
. In our example, a Graphi
alObje
t
has a type only (e.g. Cir
le, Re
tangle, et
). Noti
e that Graphi
alObje
t must
implement the Serializable interfa
e, as obje
ts of this
lass will be passed using
JAVA RMI.
// *********************************************************
// * Graphi
alObje
t.java
*
// * Comments follow the
ommented line
*
// *********************************************************
import java.io.Serializable;
// Important! The
lass must implement the Serializable interfa
e
publi
lass Graphi
alObje
t implements Serializable{
publi
String type;
// A variable for the
hara
teristi
s of the graphi
al obje
t
// In a proper example, here you should de
lare
oords,
olours, et
.
publi
Graphi
alObje
t() { }
// an empty
ontrsu
tor
publi
Graphi
alObje
t(String aType) {
// a better
onstru
tor
type = aType;
// just setting up the variable above.
Remote interfa es
We now dene the interfa
es of the server. Remote interfa
es (p. 173) spe
ify
the remote methods implemented by a remote obje
t, i.e. the methods that
may be invoked by
lients.
There are two kind of remote obje
ts on the server: the whiteboard itself
is seen as a list of shapes, and its methods are dened in the ShapeList.java
interfa
e.
At the same time, shapes are drawn on the whiteboard, and ea
h of these
shapes is seen as an obje
t. The methods of the \shape obje
ts" are dened in
the Shape.java interfa
e.
//
//
//
//
//
//
*********************************************************
* ShapeList.java, page 195
*
* Comments follow the
ommented line.
*
* This interfa
e des
ribe the methods implemented by
*
* the whiteboard, whi
h is a remote obje
t
*
*********************************************************
import java.rmi.*;
// THIS IS IMPORTANT!!
import java.util.Ve
tor;
// Just a
lass to store the list of shapes drawn on the board
publi
interfa
e ShapeList extends Remote {
// IMPORTANT! We are extending Remote here, to denote that
// this is a remote obje
t.
Shape newShape(Graphi
alObje
t g) throws RemoteEx
eption;
// This method will be used to add a new shape to the board.
// Noti
e that an interfa
e is returned (Shape). This is the
ase
// with all remote obje
ts (see pp.195-196)
Ve
tor allShapes()throws RemoteEx
eption;
// This method will return the list of all shapes.
//
//
//
//
//
//
*********************************************************
* Shape.java, page 195
*
* Comments follow the
ommented line.
*
* This interfa
e des
ribes the methods implemented by *
* the remote obje
ts drawn on the whiteboard
*
*********************************************************
import java.rmi.*;
// IMPORTANT (see above)
publi
interfa
e Shape extends Remote {
//IMPORTANT (see above)
int getVersion() throws RemoteEx
eption;
// Just returning the version of this Shape
Server lasses
Here we set up the server
lasses implementing the two interfa
es presented
above. The rst
lass is ShapeServant.java and implements the methods of
the interfa
e Shape.java, i.e. ShapeServant.java implements the methods of the
shapes drawn on the board.
// *********************************************************
// * ShapeServant.java, page 195
*
// * Comments follow the
ommented line.
*
// * This is the implementation of Shape.java
*
// *********************************************************
import java.rmi.*;
// IMPORTANT (see above)
import java.rmi.server.Uni
astRemoteObje
t;
// ShapeServant extends Uni
astRemoteObje
t, whi
h
// provides remotes obje
ts that live only as long
// as the pro
ess in whi
h they are
reated (p.198).
publi
lass ShapeServant extends Uni
astRemoteObje
t implements Shape {
// De
laring the
lass. NOTICE! It implements the Shapen interfa
e.
int myVersion;
Graphi
alObje
t theG;
// ea
h shape has a version, and a Graphi
alObje
t.
publi
ShapeServant(Graphi
alObje
t g, int version)
throws RemoteEx
eption{
// The
onstru
tor. NOTICE that it throws a RemoteEx
eption
// The
onstru
tor is
alled by ShapeListServant.java
theG = g;
myVersion = version;
System.out.println("Created Shape with ID "+version);
// just setting up the lo
al variables
}
publi
int getVersion() throws RemoteEx
eption {
return myVersion;
// just returning the version
}
The following is the
lass for the whiteboard, ShapeListServant.java. ShapeListServant.java implements the interfa
e ShapeList.java.
// *********************************************************
// * ShapeListServant.java
*
// * Comments follow the
ommented line.
*
// * This
ode implements the interfa
e
*
// * ShapeList.java
*
// *********************************************************
import java.rmi.*;
import java.rmi.server.Uni
astRemoteObje
t;
// See
omments above for these pa
kages
import java.util.Ve
tor;
// This is a Ve
tor data stru
ture (an Array)
publi
lass ShapeListServant extends Uni
astRemoteObje
t
implements ShapeList{
// See
omments for ShapeServant.java; this
lass implements
// the ShapeList interfa
e.
private Ve
tor theList;
// The shapes on the whiteboard are stored in this Ve
tor
//
alled theList.
private int version;
// a number (in
reasing)
publi
ShapeListServant()throws RemoteEx
eption{
// Constru
tor: at the beginning, no shapes and version=0
theList = new Ve
tor();
version = 0;
}
publi
//
//
//
version++;
// First, in
rease the version by one.
Shape s = new ShapeServant( g, version);
// Then,
reate a new Shape obje
t. NOTICE: Shape is
// a remote obje
t, see ShapeServant.java.
theList.addElement(s);
// Now adding the the Shape obje
t to the Ve
tor
return s;
// Returning a referen
e to the remote obje
t (Shape)
The following is the implementation for the exe
utable Server, Whiteboard.java.
// *********************************************************
// * Whiteboard.java
*
// * Comments follow the
ommented line.
*
// * This
ode implements the Server.
*
// *********************************************************
import java.rmi.*;
// Obviously...
publi
lass Whiteboard {
publi
stati
void main(String args[){
// Just the main method here.
System.setSe
urityManager(new RMISe
urityManager());
// First, set up a se
urity manager. It prote
ts the lo
al
// resour
es to ensure that the
lasses that are loaded
// from remote sites
annot have any effe
t on resour
es
// su
h as files (see p.199)
System.out.println("Main running");
try{
ShapeList aShapelist = new ShapeListServant();
// First,
reate an instan
e of ShapeListServant, i.e.
//
reate an empty board...
System.out.println("Whiteboard
reated");
Naming.rebind("ShapeList", aShapelist);
// This is used to bind the the instan
e of ShapeListServant
//
reated above (aShapelist) to the RMIregistry (p.197).
// RMIregistry is the binder for Java RMI (see p.180).
System.out.println("Whiteboard ready");
}
at
h(Ex
eption e) {
System.out.println("ShapeList server main " +
e.getMessage());
}
To
ompile and run a JAVA RMI
ode some preliminary operations are required.
1. Compile the server: verb+java
Whiteboard.java+
2. Compile the remote obje
ts. For this, you need the rmi
ompiler (RMI
ompiler). You have to
ompile ShapeServant and ShapeListServant:
rmi
ShapeServant
rmi
ShapeListServant
This
reates the
lasses for proxies, dispat
hers and skeletons (see p.179
and g. 5.6 p. 177).
3. Laun
h the JAVA RMI binder,
alled rmiregistry: rmiregistry & (NOTICE: THIS CAN BE DIFFERENT UNDER WINDOWS XP).
4. Create a permission le,
all it mypoli
y, whose
ontent is:
grant {
permission java.se
urity.AllPermission;
};
The following is the implementation for a
lient of Witheboard. The name of the
lient is ShapeListClient.java. It is a very basi
version, just to understand how
to invoke remote methods and pass obje
ts. See below for possible extensions
of this.
// *********************************************************
// * Whiteboard.java
*
// * Comments follow the
ommented line.
*
// * This
ode implements the Server.
*
// *********************************************************
import java.rmi.*;
// Obviously...
import java.util.Ve
tor;
publi
lass ShapeListClient{
publi
stati
void main(String args[){
if (args.length < 2) {
// Che
king the
orre
t number of arguments.
// This
he
k
ould be improved.
System.out.println(
"ERROR. Usage: java " +
" ShapeListClient ServerName " +
"{Read|Write} Shape");
System.exit(1);
10
System.out.println("Stored shape");
}
at
h(RemoteEx
eption e) {
System.out.println("allShapes: " + e.getMessage());
}
at
h(Ex
eption e) {
System.out.println("Lookup: " + e.getMessage());
}
11
To
ompile and running the
lient you need some preliminary operations. First
of all, the
ompilation pro
ess depends on whether or not you are running the
lient on the same ma
hine where the Whiteboard server is running. If this is
not the
ase, you have to start rmiregistry (see step 3 above). Then, prepare a
dire
tory where you will store all the required les.
1. Copy in this dire
tory ShapeListClient.java, Graphi
alObje
t.java, Shape.java
and ShapeList.java (i.e. the two interfa
es), ShapeServant.java and ShapeListServant.java . Also, you will need the le mypoli
y (see above).
2. Compile the
lient: verb+java
ShapeListClient.java+
3. Compile ShapeServant.java and ShapeListServant.java (using java
).
4. Create the skeletons et
. for ShapeServant and ShapeListServant: rmi
ShapeServant and rmi
ShapeListServant (see above).
Now you are ready to run the
lient. Examples:
You should see something on the s reen of the server when a shape is reated.
12
Exer
ises
This part is for the interested students ;-). You are not required to
do these exer
ises, and you are not supposed to know all the details
below.
the Shape you want to remove. Hint: add the method to ShapeList.java,
and its implementation to ShapeListServant.java. In the implementation,
you may nd useful the following methods of Ve
tor: IndexOf(Obje
t), removeElementAt(int), equals(Obje
t). Also, you need to
hange the
lient
to a
ept a new request.
(This is not easy) If you import the relevant graphi al pa kages, you
13