Rmi Programming
Rmi Programming
- Remote Invocation
2
Introduction
Applications
Request
doOperation
message getRequest
select object
(wait) execute
Reply method
message sendReply
(continuation)
4
Request-Reply Operations
5
Invocation Semantics
6
Distributed Objects
7
Outline
8
Java RMI
9
RMI Architecture and Components
Remote reference module (at client & server) is responsible for providing addressing to the
proxy (stub) object
Proxy is used to implement a stub and provide transparency to the client. It is invoked directly
by the client (as if the proxy itself was the remote object), and then marshal the invocation into a
request
Communication module is responsible for networking
Dispatcher selects the proper skeleton and forward message to it
Skeleton un-marshals the request and calls the remote object
client server
remote
skeleton object B
object A proxy for B Request & dispatcher
for B’s class
Reply
servant
Remote Communication Communication Remote reference
10 module module module
reference module
Invocation Lifecycle
Client Server
Stub Skeleton
Serializes Receives, Receives, Serialises
arguments, deserialises deserialises response,
transmit response arguments transmit
2 7 3 6
Network
11
Outline
12
Steps for implementing an RMI application
13
RMI Programming and Examples
Application Design
Remote Interface
Exposes the set of methods and properties available
Defines the contract between the client and the server
Constitutes the root for both stub and skeleton
Servant component
Represents the remote object (skeleton)
Implements the remote interface
Server component
Main driver that makes available the servant
It usually registers with the naming service
Client component
14
Java RMI
Client Server
RemotObj 6. method1()
<Request over the network> RemoteObj
proxy Dispatcher/ 7. method1()
<implements
RemoteInterface> 9. Return value Skeleton
<Reply over the network>
10. Return RemoteObj
5. method1() 8. return value <implements
value
RemoteInterface>
2. bind(“myRO”, RemoteObj)
RMI Registry
3. lookup(“myRO”) <“myRO”, Remote Ref. to RemoteObj>
15
Example application – Hello World
Server side
Create a HelloWorld interface
Implement HelloWorld interface with methods
Create a main method to register the HelloWorld
service in the RMI Name Registry
Generate Stubs and Start RMI registry
Start Server
Client side
Write a simple Client with main to lookup
HelloWorld Service and invoke the methods
16
1. Define Interface of remote method
//file: HelloWorld.java
import java.rmi.Remote;
import java.rmi.RemoteException;
17
2. Define RMI Server Program
// file: HelloWorldServer.java
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
18
3. Define Client Program
// file: RMIClient.java
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
String hostName = "localhost";
String serviceName = "HelloWorldService";
String who = "Raj";
if(args.length == 3){
hostName = args[0];
serviceName = args[1];
who = args[2];
}
else if(args.length == 1){
who = args[0];
}
try{
HelloWorld hello = (HelloWorld)Naming.lookup("rmi://"+hostName+"/"+serviceName);
System.out.println(hello.sayHello(who));
}catch(Exception e){
e.printStackTrace();
}
}
}
19
Define Access Policy
20
Java RMI Example
21
Outline
22
Security Manager
23
Security Manager (cont.)
24
File: “local.policy” contents
Specific permissions:
grant {
permission java.net.SocketPermission "*:1024-65535","connect,accept";
permission java.io.FilePermission "/home/globus/RMITutorial/-", "read";
};
25
Exceptions
26
Passing objects
27
RMI Dynamic Class Loading
28
Outline
29
A Simple Math Server in RMI
MathServer
(multiple operations)
User
Math Service
30
Java RMI Example
31
Java RMI Example
32
Java RMI Example
33
Java RMI Example
LocateRegistry.getRegistry("localhost");
IRemoteMath remoteMath = (IRemoteMath) registry.lookup("Compute");
34
Java RMI Example
35
Outline
36
Remote Procedure Call (RPC) – used in C
Request
Reply
client stub server stub
procedure procedure
client service
program Communication Communication procedure
module module dispatcher
37
Remote Procedure Call (RPC)
Communication Module
Implements the desired design choices in terms of retransmission of requests,
dealing with duplicates and retransmission of results
Client Stub Procedure
Behaves like a local procedure to the client. Marshals the procedure identifiers
and arguments which is handed to the communication module
Unmarshalls the results in the reply
Dispatcher
Selects the server stub based on the procedure identifier and forwards the
request to the server stub
Server stub procedure
Unmarshalls the arguments in the request message and forwards it to the service
procedure
Marshalls the arguments in the result message and returns it to the client
38
Summary: RMI Programming
39