Kĩ thuật lập trình - Java RMI

1. Định nghĩa giao diện 2. Phát triển đối tượng bằng cách cài đặt giao diện remote 3. Phát triển chương trình client, server. 4. Biên dịch tập tin java. 5. Tạo ra đối tượng stub và skeleton. 6. Khởi động RMI registry. 7. Chạy các đối tượng remote server 8. Chạy client

ppt12 trang | Chia sẻ: nguyenlam99 | Lượt xem: 1015 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Kĩ thuật lập trình - Java RMI, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
*Java RMI Mục đíchHỗ trợ gọi phương thức từ xa trên các đối tượng trong các máy ảo khác nhauTích hợp mô hình đối tượng phân tán vào Java Làm cho sự khác biệt giữa mô hình đối tượng phân tán và mô hình đối tượng cục bộ không có sự khác biệt.Tạo ra các ứng dụng phân tán có độ tin cậy một cách dễ dàngDuy trì sự an toàn kiểu được cung cấp bởi môi trường thời gian chạy của nền tảng JavaMô hình gọi đối tượng từ xa - RMILocal Machine (Client)SampleServer remoteObject;int s;s = remoteObject.sum(1,2);System.out.println(s);Remote Machine (Server)public int sum(int a,int b) { return a + b;}1,23Kiến trúc RMIJava RMI LayersTCPRemote Reference LayerTransport LayerJava Virtual MachineClient ObjectRemote Reference LayerTransport LayerJava Virtual MachineStubRemote ObjectSkeletonThe Stub and SkeletonMô hình các đối tượng phân tánRemoteRemoteServerRemoteObjectRemoteExceptionIOExceptionUnicastRemoteObjectInterfacesclassesextensionimplementationObjectStubRemote InterfaceServer ImplCác bước tạo RMI 1. Định nghĩa giao diện2. Phát triển đối tượng bằng cách cài đặt giao diện remote3. Phát triển chương trình client, server.4. Biên dịch tập tin java.5. Tạo ra đối tượng stub và skeleton.6. Khởi động RMI registry.7. Chạy các đối tượng remote server8. Chạy clientBước 1. Định nghĩa giao diện/* SampleServer.java */import java.rmi.*;public interface SampleServer extends Remote{ public int sum(int a,int b) throws RemoteException;}Bước 2. Phát triển đối tượng bằng cách cài đặt giao diện remote/* SampleServerImpl.java */import java.rmi.*;import java.rmi.server.*;import java.rmi.registry.*;public class SampleServerImpl extends UnicastRemoteObject implements SampleServer{ SampleServerImpl() throws RemoteException { super(); }public int sum(int a,int b) throws RemoteException { return a + b; }}Bước 3. Phát triển chương trình 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()); try{ //get the remote object from the registry 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()); } }}Bước 3. Phát triển chương trình server./* 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()); } }

Các file đính kèm theo tài liệu này:

  • pptchuong_9_rmi_8977.ppt