Title: Creating a Distributed System with RMI
1Creating a Distributed System with RMI
2Remote Method Invocation
- Remote Method Invocation (RMI) is Javas
implementation of object-to-object communication
among Java objects to realize a distributed
computing model. - RMI allows us to distribute our objects on
various machines, and invoke methods on the
objects located on remote sites. - Source code for the demo is a modified version of
code in Chapter 20 of Deitel Deitels text Java
How to Program.
3RMI-based Distributed System
4.
4Steps in RMI-based Application
- 1. Design the interface for the service.
- 2. Implement the methods specified in the
interface. - 3. Generate the stub and the skeleton.
- 4. Register the service by name and location.
- 5. Use the service in an application.
5Compile and Register Commands
rmiregistry
Stores object by name
Finds object by name
rmic
2.
3.
3.
5.
XYZ Client
Skeleton
XYZ Implementation
Stub
1.
uses
implements
XYZ interface
Client Host
Server Host
6More Details
- Once the object (or service) is registered, a
client can look up that service. - A client (application) receives a reference that
allows the client to use the service (call the
method). - Syntax of calling is identical to a call to a
method of another object in the same program.
7Parameter Marshalling
- Transfer of parameters (or marshalling) is done
by the RMI. - Complex objects are streamed using Serialization.
- RMI model of networking for distributed system
involves only Java. - No need to learn IDL or any other language.
8Case Study Temperature Service
- Lets create a distributed system using RMI model
for networking (remote access). - Basically this program will download the weather
(temperature) information from the site - http//iwin.nws.noaa.gov/iwin/us/traveler.html
9Temperature Client/Server Distributed Application
4
rmic
compiles
2.
3.
3.
5.
generates
TempImpl_Stub
TempImpl_Stub
uses
TempClient
TempServerImpl
1.
uses
implements
TempServer
Client Host
Server Host
10Defining Remote Interface
- import java.rmi.
- // the interface extends Remote interface
- // any class implementing Remote can be accessed
remotely security permitting - public interface TemperatureServer extends Remote
- // specify methods that can be called remotely
- // each method throws RemoteException
11RemoteException
- Any time you depend on outside entities there is
a potential for problems in communication,
networking, server crash etc. - Any exception due to these should be handled by
the services. - This feature imparts robustness to the
application. - Java mandates this feature for any RMI service.
12Implementing the Remote Interface
- import java.rmi.
- import java.rmi.server.
- import java.net.
- // others as needed
- TemperatureServerImpl
- extends UnicastRemoteObject implements
TemperatureServer
13TemperatureServerImpl
- This classs constructor calls a private method
which in turn - 1. Connects to the url specified
- 2. Streams into a buffer the page referenced.
- 3. Parses the buffer to get the required data.
- 4. Creates an array of weather information.
14TemperatureServerImpl (contd.)
- It implements the service method getWeatherInfo
which simply returns the weather data gathered. - The main method instantiates an object for the
service, and registers it with rmiregistry.
15Streaming URLs
- Using the openStream of java.net.URL class you
can stream in the file spefied by an universal
resource locator(url). - It can be streamed into a buffer where it can be
analyzed for information. - Any number of urls can be streamed in.
- Unicast Communication When you are interested
in a particular remote site you will direct your
net connection to that particular site using
unicast.
16Server Object Name
- Syntax for the server object name is
- //hostport/remoteObjectName
- Default port number for rmiregistry is 1099
- For local host the object name
- //localhost/TempServer
- For a remote host
- //127.0.0.1/TempServer
17Name Binding
- rebind method binds a servers object name to the
objects name as it is in the registry. - Clients use the name in the registry.
- There is also a bind() method.
- But rebind is better since it binds the most
recently registered object.
18WeatherInfo class
- It is very traditional class for keeping the
information about the temperature at a single
location. - It has data fields cityName, temperature, and
description and get methods for these. - An array of objects of this class is used in the
server implementation.
19Temperature Client
- import java.rmi.
- // import other packages
- constructor calls a private method getRemoteTemp
which takes care of lookup of remote object and
access. - In this application it also displays the
information.
20Temperature Client (contd.)
- The main method in this client can get the IP
address of the remote host as a command line
argument. - Command line argument is an array of String of
items in the command line after the name of the
application.
21Client Details
- The name of the server object along with the IP
of the remote location is used in Naming classs
lookup method to get an object reference. - This object reference is then used for remote
method calls. - Observe that there is no difference between the
local and remote call. - WeatherItem class used in the Graphical display
of the weather information.
22Preparing the Application
- 1. Compile all the class using javac.
- 2. Generate the stub and the skeleton
- rmic -v1.2 TemperatureServerImpl
- 3. Then start the registry (this will be running
as a daemon) - rmiregistry
23Preparing the Application
- 4. Run the server which will register with the
RMI registry. - Java TemperatureServerImpl
- 5. Run the client.
- Java TemperatureClient
- or
- java TemperatureClient IPAddress
- java TemperatureClient 192.168.0.150
24Inside RMI
- http//java.sun.com/j2se/1.5.0/docs/index.html
- Basic RMI classes /usr/java1.1/src/java/rmi
- java.rmi.registry.
- java.rmi.Naming class (static/class methods)
- java.rmi.Remote interface (marker interface)
- java.rmi.server.
- Default RMI port 1099
- Both lookup from local and remote are acceptable.
25Implementation of RMI (5.2.5)
- AccessException.java
- RemoteException.java
- AlreadyBoundException.java
- ConnectException.java
- ServerException.java
- ConnectIOException.java
- ServerRuntimeException.java
- MarshalException.java
- StubNotFoundException.java
- UnexpectedException.jav
- ServerError.java
- UnknownHostException.java
- NoSuchObjectException.java
- UnmarshalException.java
- NotBoundException.java
- RMISecurityException.java
- RMISecurityManager.java
- Remote.java
- MarshalledObject.java
- Naming.java
- activation
- dgc
- Registry
- server
26The role of proxy and skeleton in remote method
invocation
server
client
remote
skeleton
object B
object A
proxy for B
Request
dispatcher
for Bs class
Reply
servant
Communication
Remote reference
Communication
Remote
module
module
module
reference module
Object A invokes a remote object in Object B for
which it holds a remote object reference. System
Model
27RMI Internals Communication Module
- Carries out request-reply protocol
- On the client side message type, message id,
remote reference to object are gathered and sent
out. At most once invocation semantics - On the server side, it gets local reference for
remote reference from remote reference module,
invokes a dispatcher with this reference. - See UnicastRemote (implements UnicastRemote)
28RMi Internals Remote Reference module
- Responsible for translating between local and
remote object references and for creating remote
object references. - A remote object table has a mapping between local
and remote references. A table at server (entry
for object ref for B) and a table at client
(entry for object ref for proxy B).
29RMI Internals Remote References
- Action of remote reference module See
RemoteRef.java interface - When a remote object is to be passed as argument
or result for the first time, the remote ref is
asked to create a remote ref object which is
added to the table. - When a remote object reference arrives in a
request or reply, the remote ref module is asked
for corresponding local object ref, which may
either a proxy or remote object. If it is not in
the table RMI runtime creates it and asks remote
ref module to add it to the table.
30RMI Internals RMI software
- Layer of software between application level
objects and communication and remote reference
modules Middleware - Proxy provides remote access transparency. One
proxy for every remote object in the client. - Dispatcher A server has one dispatcher and
skeleton for each class representing a remote
object. - It receives request message from comm. Module
- It used MessageId to select appropriate method in
skeleton. - Proxy and dispatcher use same MessageId.
- Skeleton A class of remote object has a skeleton
that implements of the remote interface. All the
access dependencies are hidden in this class. A
remote object has a servant that directly
implements the methods. Java 5 creates this
dynamically. - Proxies, dispatcher and skeleton are
automatically generated by interface compiler. - Binder binds textual names to remote object
references. RMIRegistry is a binder Naming
class see fig.5.13 - Server Threads one thread per invocation
- Distributed garbage collection See Andrew
Birells paper 1995.
31RMI Internals Distributed Garbage Collection
- Based on reference counts.
- Local garbage collectors and a distributed
support. - Each server holds the list of processes that hold
remote object references for example, B.Holders - When a client C first receives a remote reference
to a particular remote object, say B, it makes a
addRef(B) invocation to server of that remote
object and then creates proxy server adds C to
B.Holders. - When client Cs garbage collector finds that
proxy is no longer reachable (ref count), it
makes a removeRef(B) invocation to server and
then deletes proxy the server removes C from
B.Holders. - When B.Holders is empty, servers local garbage
collector will reclaim the space occupied B
unless there are any local holders. - These extra calls for updates occur during proxy
creation and deletion and do not affect normal
opertion. - Tolerates communication failures addRef() and
removeRef() are idempotent effects of N gt 0
identical requests is the same as for a single
request. - If addRef() fails with an exception, proxy is not
created, removeRef() is transmitted removeRef()
failures are dealt with by leases (Jini kind).
32RMI Internals Use of Reflection
- What is reflection? See Reflection package
- Reflection enables Java code to discover
information about the fields, methods and
constructors of loaded classes, and - To use reflected fields, methods, and
constructors to operate on their underlying
counterparts on objects, within security
restrictions. - http//java.sun.com/docs/books/tutorial/reflect/cl
ass/index.html - Reflection feature allowed for dynamic creation
of skeleton and proxy in Java 2 version onwards. - Read more about reflection model of computing.
33A Little bit of Reflection
- Method class, invoke method
- Invoke method requires two parameters first the
object to receive invocation, second an array of
Object parameters. - Invoke executes the method on the object and
returns result as Object. - Method m
- Object result M.invoke(String, Args)
34Using Reflection in RMI
- Proxy has to marshal info. about a method and its
arguments into a request message. - For a method it marshals an object of class
Method into the request. It then adds an array of
objects for the methods arguments. - The dispatcher unmarshals the Method object and
its arguments from request message. - The remote object reference is obtained from
remote ref. table. - The dispatcher then calls the invoke method on
the object reference and array of arguments
values. - After the method execution the dispatcher
marshals the result or any exceptions into the
reply message.
35Summary
- We discussed designing a distributed system using
RMI - We also looked at RMI internal
- We also learned about marker interface,
distributed garbage collection, object
marshalling, registry, server-port binding,
Naming class of RMI,