Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 52 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
52
Dung lượng
318,5 KB
Nội dung
Môn Lập trình Java NC Môn Lập trình Java NC Chương 4: RemoteObjectsChương 4: RemoteObjects 2Object Oriented Programming 2 - Chapter 5: Remo te Objects Content Content 1. Introduction: The Roles of Client and Server 1. Introduction: The Roles of Client and Server 2. Setting up Remote Method Invocation 2. Setting up Remote Method Invocation 3. Parameters passing in Remote methods 3. Parameters passing in Remote methods 4. Using RMI with Applets 4. Using RMI with Applets 5. Server Objects Activation 5. Server Objects Activation 3Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server and Server 4Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) Review Review In the traditional client/server model, the request from In the traditional client/server model, the request from client is translated to an intermediary format. client is translated to an intermediary format. The server parses the request format, computes the The server parses the request format, computes the response, and formats the response for transmission response, and formats the response for transmission to the client. to the client. The client then parses the response and displays it to The client then parses the response and displays it to the user. the user. 5Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) “ “ The network is the computer” The network is the computer” Consider the following program organization: Consider the following program organization: If the network If the network is is the computer, we ought to be able the computer, we ought to be able to put the two classes on different computers to put the two classes on different computers RMI is one technology that makes this possible RMI is one technology that makes this possible SomeClass AnotherClass method call returned object computer 1 computer 2 6Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) Terminology Terminology A A remote object remote object is an object on another computer is an object on another computer The The client object client object is the object making the request is the object making the request (sending a message to the other object) (sending a message to the other object) The The server object server object is the object receiving the request. is the object receiving the request. As usual, “client” and “server” can easily trade roles As usual, “client” and “server” can easily trade roles (each can make requests of the other). (each can make requests of the other). 7Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) Terminology (cont.) Terminology (cont.) The The rmiregistry rmiregistry is a special server that looks up is a special server that looks up objects by name. objects by name. Hopefully, the name is unique! Hopefully, the name is unique! rmic rmic is a special compiler for creating is a special compiler for creating stub stub (client) (client) and and skeleton skeleton (server) classes. (server) classes. 8Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) Stubs Stubs When client code wants to invoke a remote method on When client code wants to invoke a remote method on a remote object, it actually calls an ordinary method of a remote object, it actually calls an ordinary method of the Java programming language that is encapsulated the Java programming language that is encapsulated in a surrogate object called a in a surrogate object called a stub. stub. The stub resides on the client machine, not on the The stub resides on the client machine, not on the server. The stub packages the parameters used in the server. The stub packages the parameters used in the remote method into a block of bytes. remote method into a block of bytes. 9Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) Parameter Marshalling Parameter Marshalling The process of encoding the parameters is called The process of encoding the parameters is called parameter marshalling. parameter marshalling. The purpose of parameter marshalling is to convert The purpose of parameter marshalling is to convert the parameters into a format suitable for transport from the parameters into a format suitable for transport from one virtual machine to another. one virtual machine to another. 10Object Oriented Programming 2 - Chapter 5: Remo te Objects 1. Introduction: The Roles of Client 1. Introduction: The Roles of Client and Server (cont.) and Server (cont.) RMI Architecture RMI Architecture The stub method on the client builds an information The stub method on the client builds an information block that consists of: block that consists of: An identifier of the remote object to be used; An identifier of the remote object to be used; A description of the method to be called; A description of the method to be called; The marshalled parameters. The marshalled parameters. The stub then sends this information to the server. The stub then sends this information to the server. [...]... Must be public Must extend the interface java.rmi .Remote Every method in the interface must declare that it throws java.rmi.RemoteException (other exceptions may also be thrown) Object Oriented Programming 2 - Chapter 5: Remo 18 2 .4 Create Interface import java.rmi.*; public interface HelloInterface extends Remote { public String say() throws RemoteException; } View sample HelloInterface.java... 19 2.5 Classes A Remote class is one whose instances can be accessed remotely On the computer where it is defined, instances of this class can be accessed just like any other object On other computers, the remote object can be accessed via object handles A Serializable class is one whose instances can be marshaled It probably isn’t a good idea for an object to be both remote and serializable... (used only by the server): Must implement a Remote interface Should extend java.rmi.server.UnicastRemoteObject May have locally accessible methods that are not in its Remote interface View sample Hello.java Object Oriented Programming 2 - Chapter 5: Remo 21 2.5 Classes (cont.) import java.rmi.*; import java.rmi.server.*; public class Hello extends UnicastRemoteObject implements HelloInterface {... call Or, if the remote method threw an exception, the stub rethrows it in the process space of the caller Object Oriented Programming 2 - Chapter 5: Remo 12 1 Introduction: The Roles of Client and Server (cont.) RMI Architecture (cont.) Object Oriented Programming 2 - Chapter 5: Remo 13 2 Setting up Remote Method Invocation 2.1 What is needed for RMI 2.2 Processes 2.3 Interfaces 2 .4 Create Interface... String message; // Strings are serializable public Hello (String msg) throws RemoteException{ message = msg; } public String say() throws RemoteException { return message; } } Object Oriented Programming 2 - Chapter 5: Remo 22 2.5 Classes (cont.) – Server Class The class that defines the server object should extend UnicastRemoteObject This makes a connection with exactly one other computer If... other class, you can use exportObject() instead Sun does not provide a MulticastRemoteObject class The server class needs to register its server object: String url = "rmi://" + host + ":" + port + "/" + objectName; The default port is 1099 Naming.rebind(url, object); Every remotely available method must throw a RemoteException (because connections can fail) Object Oriented Programming 2 - Chapter... is ready."); }catch(Exception e) { System.out.println("Hello Server failed: " + e); } } } View sample HelloWorldServer.java Object Oriented Programming 2 - Chapter 5: Remo 24 2.5 Classes (cont.) The client class Acquires remoteobjects from naming service Naming.lookup (“rmi://host:port/serviceName”); View sample HelloClient.java Object Oriented Programming 2 - Chapter 5: Remo 25 2.5 Classes (cont.)... 2.6 Security 2.7 Build and execute program with RMI 2.8 Install an example 2.9 Compile and execute program 2.10 rmiregistry Object Oriented Programming 2 - Chapter 5: Remo 14 2.1 What is needed for RMI To send a message to a remote “server object,” The “client object” has to find the object Do this by looking it up in a registry The client object then has to marshal the parameters (prepare... processes The Client The Server The Object Registry, rmiregistry, which is like a DNS service for objects You also need TCP/IP active Object Oriented Programming 2 - Chapter 5: Remo 16 2.3 Interfaces Interfaces define behavior Classes define implementation Therefore, In order to use a remote object, the client must know its behavior (interface), but does not need to know its implementation... Remo 27 2.6 Security (cont.) Permissions for RMI grant { permission java.net.SocketPermission “*:10 24- 65535”,”connect”; permission java.io.FilePermission “downloadDir”,”read”; } View policy file : client.policy Object Oriented Programming 2 - Chapter 5: Remo 28 2.7 Build and execute program with RMI 1 2 3 4 5 Compile all java file as normal Using rmic program to generate Stub file Start rmiregistry program