You are on page 1of 26

Lp

trnh phn tn: RMI

Remote Invoke Method


RMI l mt c ch cho php mt i tng ang chy trn mt my o Java ny ( Java Virtual Machine) gi cc phng thc ca mt i tng ang tn ti trn mt my o Java khc (JVM) RMI to ra cc ng dng phn tn c Sn cy mt cch d dng

RMI
Server: Cung cp dch v RMI (phng thc t xa) Client: Gi cc phng thc t xa c cung cp bi server.

RMI - V d
Local Machine (Client)
SampleServer remoteObject; int s; s = remoteObject.sum(1,2);

Remote Machine (Server)

1,2
public int sum(int a,int b) { return a + b; }

System.out.println(s);

Kin trc RMI


Remote Machine bind RMI Server Registry skeleton

return

call

lookup

stub

RMI Client

Local Machine

Truyn Sn trong RMI


RMI s dng cc lp trung gian truyn Sn: Skeleton v Stub Lp Stub dng client. Lp Skeleton dng pha server. Java s dng rmic.exe to cc lp trung gian. TCP Socket

Hot ng ca RMI
Server RMI phi ng k vi mt dch v tra rm v ng k tn (rmiregistry) Sau khi server c ng k, n s ch cc yu cu RMI t cc client Nu mt dch v chuyn t server ny sang mt server khc, client ch cn tra rm trnh ng k rm ra v tr mi Cc client RMI s gi cc thng ip RMI gi mt phng thc trn mt i tng t xa

Hot ng ca RMI
ng dng client yu cu mt tn dch v c th, v nhn mt URL tr ti ti nguyn t xa rmi://hostname:port/servicename

Stub
call
skeleton

RMI Client

Stub

RMI Server

return

Stub: mt i tng y quyn, truyn ti yu cu i tng ti server RMI Ngi pht trin ng dng khng cn quan tm n ti nguyn RMI nm u, n ang chy trn nn no, n p ng y yu cu nh th no -> Client RMI gi mt phng thc trn i tng y quyn

Skeleton
call
skeleton

RMI Client

Stub

RMI Server

return

Skeleton c nhim v lng nghe cc yu cu RMI n v truyn cc yu cu ny ti dch v RMI Skeleton khng cung cp bn ci t ca dch v RMI. N ch ng vai tr nh l chng trnh nhn cc yu cu, v truyn cc yu cu

Computer A

A1

C1- stub

Computer C C1 Skel

A2


B1_stub


C1

B1Skel


B1


Computer B

java.rmi.server.* java.rmi.*

Java classes
java.rmi.Remote
public interface Remote:
1. public interface BankAccount extends java.rmi.Remote { 2. public void deposit(oat amount) 3. throws java.rmi.RemoteExcepSon; 4. public void withdraw(oat amount) 5. throws OverdrawnExcepSon, java.rmi.RemoteExcepSon; 6. public oat getBalance() 7. throws java.rmi.RemoteExcepSon; 8. }

Cc bc pht trin mt h thng RMI


1. nh ngha mt giao din remote 2. Pht trin i tng remote, i tng ny thc thi giao din remote 3. Pht trin chng trnh client. 4. Bin dch source codes. 5. To cc client stubs v server skeletons. 6. Khi ng RMI registry. 7. Khi ng cc i tng server remote 8. Chy chng trnh client

Bc 1. Xc nh giao din Remote


to ng dng RMI, bc 1 l nh ngha mt giao din remote gia cc i tng client v server
/* SampleServer.java */ import java.rmi.*; public interface SampleServer extends Remote { public int sum(int a,int b) throws RemoteException; }

Bc 2. Pht trin i tng remote v giao din


Server l mt server unicast remote n gin To server k tha bng vic k tha java.rmi.server.UnicastRemoteObject. Server s dng RMISecurityManager bo v ti nguyn trong truyn thng t xa.
/* SampleServerImpl.java */ import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class SampleServerImpl extends UnicastRemoteObject implements SampleServer { SampleServerImpl() throws RemoteException { super(); }

Bc 2. Pht trin i tng remote v giao din


Thc thi cc phng thc remote
/* SampleServerImpl.java */ public int sum(int a,int b) throws RemoteException { return a + b; } }

Server phi ng k tn vi registry, client s rm kim tn server. S dng lp Use java.rmi.Namingclass ng k tn server vi registry. Trong v d ny, tn server l SAMPLE-SERVER. Trong phng thc main ca i tng server, RMI security manager c to v ci t.

Bc 2. Pht trin i tng remote v giao din


/* SampleServerImpl.java */ public static void main(String args[]) { try { System.setSecurityManager(new RMISecurityManager()); //set the security manager //create a local instance of the object SampleServerImpl Server = new SampleServerImpl(); //put the local instance in the registry Naming.rebind("SAMPLE-SERVER" , Server); System.out.println("Server waiting....."); } catch (java.net.MalformedURLException me) { System.out.println("Malformed URL: " + me.toString()); } catch (RemoteException re) { System.out.println("Remote exception: " + re.toString()); }

Bc 3. Pht trin chng trnh client


to i tng client gi cc phng thc trn server, client phi rm kim tn ca server trong registry. S dng lpjava.rmi.Naming tra cu tn server. Tn server c xc nh nh mt URL vi nh dng ( rmi://host:port/name ) Cng RMI mc nh 1099. Tn xc nh trong URL phi ging tn c server ng k vi registry. Trong v d ny, tn server l: SAMPLE-SERVER Gi phng thc t xa: remoteObject.sum

Bc 3. Pht trin chng trnh client


import java.rmi.*; import java.rmi.server.*; public class SampleClient { public static void main(String[] args) { // set the security manager for the client System.setSecurityManager(new RMISecurityManager()); //get the remote object from the registry try { System.out.println("Security Manager loaded"); String url = "//localhost/SAMPLE-SERVER"; SampleServer remoteObject = (SampleServer)Naming.lookup(url); System.out.println("Got remote object"); System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) ); } catch (RemoteException exc) { System.out.println("Error in lookup: " + exc.toString()); } catch (java.net.MalformedURLException exc) { System.out.println("Malformed URL: " + exc.toString()); } catch (java.rmi.NotBoundException exc) { System.out.println("NotBound: " + exc.toString()); } } }

Bc 4 v 5: Bin dch le m ngun Java & to ra client stubs v server skeletons


elpis:~/rmi> elpis:~/rmi> elpis:~/rmi> elpis:~/rmi> elpis:~/rmi> javac SampleClient.java set CLASSPATH=~/rmi javac SampleServer.java javac SampleServerImpl.java rmic SampleServerImpl

Bc 6. Khi ng RMI registry


Cc ng dng RMI cn ci t vi Registry. Registry c khi ng bng lnh: rmiregisty. rmiregistry s dng cng mc nh 1099. C th gn rmiregistry ti cc cng khc bng lnh: rmiregistry <new port>
elpis:~/rmi> rmiregistry

Windows:
> start rmiregistry

Steps 7 & 8: Start the remote server objects & Run the client
Khi Registry c chy, server c th c khi ng v s c th c lu tr trong Registry. V nh bo mt ca Java, cn phi thit lp mt chnh sch bo mt cho RMI bng thit lp java.security.policy to the le policy.all
elpis:~/rmi> java Djava.security.policy=policy.all SampleServerImpl elpis:~/rmi> java Djava.security.policy=policy.all SampleClient

Java Policy File


Trong v d ny, thit lp le policy.all m Java c ton quyn:
grant { permission java.security.AllPermission; };

Mt v d khc v gn quyn truy cp:


grant { permission java.io.filePermission /tmp/*, read, write; permission java.net.SocketPermission somehost.somedomain.com:999,connect; permission java.net.SocketPermission *: 1024-65535,connect,request; permission java.net.SocketPermission *:80,connect; };

Comment for the Java Policy File


1. allow the Java code to read/write any les only under the /tmp directory, includes any subdirectories 2. allow all java classes to establish a network connecSon with the host somehost.somedomain.com on port 999 3. allows classes to connecSon to or accept connecSons on unprivileged ports greater than 1024 , on any host 4. allows all classes to connect to the HTTP port 80 on any host. You can obtain complete details by following links:
http://java.sun.com/products//jdk/1.2/docs/guide/security/ spec/security-spec.doc3.html

You might also like