RPC/RMI Tutorial: Prepared by Dr. Seyhan Ucar Adopted by Beakal Gizachew
RPC/RMI Tutorial: Prepared by Dr. Seyhan Ucar Adopted by Beakal Gizachew
2
Remote Procedure Call
◼ Powerful technique for constructing distributed applications.
◼ Idea: mask distributed computing system using a “transparent”
abstraction
❑ Looks like normal procedurecall
❑ Hides all aspects of distributed interaction
❑ Supports an easy programming model
◼ Based on extending procedure call so that the called procedure does
not need to exist in the same address space as the calling procedure.
◼ By using RPC, programmers of distributed applications avoid the
details of
the interface with the network.
◼ RPC is at the core of many distributed systems today.
3 3
RPC Protocol
client server
“binds” to registers with
server name service
4
RPC Protocol cont.
client server
“binds” to registers with
server name service
prepares,
sends request
receives request
5
RPC Protocol cont.
client server
“binds” to registers with
server name service
prepares,
sends request
receives request
invokes handler
sends reply
6
RPC Protocol cont.
client server
“binds” to registers with
server name service
prepares,
sends request
receives request
invokes handler
sends reply
unpacks reply
7
Client and Server Stubs
◼ Client-side stub ◼ Server-side stub
❑ Looks like local server ❑ Looks like local client
8
Overview of RPC
9
Interaction in RMI
◼ RMI uses a standard mechanism (employed in RPC systems) for communicating with
remote objects: stubs and skeletons.
◼ A stub for a remote object acts as a client's local representative or proxy for the remote
object.
◼ The caller invokes a method on the local stub which is responsible for carrying out the
method call on the remote object.
◼ In RMI, a stub for a remote object implements the same set of remote interfaces that a
remote object implements. When a stub's method is invoked, it does the following:
◼ initiates a connection with the remote JVM containing the remote object
◼ marshals (writes and transmits) the parameters to the remote JVM
◼ waits for the result of the method invocation,
◼ unmarshals (reads) the return value or exception returned,
◼ returns the value to the caller.
10
RMI (Remote Method Invocation)
SampleServer remoteObject;
int s;
…
s = remoteObject.sum(1,2); 1,2
public int sum(int a,int b)
{
return a + b;
3 }
System.out.println(s);
11
Building and Running RMI Applications
I. Building RMI Applications
1) Implementing the Interfaces
2) Implementing the Server
3) Implementing the Client
II. Running RMI Applications
1) Run rmiregistry
2) Run Server
3) Run Client
12
Example- Demo:
Mortgage
Calculator
13
RMI Demo
◼ We'll create a service that can calculate the mortgage payment and the method is
available to public.
◼ The first thing we need to do is to agree upon an interface. An interface is a
description of the methods we will allow remote clients toinvoke.
◼ In Mortgage calculator, the interface will becalculatePayment.
◼ Let's consider exactly what we'll need.
❑ A method that accepts as parameters an double principal, double annualrate , and int terms,
calculatePayment(double principal, double annualRate, int terms)
❑ When clients invoked the remote method with provided parameters monthly payment will be
calculated.
14
1. Interface Implementation
15
2. Server Implementation
16
3. Client Implementation
17
How to Run RMI application
(from Windows Command line)
18
References
◼ Book: Distributed Systems Principles and Paradigms, 2nd edition (Chapter 4,
Section 4.2), Andrew S. Tanenbaum, Maarten van Steen
◼ Hello RMI Tutorial
https://docs.oracle.com/javase/7/docs/technotes/guides/rmi/hello/hello-
world.html#define%60
◼ You tube demo tutorial https://www.youtube.com/watch?v=3fq4AdaiGFA
◼ PC Code example
19
QUESTIONS??
20
Example
◼ A client makes remote procedure calls to a server. The client takes 5 milliseconds
to compute the arguments for each request, and the server takes 10 milliseconds to
process each request. The local operating system processing time for each send or
receive operation is 0.5 milliseconds, and the network time to transmit each request
or reply message is 3 milliseconds.
Marshalling or unmarshalling takes 0.5 milliseconds permessage.
Calculate the time taken by the client to generate and return from two requests:
◼ a) if it is single-threaded
◼ b) if it has two threads that can make requests concurrently on a single processor.
(You can ignore context-switching times.)
21
Sample Answer
❑ a) time per call = calc. args + marshal args + OS send time + message transmission + OS
receive time + unmarshall args + execute server procedure + marshall results + OS send
time + message transmission + OS receive time + unmarshal args
= 5 + 4 * marshal / unmarshal + 4 * OS send / receive + 2 * message transmission +
execute server procedure = 5+ 4 * 0.5 + 4 * 0.5 +2 * 3 + 10 ms = 5+2+2+6+10 =25ms.
Time for two calls = 50 ms.
❑ b) threaded calls: client does calc. args + marshal args + OS send time (call 1)
= 5+.5+.5 = 6
then calc args + marshal args + OS send time (call 2) = 6
= 12 ms then waits for reply from first call
server gets first call after message transmission + OS receive time + unmarshal args
= 6+ 3+.5+.5 = 10 ms, takes 10+1 to execute, marshal, send at 21 ms
22
Thank you!
23