CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

35
CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    1

Transcript of CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

Page 1: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

CMPT 401 2008

Dr. Alexandra Fedorova

Lecture VI: Distributed Objects.Remote Method Invocation

Page 2: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

2CMPT 401 2008 © A. Fedorova

Remote Method Invocation

• In an object-oriented language (usually Java)…• A way to call a method on an object…• That lives in another process..• Possibly on a different computer

©Pearson Education 2001

Page 3: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

3CMPT 401 2008 © A. Fedorova

Object-Oriented Jargon Buster: Object

• Real-world objects have:– State– Behavior

• Objects in a programming language are similar:– Their state is represented by attributes– Their behavior is represented by methods

Page 4: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

4CMPT 401 2008 © A. Fedorova

Example: Bicycle Objectclass Bicycle {

private int cadence = 0; private int speed = 0; private int gear = 1;

void changeCadence(int newValue) { cadence = newValue;

}

void changeGear(int newValue) { gear = newValue;

}

void brake(int decrement) { speed = speed -

decrement; }

}

Object definition is described in a class

attributes

method

Page 5: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

5CMPT 401 2008 © A. Fedorova

Example: Using Bicycle

public static void main(){Bicycle myNewBike = new Bicycle();

myNewBike.changeGear(5);}

Create a new Bicycle object

Invoke a methodon an object

Cannot access private attributes directly

myNewBike.gear = 5; //Illegal!!!

Page 6: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

6CMPT 401 2008 © A. Fedorova

Object-Oriented Jargon Buster: Interface

public interface MotorBike{ void kickStart();void squeezeClutch();void turnThrottle(int

degrees);}

• A definition of methods, • Just signatures, no

implementation

Page 7: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

7CMPT 401 2008 © A. Fedorova

Implementing the Interfaceclass Bicycle implements MotorBike{

...private boolean engineStarted;private boolean clutchLeverSqueezed;private int throttleSettingDegrees;...

public void kickStart(){engineStarted = true;

}

public void squeezeClutch(){clutchLeverSqueezed = true;

}

public void setThrottle(int degrees){

throttleSettingDegrees = degrees;

}}

This class must provide implement methods in

this interface

some additional variables for the new

methods

implementation of the interface

Page 8: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

8CMPT 401 2008 © A. Fedorova

Object-Oriented Jargon Buster: Exceptions

• A way to handle errors during execution of a method• In C you usually return an error code from a function• In Java you throw an exception

public interface MotorBike{ void kickStart();void squeezeClutch();void turnThrottle(int degrees);void changeGear(int gear) throws

ClutchNotSqueezedException;}

new declaration of changeGear method that may generate an exception

Page 9: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

9CMPT 401 2008 © A. Fedorova

Throwing an Exceptionclass Bicycle implements MotorBike{

...

public void changeGear(int newGear) throws ClutchNotSqueezed Exception{

if(!clutchLeverSqueezed)throw new

ClutchNotSqueezedException();else

gear = newGear;}

}

new implementation of changeGear that may generate an exception

Can’t change gears unless clutch is squeezedCreate an Exception object that may contain information about the error

Page 10: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

10CMPT 401 2008 © A. Fedorova

Catching An Exception

• As you saw, is a method may throw an exception, this is specified in the method’s signature

• The code calling that method must be written to handle that exception

public static void main(){MotorBike myDirtBike = new MotorBike();

try{myDirtBike.changeGear(5);

}catch(ClutchNotSqueezedException cnse){

System.out.println(“Can’t change gears unless you squeeze the clutch!”);

}}

wrap code that might throw exception in try-catch clause

code that handles the exception

Page 11: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

11CMPT 401 2008 © A. Fedorova

A Remote Object

©Pearson Education 2001

• A remote object will advertise and implement a remote interface• Remote invocation can only invoke methods in the remote interface

Page 12: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

12CMPT 401 2008 © A. Fedorova

A Remote Object in Java

• Must declare a remote interface – that is the interface that extends the interface Remote

• Each method in a remote interface must be declared to throw a RemoteException

public interface BankAccount extends java.rmi.Remote {

public void deposit(float amount) throws

java.rmi.RemoteException; public void withdraw(float amount) throws

OverdrawnException, java.rmi.RemoteException;

public float getBalance() throws java.rmi.RemoteException; }

extends interface Remote

throws Remote exception

Page 13: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

13CMPT 401 2008 © A. Fedorova

A Bird Eye’s View of RMI

• Client program knows the interface• Server program implements the interface• Client invokes remote methods described by the interface using the

RMI system

Page 14: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

14CMPT 401 2008 © A. Fedorova

Implementation and Proxy

• The actual implementation of the interface lives on the server• There is a proxy implementation on the client• The client invokes the proxy implementation• The proxy implementation communicates with the actual implementation

and returns the result to the client

proxy createslocation transparency

Page 15: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

15CMPT 401 2008 © A. Fedorova

RMI System

• Stub and Skeleton Layer: intercepts calls from client and redirects to Remote Reference Layer

• Remote Reference Layer interprets references to remote objects, knows what to do with them. Passes messages to the Transport Layer

• Transport Layer sends messages using request-reply protocol

Page 16: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

16CMPT 401 2008 © A. Fedorova

Stub and Skeleton Layer

• The skeleton: – Reads the parameters for the

method call from the link– Makes the call to the remote

service implementation object– Accepts the return value– Writes the return value back to

the stub.

Client Server

stub skeleton

actual implementation

• The stub: – Marshalls call parameters– Sends them to the server– Unmarshalls return parameters– Returns them to the client

program

Page 17: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

17CMPT 401 2008 © A. Fedorova

Remote Reference Layer (RRL)

• Client RRL: – knows if the remote object (still)

exists– knows where to locate server

holding the remote object– called by the stub

• Server RRL: – knows if the local object

exists– knows where to locate the

local implementation– calls the skeleton

Page 18: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

18CMPT 401 2008 © A. Fedorova

Transport Layer

• Manages connection between Java Virtual Machines• Used over the network and on the local host• There is a messaging protocol implemented over TCP/IP

Java Runtime Environment – code that enables JVM to run

Page 19: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

19CMPT 401 2008 © A. Fedorova

Components of the Transport Layer

• Messaging protocol provides at-most-once semantics• Any layer can be switched by an alternative: e.g., TCP/IP with UDP/IP• Sun and IBM are working on next version of RMI that will use IIOP, the

open protocol used in CORBA• Bea Weblogic and Ninja RMI use their proprietary messaging protocols

TCP/IP

Messaging protocol (e.g., Java Remote Method

Protocol – JRMP)

UDP/IP

IIOPBEA Weblogic protocol

Ninja RMI protocol

Page 20: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

20CMPT 401 2008 © A. Fedorova

Local vs. Remote Objects

• Remote objects are defined using a Remote interface definition• Local objects are defined using a Class definition• Why is there a difference? • A class usually has a constructor, so you can construct an object

described by a class in a local memory using the constructor• An interface does not have a constructor, which is the right thing for

the remote object• You should not be able to create a remote object in a local memory,

so you are not given a constructor

Page 21: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

21CMPT 401 2008 © A. Fedorova

Creation of Remote Object

• Server creates an instance of remote object• Client wants to invoke a method on that remote object• But first it must obtain a reference to the remote object• How does the client obtain the remote reference?

Client Server

remote object

instanceremote object

reference

Page 22: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

22CMPT 401 2008 © A. Fedorova

Remote Object References

Naming and Directory Service[well known DNS name and port]rmiregistry

Server 1. Create an object instance

2. Export to RMI registry

3. Create a service that listens for invocations on that object

4. Register object under public name

Page 23: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

23CMPT 401 2008 © A. Fedorova

Server Creates and Registers Remote Object

import java.rmi.Naming;

public class BankServer {

public BankServer() {

try {

BankAccount b = new BankAccountImpl(); Naming.rebind("rmi://localhost:1099/

BankService", c);

} catch (Exception e) { System.out.println("Trouble: " + e);

}

}

public static void main(String args[]) {

new CalculatorServer();

}

}

BankAccountImpl implements BankAccount interface

Create a BankAccount object

Register object under public name

Page 24: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

24CMPT 401 2008 © A. Fedorova

Client Obtains Remote Reference

public class BankClient {

public static void main(String[] args){

try {

BankAccount b =

(BankAccount) Naming.lookup( "rmi://localhost /BankService");

}

catch (RemoteException re) {

... //handle exception

}

}

Obtain remote object reference via rmiregistry

Page 25: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

25CMPT 401 2008 © A. Fedorova

The Entire RMI Program

• Written by the programmer– BankAccount.java – Remote interface– BankAccountImpl.java – Implementation of

remote interface on the server– BankServer.java – The server that creates an instance of

BankAccountImpl and binds it– BankClient.java – The client that obtains remote

object reference and invokes remote methods on it• Generated by rmic compiler (rmic BankAccountImpl.java)

– BankAccount_Stub.class– BankAccount_Skel.class

Page 26: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

26CMPT 401 2008 © A. Fedorova

Local vs. Remote Parameter Passing• For a local call

– Primitive types are passed by value - a primitive type variable is copied on the caller’s stack

– Object references are passed by value – an object reference (not the entire object) is copied on the caller’s stack

• For a remote call– Primitive types are copied to the message sent to the server– Entire object, not just the reference is copied– All objects referenced by the parameter object are copied too! (Like pointer

picking)– Java serialization is a format to convert an object and object that it references

in a linear form.– Objects are serialized before they are passed remotely and deserialized on the

other side

Page 27: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

27CMPT 401 2008 © A. Fedorova

There’s More to RMI System

• What do you need a web server for?• Sometimes a client passes or a server returns an object whose class definition is not available

locally• In that case, the definition is downloaded from the web server

Page 28: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

28CMPT 401 2008 © A. Fedorova

Distributed Garbage Collection

• In C you have to explicitly deallocate memory that is no longer used

• In Java, unused objects are garbage collected: local JVM automatically destroys objects that are not referenced by anyone

• Garbage collection must also work with RMI• Java RMI system implements a distributed garbage

collector

Page 29: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

29CMPT 401 2008 © A. Fedorova

Distributed Garbage Collection (cont)

• RMI Remote Layer on the server counts the number of remote references to each remote object it exports

• When there are no more local and remote references to the object, the object is destroyed

• The client should tell the server when it no longer uses the object

• But what if it does not?

Page 30: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

30CMPT 401 2008 © A. Fedorova

Distributed Garbage Collection (cont)

• Each remote reference has an associated “lease” time• Client RMI layer must renew the lease on the reference if

the reference is still in use on the client• When all leases expire, the server can destroy the object• Client must be prepared to deal with “disappeared”

objects

Page 31: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

31CMPT 401 2008 © A. Fedorova

Is RMI Transparent?

Page 32: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

32CMPT 401 2008 © A. Fedorova

RMI is Transparent

• Hides details for argument marshalling/unmarshalling• Hides client/server communication details• Garbage collection works in a distributed manner

Page 33: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

33CMPT 401 2008 © A. Fedorova

RMI is Not Transparent• Call invocation semantics

– Local invocation has “exactly-once” semantics– RMI has “at-most-once” semantics– If RMI had “at-least-once” semantics, programmer would need to be

sure that remote operations are idempotent• RMI is subject to partial failures

– Server, registry or network can fail independently of the client– RMI-specific failure mode is exposed to the programmer (must catch

RemoteException)• Latency of RMI is higher than that of local invocation

– Should the programmer be allowed to set a timeout or abort an RMI that’s taking too long?

Page 34: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

34CMPT 401 2008 © A. Fedorova

Summary

• Java RMI is language-specific abstraction for communication in a distributed system

• A Java program can invoke a method on an object located in another JVM on another host

• Remote objects are registered with a global naming service

• Client can obtain a remote reference by name• RMI exposes “remoteness” to the programmer

Page 35: CMPT 401 2008 Dr. Alexandra Fedorova Lecture VI: Distributed Objects. Remote Method Invocation.

35CMPT 401 2008 © A. Fedorova

Debate

• RPC vs. RMI• RPC and RMI took different approaches to implementing

remote function calls• RPC tried to make it completely transparent• RMI exposed “remoteness” to the programmer• Which of the two approaches is the right one?