Download remote interface

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 4
Remote Method Invocation
From Coulouris, Dollimore, Kindberg and Blair
Distributed Systems:
Concepts and Design
Remote Method Invocation
Remote Method Invocation (RMI) is Java’s
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.
2
5/6/2017
The role of proxy and skeleton (stub) in remote
method invocation
server
client
object A proxy for B
Request
skeleton
& dispatcher
for B’s class
remote
object B
Reply
Remote Communication
reference module module
servant
CommunicationRemote reference
module
module
Object A invokes a remote object in Object B for which it holds a remote object
reference.
3
5/6/2017
RMI Internals: Client side stub
The stub is an object that resides at the client side and
represents the remote object. When the caller
invokes method on the stub object, it does the
following tasks:
 It initiates a connection with remote Virtual Machine (JVM),
 It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
 It waits for the result
 It reads (unmarshals) the return value or exception, and
 It finally, returns the value to the caller.
4
5/6/2017
RMI Internals: Server side stub/skeleton
When the skeleton receives the incoming request, it
does the following tasks:
 It reads the parameter for the remote method
 It invokes the method on the actual remote object, and
 It writes and transmits (marshals) the result to the caller.
5
5/6/2017
RMI 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.
6
5/6/2017
RMI 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).
7
5/6/2017
RMI-based Distributed System
4.
Finds object by name
5.
XYZ
Client
rmiregistry
rmic
Compile
3.
1.
XYZ
interface
Client Host
3.
2.
XYZ
Implementation
Stub
Stub
uses
Stores object by name
implements
Server Host
8
5/6/2017
Steps 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.
9
5/6/2017
More 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.
10
5/6/2017
Parameter 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.
11
5/6/2017
Java RMI





Define the remote interface
Implement the server
Implement the client
Compile the source files
Start the Java RMI registry, server, and client
Take a look at:
http://download.oracle.com/javase/6/docs/technotes/gu
ides/rmi/hello/hello-world.html
12
5/6/2017
Defining Remote Interface




Import java.rmi.*;
The interface extends Remote interface
Specify methods that can be called remotely
Each method “throws RemoteException”
13
5/6/2017
RemoteException
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.
14
5/6/2017
Implementing the server
import java.rmi.*;
import java.rmi.server.*;
import java.net.*;
// others as needed
XYZImpl
implements TemperatureServer {
The main method: (1) creates and exports an object for
the service, and (2) registers it with rmiregistry.
15
5/6/2017
Create and export an object
16
5/6/2017
UnicastRemoteObject
UnicastRemoteObject is a base for your remote object
that provides simple transient point-point RMI
servers.
 Exports the supplied remote object to receive incoming remote method
invocations on an anonymous TCP port and returns the stub for the
remote object to pass to clients.
 The returned stub implements the same set of remote interfaces as the
remote object's class and contains the host name and port over which the
remote object can be contacted.
17
5/6/2017
Register the remote object
18
5/6/2017
Java RMI Registry
Java RMI provides a registry API for applications to
bind a name to a remote object's stub and for clients
to look up remote objects by name in order to obtain
their stubs.
 A Java RMI registry is a simplified name service that allows
clients to get a reference (a stub) to a remote object.
 Once a remote object is registered on the server, callers can
look up the object by name, obtain a remote object reference,
and then invoke remote methods on the object.
19
5/6/2017
Name Binding
rebind method binds a server’s object name to the
object’s 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.
20
5/6/2017
Server Object Name
Syntax for the server object name is:
rmi://host:port/remoteObjectname
Default port number for rmiregistry is 1099
For local host the object name:
rmi://localhost:1099/XYZServer
For a remote host
rmi://127.0.0.1:1099/XYZServer
e.g.,
Serverside: registry.rebind("rmi://localhost:5000/hello",stub);
Clientside: xxx stub=(xxx)registry.lookup("rmi://localhost:5000/hello");
21
5/6/2017
Implement the client
22
5/6/2017
Client Details
The name of the server object along with the IP of the
remote location is used in Naming class’s 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.
23
5/6/2017
After invoke sayHello()
 The clientside runtime opens a connection to the
server using the host and port information in the
remote object's stub and then serializes the call
data.
 The serverside runtime accepts the incoming call,
dispatches the call to the remote object, and
serializes the result (the reply string "Hello, world!")
to the client.
 The clientside runtime receives, deserializes, and
returns the result to the caller.
24
5/6/2017
Compile the source files
Compile all the class using javac.
e.g., javac d destDir Hello.java Server.java Client.java
Note: If the server needs to support clients running on
pre5.0 VMs, then a stub class for the remote object
implementation class needs to be pregenerated using
the rmic compiler, and that stub class needs to be
made available for clients to download.
25
5/6/2017
Preparing the application (Windows system)
1. Start the registry (this will be running as a daemon)
start rmiregistry
*The default port number is 1099
2. Start the server
3. Run the client
26
5/6/2017
Inside RMI
http://download.oracle.com/javase/6/docs/
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.
27
5/6/2017
Implementation 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
28
5/6/2017
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 RemoteStub for each class
representing a remote object.
 It receives request message from comm. module
 It used MessageId to select appropriate method in RemoteStub.
RemoteStub and dispatcher use same MessageId.
RemoteStub: A class of remote object has a that stands for 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.
Binder: binds textual names to remote object references. RMIRegistry is a
binder; Naming class; see fig.5.13 We will discuss this next.
Server Threads: one thread per invocation
Distributed garbage collection: See Andrew Birell’s paper [1995].
29
5/6/2017
Simple RMI System Model
Host M/C
Registry
Client M/C
JVM
server
JVM
server
ClientApp
Remote interface
30
5/6/2017
Registry (singleton – per system - during production)
Name
Reference
Mortgage
Tempertur
31
5/6/2017
Serialization
Serialization is a the operation of encoding objects into stream
of bytes: bytification.
De-serialization is taking the bytes and constructing objects out
of the byte stream.
Is used to marshal and unmarshal arguments and results.
If serialization is not addressed your distributed system will not
work: whether it be your mobile systems or land networked
system. A stumbling block for many who are new to DS.
32
5/6/2017
Serialization (contd.)
In order for an object to be passed around in a DS it
must implement Serializable interface (another
marker interface: Remote was the first one we saw)
For performance reasons you may want to specify the
serialization version (though not required)
serialVersionUID
33
5/6/2017
Reference
For more details, check the link below
http://download.oracle.com/javase/6/docs/technotes/gu
ides/rmi/index.html
34
5/6/2017