Professional Documents
Culture Documents
RMI
Remote Method Invocation
GV: Nguyn Th Thanh Vn
Ni dung
Gii
thiu
Hot ng ca RMI
Hot ng ca lp trung gian
Thit k v ci t mt chng trnh RMI
Thc thi h chng trnh RMI, ex
Chuyn tham s trong RMI, ex
Tham tr
Tham chiu
Tun
t ha i tng, ex
Factory Object, ex
RMI vi firewall
Java Simplified / Session 22 / 2 2
of 45
Gii thiu
Lp
Mi trng hp tc
Tn dng ngun ti nguyn phn tn trn mng
Java:
C .NET ca Microsoft
Trong
Gii thiu
Local
Computer B
A2
B2
A1
B2
B3
Computer C
C1
Java Simplified / Session 22 / 5 5
of 45
RPC : h tr a ngn ng
RMI: ch h tr cc ng dng c vit bng
Java
RMI Client/Server
Client
Server
Mc ch ca RMI
H
Vn pht sinh
Vic
Ph
thuc vo kt ni mng
Java Simplified / Session 22 / 9 9
of 45
Gii php
i
A2
A1
Computer B
B1_Skel
B1_Skel
Computer A
B1_Stub
B1_Stub
C1_Skel
C1_Skel
C1_Stub
Computer C
C1
Java Simplified / Session 22 / 11 11
of 45
gian:
Lp Stub (lp mc): chuyn v cho client
Lp Skeleton (lp ni): Pha Server
Stub
a,b
c
Java Simplified / Session 22 / 13 13
of 45
The Stub and Skeleton layer, which intercepts method calls made
by the client to the interface reference variable and redirects these
calls to a remote RMI service.
The Remote Reference layer understands how to interpret and
manage references made from clients to the remote service objects.
The Transport layer, which is based on TCP/IP connections between
machines in a network. It provides basic connectivity, as well as some
firewall penetration strategies.
Java Simplified / Session 22 / 14 14
of 45
Skeleton
return
bind
Registry
lookup
call
Stub
RMI Client
ng
tip Remote
Naming
RemoteObject class
RemoteObject
RemoteObject class
Cc i tng t xa l th hin ca cc lp con
ca RemoteObject.
RemoteServer class
l lp con ca lp RemoteObject;
l lp cha ca lp UnicastRemoteObject.
UnicastRemoteObject class
l mt lp con c th ca lp RemoteServer.
to ra mt i tng xa:
ta phi tha k lp UnicastRemoteServer
khai bo lp ny thc thi giao tip Remote
Phng thc exportObject(Object): lm cho my
o Java nhn din c i tng Object.
UnicastRemote
Server
Object
RemoteServer
UnicastRemote
Object
UnicastRemote
Server
Remote
Cc
phng thc
Bind() gn mt tn vi mt i tng t xa c th
List() lit k tt c cc tn c ng k vi trnh ng k
Lookup() tm mt i tng t xa c th vi mt URL cho
trc gn vi n
Rebind() gn mt tn vi mt i tng xa khc
Unbind() loi b mt tn c gn cho mt i tng
xa trong trnh ng k
Registry.REGISTRY_PORT l cng mc nh lng nghe
cc cc yu cu. Gi tr mc nh l 1099.
Cc bc thit k v ci t RMI
1. c t mt giao tip t xa (remote interface) trn
server.
2. Hin thc giao tip t xa (remote interface) Xy
dng mt i tng t xa (remote object) .
3. Sinh ra cc Stub pha client v Skeleton pha server.
4. Xy dng chng trnh pha Server
5. Xy dng chng trnh pha Client.
6. Khi ng b ng k RMI (RMI registry)
7. Khi ng cc i tng t xa pha server
8. Chy chng trnh pha client.
Java Simplified / Session 22 / 26 26
of 45
/* CalculatorIm pl.java*/
im p ort java.rm i.*;
p u b lic class C alcu latorIm p l im p lem en ts
C alcu lator {
p u b lic in t ad d N u m (in t x,in t y) th row s
R em oteExcep tion {
System .out.println("Client request to calculate");
retu rn (x+ y);
}
}
Bin dch
Bin dch
/* CalculatorClient.java*/
im p ort java.rm i.*;
p u b lic class C alcu latorC lien t {
p u b lic static void m ain (S trin g [] arg s){
try{
System .out.println("Finding O bject ");
// tm
itng c
n truy xu
t theo tn
Calculator c= (C alcu lator)N am ing.lookup ("rm i://localhost/Van");
// goiphuong thuc cua doituong
System .out.println(c.addN um (5,10));
} catch (Excep tion e) { S ystem .ou t.p rin tln (e); }
}
}
Bin dch
Java Simplified / Session 22 / 32 32
of 45
6. Kch hot b ng k
C:\RMI>rmiregistry.exe
N
Test
Review
Lu cch vn hnh
Da
vo JVM ta c th to ty cc my
tnh o (cho cc ct chy trn cc ca s
DOS khc nhau) giao tip vi nhau gi lp
mng.
c im: cng IP, cng chung th vin
Java, cng 1 chnh sch bo mt -> d vn
hnh.
Khi vn hnh trn cc my tnh vt l khc
nhau cn ch v th vin, security policy
Java Simplified / Session 22 / 38 38
of 45
ci t ca i tng
to b ng k v t ng k i tng:
LocateRegistry.createRegistry(1099);
Calculator cal= n ew C alcu latorIm p l();
System .out.println("Exporting Calculator ! ");
U nicastRem oteO bject.exportO bject(cal);
System .out.println("Registering object ");
N am ing.bind("rm i://localhost/m yCal", cal);
System .out.println(Register Calculator");
System .out.println("W aiting for client request ");
Java Simplified / Session 22 / 41 41
of 45
Cc lp c to ra trong RMI
Cc
lp v phm vi s dng
Server
Client
Cal.class
Cal.class
CalImpl.class
CalClient.class
CalServer.class
CalImpl_Stub.class
CalImpl_Stub.class
CalImpl_Skel.class
(HTML)
hin:
SecurityManager
Khi
// ko phng v
permission java.net.SocketPermission
"*:1099", "connect, accept, resolve";
// This allows RMI clients to contact the
RMIRegistry of any host
};
hiu:
Tt c cc lp i tng ci t trong giao tip
Serializable u c kh nng tun t ha.
Khi
Output
2.
3.
4.
5.
6.
7.
8.
Khi ng rmiregistry
9.
10.
p u b lic void reg isterC lien t(A tC lien t c) th row s R em oteExcep tion
{
client = c;
}
p u b lic void callS erverM eth od (S trin g m es)th row s
R em oteExcep tion {
System .out.println(m es);
for (in t i= 1; i< 10; i+ + ){
String m sg= "Server response "+ i;
//triu giphng thc cu
a
itng chy trn m y khch
Java Simplified / Session 22 / 56 56
of 45
client.callC lientM ethod(m sg); }
//Kh
ito
itng chu
//thng bo kha
nng giao ti
p c t xa
U nicastRem oteO bject.exportO bject(server);
// ng k
itng vi RM I
N am ing.bind("rm i://localhost/serverobject",server);
System .out.println("W aiting for clientrequest ...");
}
}
//K h
i to
i t n g kh ch
AtClient client= n ew A tC lien tIm p l();
//th n g b o kh a
n n g g iao ti
p c t xa
U nicastRem oteO bject.exportO bject(client);
AtServer server=
(AtServer)N am ing.lookup("rm i://localhost/serverobject");
//Tru y
n t.ch i
u cu
a A tC lien t t trn h kh ch ln ch u
//Trn h ch u
ko c
n .looku p tru y tm tc
n A tC lien t
server.registerClient(client);
server.callServerM ethod("Client ContactServer");
}
}
Java Simplified / Session 22 / 58 58
of 45
Note
Java
Tun t ha i tng
lung ny c th c:
gi i qua mt Socket,
lu vo tp tin hoc c
x l di dng mt lung d liu.
Serialization
Tun t ha i tng
Java
Tun t ha i tng
kh tun t ha
Ch
Example Serialization
p u b lic class w riteO b j {
p u b lic static void m ain (S trin g [] arg s) th row s Excep tion {
Junk obj1 = n ew Ju n k("L X u n A n h ");
Junk obj2 = n ew Ju n k("L Q u
c B a");
O bjectO utputStream objectO ut = n ew O b jectO u tp u tS tream (n ew
B uf f
e red O u tp u tS tream (n ew FileO u tp u tS tream ("b .txt")));
objectO ut.w riteO bject(obj1); // W rite object
class Ju n k im p lem en ts
objectO ut.w riteO bject(obj2); // W rite object
S erializab le {
objectO ut.close(); // Close the output stream
String str;
O bjectInputStream objectIn = n u ll;
p u b lic Ju n k(S trin g s) {
in t C ou n t = 0;
str = s; }
Junk object = n u ll;
p u b lic S trin g toS t(){
objectIn = n ew O b jectIn p u tS tream (
retu
rn
n ew B u f f
e red In p u tS tream (n ew FileIn p u tS tream ("b.
txt")
)); str; }
}
w h ile (C ou n t < 2) {
object = (Junk) objectIn.readO bject(); Count+ + ;
System .out.println("O bject " + Count + ": " + object.toStr()); }
objectIn.close();
}
}
Java Simplified / Session 22 / 67 67
of 45
Factory Object
Khi
nhiu i tng c to ra v c ng k vi
rmiregistry => Hn ch:
rmiregistry phi qun l nhiu i tng.
Trnh khch phi nh cc tn i tng.
Gii
NewsImpl
getTodayNews() dng ly v thng tin
TimerImpl.
getCurrentTime(): dng ly ngy gi hin ti trn
Server
M
hnh
Java Simplified / Session 22 / 72 72
of 45
Cc bc trin khai
1.
2.
3.
4.
- lin h vi t FactoryService,
- y/c to 2 t News v Timer (createNews, createTimer)
- gi phng thc ca 2 t (getTodayNews(), getCurrentTime()
6.
Ci t cho FactoryService
p ackag e Factory;
p u b lic class FactoryS erviceIm p im p lem en ts FactoryS ervice{
N ew s new sobject = n ew N ew sIm p ();
Tim er tim erobject = n ew Tim erIm p ();
p u b lic FactoryS erviceIm p (){
try {
U nicastRem oteO bject.exportO bject(new sobject);
U nicastRem oteO bject.exportO bject(tim erobject);
} catch (R em oteExcep tion e) {
e.printStackTrace();
System .out.println(e); }
}
p u b lic N ew s createN ew s() th row s R em oteExcep tion {
retu rn n ew sob ject;
}
p u b lic Tim er createTim er() th row s R em oteExcep tion {
retu rn tim erob ject;
}
}
Java Simplified / Session 22 / 76 76
of 45
Output
gi hm tnh:
ng.
Trnh khch ly tham chiu n i tng bng hm
Naming.lookup() dng tng qut Object, ex
O bject obj= N am ing.lookup("rm i://localhost/O bj");
c t v ci t mt Remote Interface
// Friend.java
im p ort java.rm i.*;
p u b lic in terface Frien d exten d s R em ote {
p u b lic S trin g g reetin g (S trin g n am e) th row s
R em oteExcep tion ;
}
// Friend.java
p u b lic class Frien d Im p im p lem en ts Frien d {
p u b lic S trin g g reetin g (S trin g n am e) th row s
R em oteExcep tion ;
retu rn (H i" + nam e + N ice to m eet you ");
}
}
RMIRegistry v vn Firewall
Gii php
Yu
Tunneling
1.
i
tng
Object
ng
k
vi
RMIRegistry.
3.
4.
Web
Servlet
server
lin
y/c
h
mt
ti
RMIRegistry
i
tng
trung
ly
gian
tham
Servlet
5.
2.
Servlet
My
khch
triu
gi
gi
y/c
Object
truy
xut
Object
quachiu
giao
6.
tr Webserver
v kt qu cho
Webserver
7. Servlet
Cui cng
tr kt
qu vthng
cho trnh
khch
trn
ca my
Object
x l
thc
http ch
n (server)
Webserver.
services:
vi RMI:
Summary
1. xy dng i tng xa ta phi:
-
Truyn d liu gia trnh khch v i tng trn my ch c th thng qua tham tr
v tham chiu:
o Tham chiu: cc i tng phi ci t giao tip Remote
o Tham tr: cc i tng phi ci t giao tip Serialization
Tham chiu t i tng ch n i tng trn trnh khch: tham chiu ngc