Remote Objects and RMI

handprintSoftware and s/w Development

Nov 18, 2013 (3 years and 11 months ago)

81 views

Topic 15



Remote Objects and RMI

“Dealing with more than one client at a time is the business world’s

equivalent of bigamy. It’s so awkward to tell one client that you’re

working on someone else’s business that you inevitably start lying.”

CISC370/Object Oriented
Programming with Java

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Use and Distribution Notice

Possession of any of these files implies understanding and
agreement to this policy.

The slides are provided for the use of students enrolled in Jeff
Six's Object Oriented Programming with Java class (CISC 370) at
the University of Delaware. They are the creation of Mr. Six and
he reserves all rights as to the slides. These slides are not to be
modified or redistributed in any way. All of these slides may only
be used by students for the purpose of reviewing the material
covered in lecture. Any other use, including but not limited to, the
modification of any slides or the sale of any slides or material, in
whole or in part, is expressly prohibited.

Most of the material in these slides, including the examples, is
derived from multiple textbooks. Credit is hereby given to the
authors of these textbook for much of the content. This content
is used here for the purpose of presenting this material in CISC
370, which uses, or has used, these textbooks.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Java and Remote Objects

Distributed programming

refers to objects on
the same or separate computers
communicating with each other via
remote
method calls
.

Basically, this allows an object on one
computer to call methods of an object that
exists on a different computer, and this
method call looks just like a method call of a
local object.

This is done in Java through the use of
Remote Method Invocation (RMI)
.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Remote Method Invocation

RMI takes care of all of the network
communications and the
marshalling

of the
data (this is the packaging of the method
arguments and return values for transmission
over the network).

This system (RMI) only allows remote method
calls between two Java objects.
Communication with objects written in other
languages is not yet possible with RMI.

This is in contrast to socket level
programming in that no network coding is
necessary.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Deploying RMI Applications

There are four major steps in deploying RMI
-
based objects…


Define a
remote interface

that defines the remote
methods that client will use to interact with the
remote server object.


Define the server application that implements the
remote interface.


Define a client application that uses a
remote
interface reference

to call the remote methods of
the remote server object.


Compile and execute the server and client.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining the Remote Interface

A remote interface describes the
remote
methods

that the client can call on the server
object (how the client will interact with the
server).

To create a remote interface, define an
interface that extends the
Remote

interface.

An object that implements this interface (or a
derived interface) is a
remote object
; it can
be accessed from any JVM that can connect
to the computer on which the remote object
resides.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining the Remote Interface

As an example, suppose we wish to make a
remote object that has one remote method,
getGrade().

A client can call this method, passing it a
student ID and it will return that student’s
current grade, as a character.

The remote interface for such a server would
look like…

import java.rmi.*;

public interface GraderServer extends Remote {


public char getGrade(int student_id)



throws RemoteException;

}

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining the Remote Interface

A class that implements this interface must have
a getGrade() method that is remote; a client will
be able to call this method using RMI.

Any remote method can throw a
RemoteException

if a communications
problem occurs during the remote method call.
This must be specified in the interface.

import java.rmi.*;

public interface GraderServer extends Remote {


public char getGrade(int)



throws RemoteException;

}

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Implementing the

Remote Interface

Once the remote interface has been defined, a
class that implements this interface (the actual
server object class) must be defined.

Typically this class has the same name as the
interface, with a
Impl

suffix (standing for
Implementation).

A client will interact with a
GraderServerImpl

class object by calling the method(s) defined in
the GraderServer interface.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

public class GraderServerImpl extends UnicastRemoteObject


implements GraderServer

{


public GraderServerImpl()


{ grade_table = new Hashtable(); }


public update_grade(int student_id, char grade)


{ . . . }



public char getGrade(int student_id)



throws RemoteException


{ return grade_table.getValue(student_id); }



public static void main(String args[])



throws Exception


{



GraderServerImpl temp = new GraderServerImpl();



String serverObjName = “rmi://localhost/GraderServer”;



Naming.rebind(serverObjName, temp);


}


Hashtable grade_table;

}

CISC370


Object Oriented Programming with Java / © 2003 J. Six

The

UnicastRemoteObject Class

This implementation class extends the
UnicastRemoteObject

class. This class
encapsulates all of the basic functionality of
remote objects.

Specifically, it
exports

the object to make it
available to receive remote method calls.

Remember, RMI handles all of the
communications between the server and its
clients. All the server needs to do is extend this
class to make it available to receive client
method calls.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

The main() Method

of the Server

The main() method of this server class first
creates a new object of the GraderServer
class. It then constructs a server object
name.

This is the name of the server object that will
be used by the client to attempt to connect to
the server. It normally follows a URL form:



where
host

represents the computer that the
registry for remote objects

is running on.

rmi://host/remoteObjectName

CISC370


Object Oriented Programming with Java / © 2003 J. Six

The Registry

for Remote Objects

The registry for remote objects (
rmiregistry
) is
a program/service that runs on the same
computer as the server object. It’s job is to
allow clients to connect to it and inquire about
a particular server object.

It will then tell the client how to connect to that
particular server object.

So, in our example, the server object name
“rmi://localhost/GraderServer” means that the
same machine as the GraderServer will contain
a registry and this object’s name is
GraderServer.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Binding to the Registry

After this name has been created, the main()
method calls the static method rebind() of the RMI
Naming class.


This
binds

the remote server object temp to the
remote object registry and gives the remote object
the name “rmi://localhost/GraderServer”.

Thus, when a client connects to this computer
(which contains the registry and the server object)
and requests “rmi://localhost/GraderServer”, it gets
the temp GraderServerImpl
-
class object..

Naming.rebind( serverObjName, temp );

CISC370


Object Oriented Programming with Java / © 2003 J. Six

public class GraderServerImpl extends UnicastRemoteObject


implements GraderServer

{


public GraderServerImpl()


{ grade_table = new Hashtable(); }


public update_grade(int student_id, char grade);


{ . . . }



public char getGrade(int student_id)



throws RemoteException


{ return grade_table.getValue(student_id); }



public static void main(String args[])



throws Exception


{



GraderServerImpl temp = new GraderServerImpl();



String serverObjName = “rmi://localhost/GraderServer”;



Naming.rebind(serverObjName, temp);


}


Hashtable grade_table;

}

This encapsulates all of the basic

remote object functionality, and is

required for all remote classes.

This specifies the remote

interface this class implements.

Note the name resemblance.

Here is the remote method.

Construct the name of this server

object. This is the name a client

will use to request this object.

This binds the server name to the

GraderServerImpl object.

When a client connects to the registry

on this computer and requests the

server name, it will get the temp object.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining The Client

Now, the remote interface has been defined
(GraderServer) and implemented
(GraderServerImpl).

We need a client to make remote method
calls on our GraderServerImpl object.

This class will make a call to the getGrade()
remote method of the server object using
RMI. This method call will look just like a call
to a local object...all the network connectivity
is handled by RMI.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

public class GraderClient {


. . .


private void process()


{


int current_id;


try {


String serverObjName = “rmi://” + server_ip


+ “/GraderServer”;


GraderServer mytemp = (GraderServer)


Naming.lookup( serverObjName );




for (current_id = 0; current_id < MAX_ID;


current_id++)


{ char grade = mytemp.getGrade(current_id);


System.out.println(current_id + “ has a grade of ”


+ grade + “.”);


}


} catch (java.rmi.ConnectException ce) {


System.out.println(“Connection to server failed.”);


} catch (Exception e) {


e.PrintStackTrace(); exit(1)


}

}


CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining the Client

First, the process() method of this class
constructs the server object name. This
is the same as the name constructed in
the server object.

Then the method calls the static
lookup() method of the Naming RMI
class, passing it the server object name.
This method will contact the remote
object registry on the host specified in
the name and lookup the remote object
on that server with the specified name.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining the Client

This will return an Remote
-
class object
reference, as the registry does not known what
class each of the remote server objects belong
to. Since the client knows it is a GraderServer
object (an object that implements the
GraderServer interface), the appropriate cast is
made.

The client class now has a
remote object
reference
, a reference to an object on another
computer! It can use this reference to access
any of the remote methods defined in the
GraderServer interface.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Defining the Client

This method call is made just like a
method call on a local object…the only
difference is that when this method call
is made, RMI connects to the server
computer, sends the method
parameters, locates the remote server
object, calls the method, sends the
return value back to the client computer,
and then passes the return value back to
the calling client program.

All of this is done automatically by RMI!

CISC370


Object Oriented Programming with Java / © 2003 J. Six

public class GraderClient {


. . .


private void process()


{


int current_id;


try {


String serverObjName = “rmi://” + server_ip


+ “/GraderServer”;


GraderServer mytemp = (GraderServer)


Naming.lookup( serverObjName );




for (current_id = 0; current_id < MAX_ID;


current_id++)


{ char grade = mytemp.getGrade(current_id);


System.out.println(current_id + “ has a grade of ”


+ grade + “.”);


}


} catch (java.rmi.ConnectException ce) {


System.out.println(“Connection to server failed.”);


} catch (Exception e) {


e.PrintStackTrace(); exit(1) }

}




Builds the server object name

(same name as at the server)

Calls the lookup() method of the

Naming class. This returns a remote

objectreference to the remote object,

specified by the name parameter.

Now, any of the remote methods of

the remote server object can be

called just like methods of a local

object, without any additional

coding. (RMI is great…)

Must catch the ConnectException that

will occur if the Naming class cannot

connect to the specified server.

Other exceptions can also occur, so

the client needs to catch them too.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Compiling and Executing

Now that all of the pieces are in place,
we need to build and execute our
distributed application. First, all of the
classes (in this case, GraderServerImpl
and GraderClient) must be compiled in
the normal fashion using javac.

Next, the remote server class
(GraderServerImpl) must be compiled
using the
rmic

compiler. This produces a
stub class
.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

The RMI Stub Class

The stub lives on the client computer. It
receives the parameters for the method,
encodes/marshals them, and sends this block
of data to the server.

On the server, a receiver object (called a
skeleton) un
-
marshals the parameters,
locates the correct object and its method,
calls the desired method with the parameters,
captures and marshals the return value or
exception and then sends this marshaled data
back to the client stub.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Java 1.5 and

the RMI Compiler

Java 1.5 introduces the concept of
dynamic
proxies
, whereby the stub is generated at
runtime on the client.

There is no need to run the RMI compiler
(rmic) if both the RMI server and client are
compiled on and run on a 1.5 JVM.

This is only true is everything (client and
server) are running in a Java 1.5
environment. If the client (any client) is
running on a pre
-
1.5 JVM, the stubs must still
be generated and provided to the client.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Information Flow in a RMI Call

The stub then passes the return value or
exception back to the client class.

This process seems quite complex,
because it is.

However, it is completely automatic, and
for the most part, transparent to the
programmer. Java RMI makes remote
objects have very similar look
-
and
-
feel as
local objects.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Information Flow in a RMI Call

Client

Class

Stub

Server’s

Skeleton

Server

Class

(1)
Client calls

server stub

locally

(2) Stub passes call

to RMI, which

sends marshalled

data to server

(3) Skeleton receives

call/data and calls

the local method

(4) Skeleton sends marshalled data

or exception back to client

(5) Stub returns data

or exception

CLIENT

SERVER

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Compiling and Executing

To create this stub class, run the rmic compiler on
the server class file (GraderServerImpl) with the
parameter

v1.2 (to denote Java 1.2)…


This will create a file with the name
GraderServerImpl_Stub.class. This file must be
available to the client (GraderClient) to enable
client/server communication.

Note that the actual server class
(GraderServerImpl.class) does not have to be
available to the client.

rmic

v1.2 GraderServerImpl

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Starting the RMI Registry

Next, the RMI registry must be started on the server
computer. This will enable the server class
(GraderServerImpl) to register itself and allow the
client to connect to it.

To do this, simply type
rmiregistry

at the
command line. This launches the RMI registry. It
listens for incoming RMI requests (the default port
for the registry is TCP port 1099) and processes
them.

If you want to start more than one rmiregistry at
once, you can provide an alternate port on the
command line (if you do this, you must specify this
port in the server URL on both the client and the
server).

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Starting the Server and Client

Finally, start the server in the usual way,
java GraderServerImpl
. This will run
the main() method of that class, which
creates a GraderServerImpl object and
registers it with the RMI registry (which must
already be running). Now, the server is up
and ready to receive remote method calls.

Start the client, with
java GraderClient
.
When the program executes, it will connect
to the remote server object for the remote
method call.

CISC370


Object Oriented Programming with Java / © 2003 J. Six

Distributed Java Programming
with RMI

Notice that the client still needs to know the IP
address (or hostname) of the server on which it
wants to make remote method calls.

That is the only networking details such a
distributed application needs to worry
about…all communication details, including the
marshalling of the data, is handled
automatically by Java RMI.

This feature makes distributed programming in
Java quite simple.