JAVA RMI (Remote Method Invocation)

43
JAVA RMI (Remote Method Invocation) SNU IDB Lab. Dong-Hyuk Im 2007.05.07

description

JAVA RMI (Remote Method Invocation). SNU IDB Lab. Dong-Hyuk Im 2007.05.07. Contents. Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference. Related Technologies. RPC (“Remote Procedure Calls”) Developed by Sun - PowerPoint PPT Presentation

Transcript of JAVA RMI (Remote Method Invocation)

Page 1: JAVA RMI (Remote Method Invocation)

JAVA RMI (Remote Method Invocation)

SNU IDB Lab.Dong-Hyuk Im

2007.05.07

Page 2: JAVA RMI (Remote Method Invocation)

2

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 3: JAVA RMI (Remote Method Invocation)

3

Related Technologies

RPC (“Remote Procedure Calls”)

Developed by Sun Platform-specific

CORBA (“Common Object Request Broker Architecture”)

Developed by OMG Access to non-Java objects (as well as Java)

DCOM (“Distributed Common Object Model”)

Developed by Microsoft Access to Win32 objects

Page 4: JAVA RMI (Remote Method Invocation)

4

What is RMI?

Supports for distributed object in java language

Allows object to invoke methods on remote objects using local invocation

Implementation of remote interface

object A object Bskeleton

Requestproxy for B

Reply

CommunicationRemote Communication module modulereference module

for B’s class& dispatcher

remote

Client Server

Remote reference module

Page 5: JAVA RMI (Remote Method Invocation)

5

Distributed Computing with RMI

Is relatively easy to use Supports communication between

different VMs, potentially across the network

Allows to develop distributed Java programs with the same syntax and semantics used for non-distributed programs

Page 6: JAVA RMI (Remote Method Invocation)

6

Principle of RMI

RMI separates: Definition of behaviour Implementation of that behaviour

Each of them is allowed to run on different JVMs

Interfaces: define definition Classes: define implementation

Page 7: JAVA RMI (Remote Method Invocation)

7

Principle of RMI

2 classes that implement the same interface Service implementation on server Service proxy on client

Client program makes method calls to proxy

RMI sends request to remote JVM Return values are sent back to proxy /

client program

Page 8: JAVA RMI (Remote Method Invocation)

8

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 9: JAVA RMI (Remote Method Invocation)

9

RMI Architecture

The RMI architecture defines how objects behave how and when exceptions can occur how memory is managed how parameters are passed to and returned

from remote methods

Page 10: JAVA RMI (Remote Method Invocation)

10

RMI Architecture

3 abstract layers:

Advantages of layer architecture: Implementation of layers independent from each other Each layer can be enhanced / replaced without affecting

rest of the system

Page 11: JAVA RMI (Remote Method Invocation)

11

Stub & Skeleton Layer

SkeletonObject

Stub Object

Server Object

Returnvalue

Methodinvocationon client

Methodinvocationon server

Marshalledparameters

Marshalledreturn value

para

met

ers

Page 12: JAVA RMI (Remote Method Invocation)

12

Stub

Represents the remote service implementation in the client (is a proxy)

Marshalls parameters : Encoding parameters

Primitive Type (integer, Byte, … ) : copy by value Reference Type (String, Object, …) : object copy

Information block from stub to skeleton Remote object’s identifier Parameters / the ID of method

Unmarshalls return value or exception

Page 13: JAVA RMI (Remote Method Invocation)

13

Skeleton

Helper class on server Generated for RMI to use Communicates with stub across the link Reads parameters for the method call

from the link Makes the call to the service object Accepts the return value, writes it back to

the stub

Page 14: JAVA RMI (Remote Method Invocation)

14

Remote Reference Layer

Exists in both the RMI client and server Provides a constant interface to the stubs

and skeletons Manages communication between

stubs/skeleton Manages references to remote objects

Threading, garbage collection …

Manages reconnection strategies if an object should become unavailable

Page 15: JAVA RMI (Remote Method Invocation)

15

Transport Layer

Stream-based network connections that use TCP/IP

Deals with communications For interoperability, RMI may use the OMG

Internet Inter-ORB Protocol (IIOP)

Page 16: JAVA RMI (Remote Method Invocation)

16

RMI Layers

TCPRemote Reference Layer

Transport Layer

Java Virtual Machine

Client Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Page 17: JAVA RMI (Remote Method Invocation)

17

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 18: JAVA RMI (Remote Method Invocation)

18

Naming Remote Objects

How does a client find an RMI remote service? Clients find remote services by using a naming

or directory service, running on a well known host and port number

RMI can use different directory services, e.g. the

Java Naming and Directory Service (JNDI) includes simple service called RMI Registry

(rmiregistry, default on port 1099)

Page 19: JAVA RMI (Remote Method Invocation)

19

RMI Flow (1/4)

Client Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“ Fred”

Server

Page 20: JAVA RMI (Remote Method Invocation)

20

RMI Flow (2/4)

Client Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“ Fred”

Server

1

2

1. Server Creates Remote Object2. Server Registers Remote Object

Page 21: JAVA RMI (Remote Method Invocation)

21

RMI Flow (3/4)

Client Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“ Fred”

Server

4

3. Client requests object from Registry4. Registry returns remote reference(and stub gets created)

3

Page 22: JAVA RMI (Remote Method Invocation)

22

RMI Flow (4/4)

Client Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“ Fred”

Server

6

5. Client invokes stub method6. Stub talks to skeleton7. Skeleton invokes remote object method

5 7

Page 23: JAVA RMI (Remote Method Invocation)

23

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 24: JAVA RMI (Remote Method Invocation)

24

Creating Remote Object (1/2)

Define a Remote Interface extends java.rmi.Remote

interface Adder extends Remote{ public int add(int x, int y) throws RemoteException}

Page 25: JAVA RMI (Remote Method Invocation)

25

Creating Remote Object (2/2)

Define a class that implements the Remote Interface extends java.rmi.RemoteObject or java.rmi.UnicastRemoteObjectclass AdderImpl extends UnicastRemoteObject implements

Adder{ public AdderImpl() throws RemoteException { } public int add(int x, int y) throws RemoteException { return x + y; }}

Page 26: JAVA RMI (Remote Method Invocation)

26

Inheritance Diagram

Remote

UnicastRemoteObject

Adder

AdderImpl

RemoteObject

RemoteServer

Object

ImplementationExtension

Page 27: JAVA RMI (Remote Method Invocation)

27

Compiling Remote Classes

Compile the Java class javac

reads .java file produces .class file

Compile the Stub and Skeleton rmic

reads .class file produces _Skel.class and _Stub.class

Page 28: JAVA RMI (Remote Method Invocation)

28

Compiling Remote Classes (Diagram)

Adder.java(interface)

Adder.class(interface classfile)

javac

AdderImpl.java(remote class)

AdderImpl.class(classfile)

javacrmic

AdderImpl_Skel.class(skeleton classfile)

AdderImpl_Stub.class(stub classfile)

Page 29: JAVA RMI (Remote Method Invocation)

29

Registering Remote Classes

Start the registry running process

Unix: rmiregistry &

Windows: start /m rmiregistry

Page 30: JAVA RMI (Remote Method Invocation)

30

Registering Remote Classes

Remote object code in server

Remote reference code in client

// ServerAdderImpl a1 = new AdderImpl(“Add”);Naming.bind(“Add”, a1);

// ClientString url = “rmi://hostName/”;Adder a = (Adder) Naming.lookup(url + “Add”);

Page 31: JAVA RMI (Remote Method Invocation)

31

RMI Security

RMI Security Server is untrusted Stubs could be malicious

RMISecurityManager Protect from malicious stubs A downloaded class is allowed to make a

connection if the connection was initiated via the RMI transport// ClientSystem.setSecurityManager(

new RMISecurityManager());

Page 32: JAVA RMI (Remote Method Invocation)

32

RMI Client Example

// ClientSystem.setSecurityManager(

new RMISecurityManager());

String url = “rmi://hostName/”;Adder a = (Adder) Naming.lookup(url + “Add”);

int sum = a.add(2,2);System.out.println("2+2=" + sum);

Page 33: JAVA RMI (Remote Method Invocation)

33

RMI Program RMI “Adder” Adder.java

AdderImpl.java

AdderImpl.class

AdderImpl_Stub.class AdderImpl_Skel.class

AdderClient.java AdderServer.javarmic AdderImpl

implement

javac AdderImple.java

javac AdderClient.java Adder.java javac Adder.java AdderServer.java

Adder.class

AdderClient.class AdderServer.class

Adder.class

ClientServer

AdderImpl.class

generates

Page 34: JAVA RMI (Remote Method Invocation)

34

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 35: JAVA RMI (Remote Method Invocation)

35

Parameters in RMI

Primitive types Passed by value

Remote objects Passed by reference : references to remote

object can be passed and returned from method calls

Non-remote objects Passed by value Uses Java Object Serialization :

java.io.Serializable

Page 36: JAVA RMI (Remote Method Invocation)

36

Distributed Garbage Collection

Reference counting The server keeps track of which clients have

requested When a reference is made, the server marks

the object as “dirty” When a client drops the reference, it is marked

as “clean” Each reference has a lease with a specified time

When the lease term expires : defaults to 10 minutes The reference is considered to be dead The remote object may be garbage collected

Page 37: JAVA RMI (Remote Method Invocation)

37

Distributed Garbage Collection

Process P2

Skeleton (maintains reference counter)

Proxy

Remote Object

Process P1

Proxy +1

+1

Page 38: JAVA RMI (Remote Method Invocation)

38

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 39: JAVA RMI (Remote Method Invocation)

39

RMI : Benefits

Enables use of Design Patterns Use the full power of object oriented

technology in distributed computing (pass behavior and use OO design patterns)

Safe and Secure RMI uses built-in Java security mechanisms

Easy to Write/Easy to Use A remote interface is an actual Java interface

Distributed Garbage Collection Collects remote server objects that are no

longer referenced by any client in the network

Page 40: JAVA RMI (Remote Method Invocation)

40

Limitation of RMI

Tied only to platforms with Java support Can only operate with Java systems - no

support for legacy systems written in C++, Fortran, Cobol, and others future languages

Uses TCP, not UDP

Page 41: JAVA RMI (Remote Method Invocation)

41

RMI vs CORBA

Comparing RMI and CORBA doesn't reveal an optimum solution - one is not "better" than the other

CORBA Language-neutral Interoperability

RMI Specifically for Java Best for Java distributed object computing

Page 42: JAVA RMI (Remote Method Invocation)

42

Contents

Introduction RMI Architecture Naming Remote Objects Using RMI : Example Parameters & Garbage Collection Summary Reference

Page 43: JAVA RMI (Remote Method Invocation)

43

References

“CORE JAVA”, Gary Cornell, Cay S. Horstmann

“JAVA RMI”, William Grosso RMI tutorial

http://java.sun.com/docs/books/tutorial/rmi/index.html

Java Remote Invocation (RMI) http://java.sun.com/javase/6/docs/technotes/g

uides/rmi/index.html