Download presentation
Presentation is loading. Please wait.
1
Chapter 4 Remote Method Invocation
From Coulouris, Dollimore, Kindberg and Blair Distributed Systems: Concepts and Design
2
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. 4/25/2018
3
The role of proxy and skeleton (stub) in remote method invocation
server client remote skeleton object B object A proxy for B Request & dispatcher for B’s class Reply servant Communication Communication Remote reference Remote module reference module module module Object A invokes a remote object in Object B for which it holds a remote object reference. 4/25/2018
4
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/25/2018
5
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. 4/25/2018
6
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. 4/25/2018
7
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). 4/25/2018
8
RMI-based Distributed System
4. rmiregistry Finds object by name Stores object by name rmic Compile 5. 3. 3. 2. XYZ Client Stub XYZ Implementation Stub 1. uses implements XYZ interface Client Host Server Host 4/25/2018
9
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. 4/25/2018
10
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. 4/25/2018
11
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. 4/25/2018
12
Define the remote interface Implement the server Implement the client
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: ides/rmi/hello/hello-world.html 4/25/2018
13
Defining Remote Interface
Import java.rmi.*; The interface extends Remote interface Specify methods that can be called remotely Each method “throws RemoteException” 4/25/2018
14
Any exception due to these should be handled by the services.
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. 4/25/2018
15
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. 4/25/2018
16
Create and export an object
4/25/2018
17
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. 4/25/2018
18
Register the remote object
4/25/2018
19
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. 4/25/2018
20
Clients use the name in the registry. There is also a bind() method.
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. 4/25/2018
21
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:// :1099/XYZServer e.g., Serverside: registry.rebind("rmi://localhost:5000/hello",stub); Clientside: xxx stub=(xxx)registry.lookup("rmi://localhost:5000/hello"); 4/25/2018
22
Implement the client 4/25/2018
23
This object reference is then used for remote method calls.
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. 4/25/2018
24
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. 4/25/2018
25
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. 4/25/2018
26
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 4/25/2018
27
Basic RMI classes: /usr/java1.1/src/java/rmi java.rmi.registry.*
Inside RMI 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. 4/25/2018
28
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 4/25/2018
29
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]. 4/25/2018
30
Simple RMI System Model
Host M/C JVM Registry Client M/C server JVM server ClientApp Remote interface 4/25/2018
31
Registry (singleton – per system - during production)
Name Mortgage Tempertur Reference 4/25/2018
32
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. 4/25/2018
33
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 4/25/2018
34
Reference For more details, check the link below ides/rmi/index.html 4/25/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.