Title: HANDOUT 14
1HANDOUT 14
- Remote Method Invocation (RMI)
THIS DOCUMENT CAN NOT BE REPRODUCED OR
DISTRIBUTED WITHOUT TH E WRITTEN PERMISSION OF
THE AUTHOR
BY GEORGE KOUTSOGIANNAKIS
2Remote Method Invocation (RMI)
- 1.0 DEFINITION
- THE REMOTE METHOD INVOCATION API ENABLES CLIENT
AND SERVER COMMUNICATIONS OVER THE NET - RMI ALLOWS JAVA (ONLY) OBJECTS , RESIDING ON THE
SAME OR DIFFERENT COMPUTERS ,TO COMMUNICATE WITH
ONE ANOTHER VIA REMOTE METHOD CALLS OVER THE
NETWORK. - RMI PROVIDES FOR TRANSFER OF OBJECTS VIA OBJECT
SERIALIZATION. - SERIALIZATION ALLOWS OBJECTS TO CARRY ALL THEIR
ATTRIBUTES (FIELDS) WITH THEM OVER THE NETWORK
BY GEORGE KOUTSOGIANNAKIS
3Remote Method Invocation (RMI)
- OBJECT SERIALIZATION IS USED TO SEND THE
ARGUMENTS (PROPERTIES) OF A METHOD INVOCATION
FROM THE CLIENT OBJECT TO THE REMOTE OBJECT - OBJECT SERIALIZATION IS ALSO USED TO RETURN THE
VALUES FROM THE SERVER OBJECT BACK TO THE CLIENT
OBJECT. - OBJECTS ARE CONVERTED INTO A STREAM OF BYTES
THAT CAN BE TRASMITTED OVER THE NETWORK BY USING
THE ObjectOutputStream CLASS. - OBJECTS ARE RECONSTRUCTED AFTER TRANSMISSION BY
USING THE ObjectInputStream CLASS.
BY GEORGE KOUTSOGIANNAKIS
4Remote Method Invocation (RMI)
- THE SERIALIZATION USED BY RMI IS TRANSPARENT TO
THE CLIENT AND SERVER OBJECTS. - APPLICATIONS USING TCP SOCKETS ARE RESPONSIBLE
FOR SERIALIZING AND DESERIALIZING OBJECTS VIA
INPUT AND OUTPUT STREAMS. - SECURITY IS AN ISSUE.
- JAVA OFFERS SECURITY VIA THE SECURITY PACKAGE.
- THE IMPLEMENTATION OF THE INTERFACE METHOD AT THE
SERVER HAS TO IMPLEMENT THE SERIALIZABLE
INTERFACE.
BY GEORGE KOUTSOGIANNAKIS
5Remote Method Invocation (RMI)
- TO OPEN A STREAM AND READ FROM AN INTERNET SITE
- BufferedReader in new BufferedReader ( new
InputStreamReader ( url.openStream ( ) ) - url is an object of the URL class that carries
the url of the site you want to connect (URL
implements serializable interface) - openStream ( ) is a method of the URL class that
Opens a connection to this URL and returns an
InputStream object for reading from that
connection. - InputStreamReader class constructor creates an
object that translates the InputStream bytes to
UNICODE characters. - The InputStreamReader object is now passed to
BufferedReader constructor that creates a stream
that is buffered so that one line at a time can
be read out of the buffer.
BY GEORGE KOUTSOGIANNAKIS
6Remote Method Invocation (RMI)
- LOCAL OBJECTS ARE OBJECTS THAT EXECUTE ON A
PARTICULAR HOST. - REMOTE OBJECTS ARE OBJECTS THAT EXECUTE ON ALL
OTHER HOSTS. - REMOTE OBJECTS HAVE TO BE EXPORTED.
- AN OBJECT ITSELF EXPORTS ITSELF BY REGISTERING
WITH A REMOTE REGISTRY SERVER. - RMI MAKES USE OF STUBS AND SKELETONS (1.2.2 USES
ONLY SKELETONS). - A STUB IS A LOCAL OBJECT THAT ACTS AS A PROXY FOR
A REMOTE OBJECT
BY GEORGE KOUTSOGIANNAKIS
7Remote Method Invocation (RMI)
- THE STUB PROVIDES THE SAME METHODS AS THE REMOTE
OBJECT. - LOCAL METHODS INVOKE THE METHODS OF THE STUB AS
IF THEY WERE METHODS OF THE REMOTE OBJECT. - THE SKELETON IS THE SAME AS THE STUB BUT ON THE
REMOTE HOST SIDE (SERVER).
BY GEORGE KOUTSOGIANNAKIS
8Remote Method Invocation (RMI)
- RMI AVOIDS THE NEED FOR PROGRAMMERS TO LEARN A
SEPARATE IDL (INTERFACE DEFINITION LANGUAGE). - SUPPORT FOR CROSS LANGUAGES IS VIA A NEW RELEASE
OF RMI CALLED RMI OVER IIOP (INTERNET INTER ORB
PROTOCOL) - Using RMI-IIOP, objects can be passed both by
reference and by value over IIOP. With RMI over
IIOP, developers can write remote interfaces in
the Java programming language and implement them
just using Java technology and the Java RMI APIs.
These interfaces can be implemented in any other
language that is supported by an OMG mapping and
a vendor supplied ORB for that language.
9Remote Method Invocation (RMI)
- The Remote Method Invocation (RMI) over Internet
Inter-Orb Protocol (IIOP) lets existing RMI code
reference and look up an object using the CORBA
CosNaming service. This gives you greater
interoperability between architectures. - A separate compiler is needed.
10Remote Method Invocation (RMI)
Default port 1099
Remote object registers its name
BY GEORGE KOUTSOGIANNAKIS
11Remote Method Invocation (RMI)
- BEFORE THE CLIENT IS STARTED THE RMI REGISTRY HAS
TO BE STARTED. - RMI REGISTRY IS A SERVER SIDE NAMING REPOSITORY
THAT ALLOWS REMOTE CLIENTS TO GET A REFERENCE TO
A REMOTE SERVER OBJECT. - ALSO THE SERVER PROGRAM HAS TO BE STARTED BEFORE
THE CLIENT.
BY GEORGE KOUTSOGIANNAKIS
12Remote Method Invocation (RMI)
- RMI IS IMPLEMENTED BY 5 PACKAGES
- java.rmi PROVIDES THE REMOTE INTERFACE, A CLASS
FOR ACCESSING REMOTE OBJECTS AND SECURITY MANAGER
FOR RMI. - Java.rmi.registry PROVIDES CLASSES AND INTERFACES
THAT ARE USED BY THE REMOTE REGISTRY. - java.rmi.server PROVIDES CLASSES AND INTERFACES
USED TO IMPLEMENT REMOTE OBJECTS AND STUBS AND TO
SUPPORT RMI COMMUNICATIONS. - java.rmi.activation SUPPORTS PERSISTENT OBJECT
REFERENCES AND REMOTE OBJECT ACTIVATION - java.rmi.dgc SUPPORTS RMI DISTRIBUTED GARBAGE
COLLECTOR.
BY GEORGE KOUTSOGIANNAKIS
13Remote Method Invocation (RMI)
- EXAMPLE OF AN RMI APPLICATION (TemperatureServerIm
pl) - LET US DEVELOP AN APPLICATION THAT WILL MONITOR
THE WEATHER INFORMATION OVER THE INTERNET FROM
THE WEATHER BUREAU WEB SITE TWICE A DAY. - THE APPLICATION , FOR THE SHAKE OF THE
PRESENTATION, WILL RUN LOCALLY (I HAVE COPIED THE
PAGE Traveler.html OF THE SITE TO THE WWWROOT
DIRECTORY IN MY LAPTOP). - IT IS ASSUMED THAT WE HAVE A LOCAL NETWORK. THE
SERVER GETS THE WEATHER INFORMATION FROM THE
INETRNET AND THE CLIENTS OF THE LOCAL NETWORK CAN
ACCESS THE INFORMATION FROM THE SERVER VIA REMOTE
METHOD INVOCATION
BY GEORGE KOUTSOGIANNAKIS
14Remote Method Invocation (RMI)
- 4.0 SUMMARY OF TEMPERATURESERVER EXAMPLE
15 Remote Method Invocation (RMI)
- 3.0 IMPLEMENTING THE RMI APPLICATION
- AT THE SERVER SIDE
- REMOTE OBJECTS ARE REFERENCED VIA INTERFACES. YOU
CREATE AN INTERFACE FOR EACH OBJECT. - THE INTERFACE MUST BE PUBLIC AND MUST EXTEND
INTERFACE Remote . - DEFINE THE REMOTE METHODS THAT YOU WANT TO INVOKE
WITHIN THE INTERFACE. THE METHODS MUST THROW
RemoteException . - IN OUR EXAMPLE THERE THE INTERFACE is
- public interface TemperatureServer extends Remote
- (See code)
BY GEORGE KOUTSOGIANNAKIS
16Remote Method Invocation(RMI)
- The interface will invoke method getWeatherInfo()
which returns an array of objects. - Extending the interface Remote implies that our
objects can be remotely accessed by any JVM that
has network connection to the server and the
appropriate stubs. - CREATE A CLASS THAT IMPLEMENTS THE INTERFACE
(TemperatureServerImpl). - THE CLASS EXTENDS UnicastRemoteObject CLASS
(SUBCLASS OF RemoteServer CLASS). (SEE
TemperatureServerImpl extends UnicastRemoteObject
implements TemperatureServer, Runnable) - UnicastRemoteObject CLASS ALLOWS THE REMOTE
OBJECT TO WAIT FOR A CLIENT CONNECTION ON AN
ANONYMOUS PORT NUMBER.
17Remote Method Invocation (RMI)
- THE CLASS SHOULD HAVE A CONSTRUCTOR THAT CREATES
, INITIALIZES THE REMOTE OBJECT - IT SHOULD IMPLEMENT ALL THE METHODS DEFINED IN
THE REMOTE INTERFACE (SEE public WeatherInfo
getWeatherInfo () ) - IT SHOULD HAVE A main METHOD THAT CAN BE EXECUTED
AS A REMOTE CLASS. - IT SHOULD REGISTER A NAME BY WHICH IT CAN BE
REMOTELY REFERENCED WITH THE REMOTE REGISTRY. (
SEE String serverObjectName //localhost/TempServ
er ) - THE NAME OF THE OBJECT USED BY THE MAIN SHOULD BE
BOUND TO THE NAME USED BY THE CLIENT. (SEE
Naming.rebind ( serverObjectName, temp) ). - USE rmic COMPILER TO CREATE A STUB AT THE SERVER
- rmic -v1.2 TemperatureServerImpl.
- THIS COMMAND GENERATED THE FILE
TemperatureServerImp_stub.class
BY GEORGE KOUTSOGIANNAKIS
18Remote Method Invocation (RMI)
- THIS FILE MUST BE AVAILABLE TO THE CLIENT
- START THE REMOTE REGISTRY SERVER. THIS PROGRAM
LISTENS ON THE DEFAULT PORT 1099 FOR INCOMING
REQUESTS TO ACCESS NAMED OBJECTS. ON THE COMMAND
WINDOW TYPE - start rmiregistry
- LEAVE REGISTRY SERVER RUNNING.
- BOUND REMOTE SERVER OBJECT TO THE REGISTER. RUN
THE COMPILED OBJECT IMPLEMENTATION CLASS - java TewmperatureServerImpl
- This is a server and shpuld be left running
- AT THE CLIENT
- WRITE A CLIENT PROGRAM THAT WILL CALL THE REMOTE
OBJECT ( THE IMPLEMENTATION OF THE INTERFACE).
BY GEORGE KOUTSOGIANNAKIS
19Remote Method Invocation (RMI)
- THE IP ADDRESS OF THE MACHINE THAT THE REMOTE
OBJECT RESIDES SHOULD BE USED (OR THE REMOTE
HOSTS NAME). - Naming.lookup ( serverObjectName) IS USED TO
INTERACT WITH THE RMI REGISTRY TO HELP THE CLIENT
OBTAIN A REFERENCE TO THE REMOTE OBJECT. - A COPY OF THE INTERFACE STUB SHOULD BE COPIED TO
THE SAME DIRECTORY AS THE CLIENT PROGRAM. - Start the client prgram by calling
- java TemperatureClient
BY GEORGE KOUTSOGIANNAKIS
20Remote Method Invocation (RMI)
SERVER
CLIENT
TemperatureServer.class temperatureServerImpl.cla
ss WeatherInfo.class TempeartureServerImpltimeDel
ay.class
Rmi registry
TemperatureClient.class Weatheritem.class Temperat
ureServerImpl_Stub.class TemperatureClient1.class
BY GEORGE KOUTSOGIANNAKIS
21Remote Method Invocation (RMI)
1) gt (TemperatureClient) passes ip address of
server or uses localhost 2) remote method call to
object TempServer 3) TemperatureServerImpl_Stub r
eceives method call and passes it to the java
RMI system which performs the network
connections 4) requets a reference to
TempServer object from rmi registry
1)rmi registry server is running 2)temp object
calls UnicastRemoteObject which exports the
remote object 3)name of remote object TempServer
is bound to registry 4)updateWeatherConditions
method stores data from internet site into
WeatherInfo objects (array) 5) client calls
getWeatherInfo method via the interface which in
turn returns a reference to theWeatherInfo
array. 6) array objects are passed to client.
BY GEORGE KOUTSOGIANNAKIS