30
Java RMI : Remote Method Invocation CS 4119 - Computer Networks Columbia University - Spring 2000 Alexander V. Konstantinou [email protected]

Java RMI : Remote Method Invocation - Columbia University · Java RMI : Remote Method Invocation CS 4119 ... • Basic types as parameters • Pointers require explicit handling •

Embed Size (px)

Citation preview

Java

RM

I : R

emot

e M

etho

d In

voca

tion

CS

4119

-C

ompu

ter

Net

wor

ks

Col

umbi

a U

nive

rsit

y -

Spri

ng 2

000

Ale

xand

er V

. Kon

stan

tinou

[email protected]

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou2

Intr

oduc

tion

: Rem

ote

Com

puta

tion

•O

bjec

ts e

ncap

sula

te d

ata

+ o

pera

tion

s

•U

sual

ly s

tore

d an

d ev

alua

ted

loca

lly

Obj

ect

Exe

cutio

nT

hrea

dM

etho

dIn

voca

tion

Java

Vir

tual

Mac

hine

•R

emot

est

orag

e/ev

alua

tion

can

also

be

usef

ul :

•O

bjec

t enc

apsu

late

s ph

ysic

al r

esou

rce

(e.g

. Pri

nter

)

•D

ata

resi

des

rem

otel

y an

d is

ve

ry

larg

e (e

.g.

phon

e di

rect

ory

look

up)

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou3

Exa

mpl

e: P

rint

Obj

ect

// …

Document myDoc = …;

Printer printer = …;

printer.print(myDoc);

// …

Printer {

void print(Document d) {

// printer-specific protocol

// e.g. postcript }

}

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou4

Rem

ote

Prin

t Obj

ect I

mpl

emen

tatio

n

•H

ow c

an J

ava

supp

ort r

emot

e op

erat

ions

?

•U

se o

f pr

oxy

obje

cts

:

public class Printer_Proxy {

public void print(Document doc){

// magic communication

} }

Document myDoc = …;

Printer printer = …;

printer.print(myDoc);

Java VM

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou5

Rem

ote

Met

hod

Invo

catio

n O

verv

iew

•R

MI

is J

ava’

s m

echa

nism

for

aut

omat

ical

lyge

nera

ting

prox

y cl

asse

s.•

Use

r co

des

serv

ice

and

clie

nt o

bjec

ts•

RM

I co

mpi

ler

gene

rate

s ne

twor

k co

mm

unic

atio

n co

de

Serv

ice

Obj

ect

(e.g

. Pri

nter

)

Gen

erat

ed

Stub

Gen

erat

ed

Prox

y

Clie

ntO

bjec

t(e

.g. E

dito

r)Ja

va V

MJa

va V

M

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou6

Rem

ote

Inte

rfac

e E

xam

ple

public interfacePrintServiceextends java.rmi.Remote {

public void print(Object obj)

throws java.rmi.RemoteException;

}

•W

hat t

ies

it al

l tog

ethe

r ?

•A

nsw

er: S

erve

r, s

tub,

pro

xy a

nd c

lient

all

shar

e th

e sa

me

rem

ote

inte

rfac

e

TCP

void

pri

nt(…

) {

… }

RM

Ise

rver

sock

et

RM

Iso

cket

call

prox

y.pr

int

(doc

)

Java

VM

Java

VM

TCP

Seri

alia

zed

docu

men

t

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou7

RM

I Fe

atur

es

•L

angu

age

spec

ific

(Ja

va)

•O

bjec

t ori

ente

d –

Full

obje

cts

as p

aram

etea

rs

–Su

ppor

ts d

esig

n pa

ttern

s

•M

obile

beh

avio

r –

Mov

e in

terf

ace

impl

emen

tatio

n fr

om c

lient

to s

erve

r,

and

serv

er to

clie

nt

•Sa

fe &

Sec

ure

(Jav

a V

M s

ecur

ity)

•C

onne

cts

to e

xist

ing/

lega

cy (

JNI/

JDB

C)

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou8

RPC

ver

sus

RM

I

•Pr

oced

ural

•L

angu

age

Inde

pend

ent

•E

xter

nal d

ata

repr

esen

tatio

n (X

DR

)

•B

asic

type

s as

par

amet

ers

•Po

inte

rs r

equi

re e

xplic

it ha

ndlin

g

•N

o co

de m

obili

ty (

sam

e fo

r C

OR

BA

, DC

OM

)

•O

bjec

t Ori

ente

d

•L

angu

age

Spec

ific

•Ja

va O

bjec

t Ser

ializ

atio

n

•A

ny o

bjec

t im

plem

entin

g se

rial

izat

ion

as p

aram

eter

•R

efer

ence

s to

loca

l and

re

mot

e ob

ject

s ha

ndle

d au

tom

atic

ally

(de

ep c

opy)

•M

obile

cod

e (J

ava

byte

-co

de)

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou9

RM

I T

erm

inol

ogy

•A

rem

ote

obje

ctis

one

who

se m

etho

ds c

an b

e in

voke

d fr

om a

noth

er J

ava

Vir

tual

Mac

hine

, pot

entia

lly o

n a

diff

eren

t hos

t.

•R

emot

e m

etho

d in

voca

tion

(RM

I) is

the

actio

n of

invo

king

a

met

hod

of a

rem

ote

inte

rfac

e on

a r

emot

e ob

ject

.

// Remote method invocation example (incomplete)

PasswordDb

db = (PasswordDb) Naming.lookup("//myhost/cs4119db");

db.put("akonstan", "secRet!");

// Local method invocation example

HashTable

table = new HashTable();

table.put("akonstan", "secRet!");

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou10

Rem

ote

Invo

catio

n Se

man

tics

The

sem

antic

s of

rem

ote

met

hod

invo

catio

ns d

iffe

r in

som

e w

ays

from

thos

e of

loca

l met

hod

invo

catio

ns :

•C

lient

s in

tera

ct w

ith r

emot

e in

terf

aces

.

•N

on-r

emot

e ar

gum

ents

, and

res

ults

fro

m, a

rem

ote

met

hod

invo

catio

n ar

e pa

ssed

by

copy

rath

er th

an b

y re

fere

nce.

•A

rem

ote

obje

ct is

pas

sed

by r

efer

ence

, not

by

copy

ing

the

actu

al r

emot

e im

plem

enta

tion.

•C

lient

s in

voki

ng r

emot

e ob

ject

s m

ust h

andl

e ad

ditio

nal

failu

re m

odes

(exc

eptio

ns)

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou11

Java

RM

I A

rchi

tect

ure

Stub

s

Clie

nt

Rem

ote

Ref

eren

ce Tra

nspo

rtSkel

eton

s

Rem

ote

Ref

eren

ce

Serv

er•

Serv

ers

exte

nd RemoteObject

and

impl

emen

tre

mot

e in

terf

aces

.

•A

ny s

eria

lizab

le o

bjec

t can

be

sent

as

a pa

ram

eter

or

retu

rned

as

a re

spon

se

•T

he R

MI

com

pile

r ge

nera

tes

clie

nt s

tubs

(pr

oxie

s) a

nd

serv

er s

kele

tons

(dis

patc

hers

)

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou12

Java

Obj

ect S

eria

lizat

ion

•R

MI

para

met

ers

pass

ed a

s se

rial

ized

obj

ects

•Se

rial

ized

obj

ects

are

con

vert

ed to

a s

trea

m o

f by

tes.

•Se

rial

izat

ion

stor

es th

e cl

ass

stru

ctur

e al

ong

with

the

valu

es

of th

e ob

ject

(cl

ass

stru

ctur

e on

ly s

tore

d on

ce p

er c

lass

).

•Se

rial

izat

ion

hand

les

refe

renc

esby

trav

ersi

ng th

em a

nd

seri

aliz

ing

obje

cts

alon

g th

e w

ay.

•Y

ou d

o no

t nee

d to

wri

te a

ny s

peci

al c

ode

to u

tiliz

e th

e se

rial

izat

ion

rout

ines

. It

is s

uffi

cien

t to

impl

emen

t the

java.io.Serializable

inte

rfac

e (t

his

is a

mar

ker

inte

rfac

e an

d do

es n

ot d

efin

e an

y m

etho

ds).

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou13

public abstract class RemoteObject

RemoteObject

implements Remote, java.io.Serializable

{protected RemoteObject();

protected RemoteObject(RemoteRef newref);

public int hashCode();

public boolean

equals(Object obj);

public String

toString();

}

RM

I In

terf

aces

and

Cla

sses

Rem

ote

Rem

oteO

bjec

t

Unic

astR

emot

eObj

ect

Rem

oteS

erve

r

IOEx

cept

ion

Rem

oteE

xcep

tion

public interface Remote

Remote

{}

java.rmi

java.rmi.server

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou14

Stub

& S

kele

ton

Gen

erat

ion

•C

lient

Stu

bs &

Ser

ver

Skel

eton

s ar

e ge

nera

ted

by th

e rmic

com

pile

r.

•T

he rmic

com

pile

r ta

kes

as in

put a

cla

ss im

plem

entin

g re

mot

e in

terf

aces

and

out

puts

a S

tub

and

a Sk

elet

on c

lass

rmic

RemoteImpl.class

RemoteImpl_Skel.class

RemoteImpl_Stub.class

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou15

Loc

atin

g se

rver

s w

ith R

MI

Reg

istr

y

•R

MI

regi

stry

is th

e ob

ject

dir

ecto

ry s

ervi

ce.

•O

bjec

ts a

re b

ound

to th

e re

gist

ry u

sing

str

ing

nam

es.

•T

he r

egis

try

proc

ess

may

exe

cute

on

any

netw

ork

host

.

•R

MI

UR

L:

rmi://myhost:1099/DCC-printer

Pri

nt

Se

rvic

e

JVM

Pri

nt

Clie

nt

JVM

Pri

nt

Se

rvic

eIm

ple

m.

Pri

nt

Clie

nt

JVM

Se

rve

rL

ap

top

RM

I R

eg

istr

yb

ind

(dcc

pr,

"D

CC

-pri

nte

r")

loo

kup

("

DC

C-p

rin

ter"

)

RM

I E

xam

ple

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou17

RM

I E

xam

ple

: Rem

ote

Lis

t Pri

nter

•Im

plem

ent a

rem

ote

Prin

ter

Serv

er.

•Pr

int S

erve

r w

ill a

ccep

t a li

nked

list

of

Java

Obj

ects

, and

pr

int t

hem

to s

tand

ard

out.

•W

e w

ill d

efin

e th

e fo

llow

ing

clas

ses

:ListNode

: a n

ode

in th

e lin

k lis

t

LinkedList

: a li

nked

list

obj

ect s

uppo

rtin

g lim

ited

oper

atio

ns

ListPrinter

: the

inte

rfac

e fo

r ou

r re

mot

e pr

inte

r se

rver

ListPrinterImpl

: an

impl

emen

tatio

n of

the ListPrinter

inte

rfac

e

Client

: a p

rint

ing

clie

nt th

at w

ill c

reat

e an

d se

nd a

list

for

pri

ntin

g

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou18

RM

I E

xam

ple

(Lis

t Cla

sses

)

public class

ListNode

implements

java.io.Serializable

{private Object value;

private

ListNode

next;

public ListNode

(Object value,

ListNode

next) {}

public Object

getValue()

public void setValue

(Object value) {}

public ListNode getNext()

public void setNext

(ListNode

next) {}

}

public class LinkedList

implements java.io.Serializable

{private

ListNode

head;

private

ListNode

tail;

public LinkedList() {

head = null;

tail = null;

} public ListNode getHead() {}

public ListNode getTail() {}

public void insert(Object obj)

public void append

(LinkedList

list) {}

public boolean isEmpty() {}

public void print() {}

}

A s

impl

e de

fini

tion

of a

link

ed li

st.

Not

e th

at b

oth

clas

ses

mus

t im

plem

ent t

he

seri

aliz

atio

n in

terf

ace

so th

at th

ey m

ay b

e us

ed in

rem

ote

met

hods

.

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou19

RM

I E

xam

ple

(Rem

ote

Inte

rfac

e)

public interface

ListPrinter

extends java.rmi.Remote

{boolean

print(LinkedList

list)

throws java.rmi.RemoteException;

} •D

ecla

re a

pub

lic in

terf

ace

that

ext

ends

java.rmi.Remote

•E

ach

met

hod

mus

t dec

lare

java.rmi.RemoteException

in it

s th

row

s cl

ause

•A

rem

ote

obje

ct p

asse

d as

an

argu

men

t or

retu

rn v

alue

m

ust b

e de

clar

ed a

s th

e re

mot

e in

terf

ace,

not

the

impl

emen

tatio

n cl

ass

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou20

RM

I E

xam

ple

(Ser

ver

Impl

emen

t.)

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

public class

ListPrinterImpl

extends

UnicastRemoteObject

implements ListPrinter

{

// Constructor

public

ListPrinterImpl(String name) throws

RemoteException

{super();

} // Implement ListPrinter method

public boolean

print(LinkedList

list) throws RemoteException{

list.print();

return true;

}}

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou21

RM

I E

xam

ple

(Ser

ver

Impl

. Con

t.)

public static void main(String[]

args) {

// Create and install a security manager

System.setSecurityManager(new

RMISecurityManager());

try {

ListPrinterImpl obj

= new

ListPrinterImpl("ListPrinterServer");

// Bind to the registry (rmiregistry)

Naming.rebind("//sutton.cs.columbia.edu:1099/myprinter",

obj);

System.out.println("myprinter

bound in registry");

} catch (Exception e) {

System.out.println("ListPrinterImpl

err: " + e.getMessage());

e.printStackTrace();

}} // main

} //

ListPrinterImpl

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou22

RM

I E

xam

ple

(Pri

nt C

lient

)

import java.util.Date;

import java.rmi.*;

public class Client {

public static void main(String[]

args){

LinkedList

a = new

LinkedList();

a.insert(new Date());

a.insert("Today is");

try {

ListPrinter lpr

= (ListPrinter) Naming.lookup

("//" +

ListPrinterImpl.serverHost

+ ":" +

ListPrinterImpl.serverPort

+ "/myprinter");

lpr.print(a);

} catch(Exception e) {

System.out.println("Client exception: "+e.getMessage());

}}

}

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou23

RM

I E

xam

ple

(Com

pila

tion)

$javac ListPrinterImpl.java

$javacClient.java

$rmic ListPrinterImpl

Com

pila

tion

will

gen

erat

e:

-class

file

s fo

r ea

ch ja

va c

lass

,

-ListPrinterImpl_Stub.class

: clie

nt s

ide

prox

y fo

r th

e re

mot

e ob

ject

,

-ListPrinterImpl_Skel.class

: ser

ver

side

dis

patc

her

for

calls

to th

e ac

tual

rem

ote

obje

ct im

plem

enta

tion,

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou24

RM

I E

xam

ple

(Exe

cutio

n)

Exe

cute

the

fol

low

ing

in s

epar

ate

win

dow

s :

$rmiregistry6234

You

mus

t re

star

t th

e re

gist

ry a

fter

cha

ngin

g th

e re

mot

e in

terf

ace!

$ java ListPrinterImpl

The

Lis

tPri

nter

Impl

pro

cess

sho

uld

outp

ut :

myprinter

bound in registry

The

Lis

tPri

nter

Impl

pro

cess

sho

uld

outp

ut :

Today is -> Sun Mar 08 19:02:31 EST 1998 -> EOL

Star

t th

e cl

ient

in a

sep

arat

e w

indo

w :

$ java Client

Adv

ance

d R

MI

Top

ics

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou26

Rem

ote

Act

ivat

ion

•R

egis

teri

ng a

rem

ote

obje

ct w

ith th

e R

MI

regi

stry

req

uire

s th

e ob

ject

to b

e co

ntin

ually

act

ive

•JD

K 1

.2 in

trod

uces

the

RM

I da

emon

(R

emot

e A

ctiv

atio

n)

•D

aem

on r

egis

ters

info

rmat

ion

abou

t rem

ote

obje

ct

impl

emen

tatio

ns th

at a

re c

reat

ed o

n-de

man

d.

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou27

Perf

orm

ance

Iss

ues

•R

MI

calls

sho

uld

be u

sed

for

larg

e-gr

ain

com

puta

tion.

•E

very

RM

I m

etho

d in

voca

tion

resu

lts in

:–

A n

ew T

CP

conn

ectio

n to

the

rem

ote

serv

er

–C

reat

ion

of a

new

thre

ad o

n th

e re

mot

e se

rver

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou28

Con

clud

ing

Not

es

•R

MI

mov

es R

PC to

the

obje

ct w

orld

•O

bjec

t ser

ializ

atio

n si

mpl

ifie

s m

arsh

alin

g of

dat

a

•L

angu

age

spec

ific

mec

hani

sm–

may

be

expo

rted

usi

ng th

e Ja

va N

ativ

e In

terf

ace

(JN

I)

•R

MI

may

be

used

to im

plem

ent a

gent

s

•O

ther

adv

ance

d R

MI

topi

cs :

–R

MI

over

Sec

ure

Sock

et L

ayer

(SS

L)

–E

xpor

ting

clas

s by

te-c

ode

usin

g H

TT

P

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou29

How

-to

Ove

rvie

w

•D

efin

e re

mot

e in

terf

ace

•W

rite

cla

ss im

plem

enti

ngre

mot

e in

terf

ace

(and

ex

tend

ing

Uni

cast

Rem

oteO

bjec

t)•

Usermic

toco

mpi

lecl

ass

stub

and

pro

xy

•St

art

RM

I re

gist

ry (

with

st

ub &

pro

xy c

lass

es in

cl

assp

ath)

•E

xecu

tese

rver

and

bin

d to

RM

I re

gist

ry•

Loo

kup

rem

ote

obje

ct in

re

gist

ry

•In

voke

rem

ote

met

hod

on

prox

y

•H

andl

ere

mot

e in

voca

tion

fail

ures

11/7

/200

2A

lexa

nder

V. K

onst

antin

ou30

Java

and

Jav

a R

MI

Res

ourc

es

•Su

n M

icro

syst

ems,

Jav

a R

MI

hom

e

–http://java.sun.com/products/jdk/rmi

•Sr

idha

ran,

Pra

shan

t. A

dvan

ced

Java

Net

wor

king

. Pr

entic

e H

all P

TR

, Upp

er S

addl

e R

iver

, New

Jer

sey,

199

7

•T

he J

ava

Tut

oria

l (R

MI

chap

ter)

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