Java programming JavaNetwork II

19 171 0
Java programming JavaNetwork II

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Java Programming II Java Network II (Distributed Objects in Java) Java Programming II Contents  Distributed Objects  Java RMI  Communication of Remote Object and Client  Writing Java RMI Application  Hello Example  RMI Structure  If room (RMI Callback) Java Programming II Distributed Objects  Objects that can communicate with objects on heterogeneous run-time environments  Distribute Objects Standard Protocol – ex: JRMP  Robust  Reliable  Transparent  Distributed Objects Technology  Multi-Platform  Transparent access to distributed objects  Language Neutral: RMI, CORBA, DCOM Java Programming II Java Remote Method Invocation (RMI)  Can use objects on remote different runtime environments as like objects on a local run-time environment  Abstraction of low-level network code on distributed network to provide developers an environment where they focus on their application development Java Programming II Introduction to RMI Distributed Processing on Network Define of Remote Interface Object Serialization java.rmi and java.rmi.server Create Stub and Skeleton Java Programming II Communication of Remote Object and Client JVM Client JVM Stub Skeleton RMI Client Application Remote Object RMI Server Application Network Java Programming II Writing Java RMI Application  Writing RMI Application  Definition of Remote Interface  Definition of Remote Implementation Class  Write RMI Server Application  Write Client Application  Compile and Run the Application  Compilation of the Implementation Class  Creation of Stub and Skeleton using “rmic” command  Compilation of the Server Application  Run the RMI Registry and Start the Server Program  Compilation of the Client Program Java Programming II Hello Example : RMI Interface import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote { String sayHello() throws RemoteException; } Java Programming II Hello Example : RMI “HelloImpl” object Implementation (Server) try { import import import import java.rmi.Naming; java.rmi.RemoteException; java.rmi.RMISecurityManager; java.rmi.server.UnicastRemoteObject; public class HelloImpl extends UnicastRemoteObject implements Hello { public HelloImpl() throws RemoteException { super(); } public String sayHello() { return "Hello World!"; } public static void main(String args[]) { // Create and install a security manager if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } HelloImpl obj = new HelloImpl(); // Bind this object instance to the name "HelloServer" Naming.rebind("HelloServer", obj); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloImpl err: " + e.getMessage()); e.printStackTrace(); } } } Compile & Skeleton Creation : % javac Hello.java % javac HelloImpl.java % rmic HelloImpl Java Programming II Hello Example : RMI A Client Application import java.rmi.Naming; import java.rmi.RemoteException; public class HelloClient { public static void main(String args[]) { String message = "Hello: This is my test message"; // "obj" is the identifier that we'll use to refer // to the remote object that implements the "Hello" // interface Hello obj = null; try { obj = (Hello)Naming.lookup("//" + "/HelloServer"); message = obj.sayHello(); } catch (Exception e) { System.out.println("HelloClient exception: " + e.getMessage()); e.printStackTrace(); } System.out.println("Message = " + message); } // end of main } // end of HelloClient Java Programming II 10 Hello Example : RMI File “policy” grant { // Allow everything for now permission java.security.AllPermission; }; Start Registry Server & Run Server and Start rmiregistry Client % rmiregistry & % java –Djava.security.policy=policy HelloImpl % javac examples/hello/HelloClient.java % java [–Djava.security.policy=policy] HelloClient Please ensure there is the “policy” file in the current directory Default port is 1099 Run the RMI Server Compile the Client Run the Client Java Programming II 11 RMI Structure  Protocol Java Remote Method Protocol (JRMP) • For distribute object environment by pure Java environment  RMI/IIOP: Can communicate with CORBA  JRMP  Communication Mechanism between Stub and Skeleton  Object Serialization: Types of parameters and return type of a remote method should follow Java serialization mechanism Java Programming II 12 Stub and Skeleton JVM JVM Remote Method Invocation Remote Method Invocation Client Data Transformation Stub Skeleton Remote Method Return Unmarshalling Object Serialization RMI Server Application Unmarshalling RMI Client Application Marshalling Remote Object Marshalling Network Java Programming II 13 Stub and Skeleton  Stubs and Skeleton  When RMI client invokes a remote method of a remote object, it uses stub reference of the remote object instead of remote object reference  For marshalling and unmarshalling of stub and skeleton, object serialization and deserialization are used  Condition of Object for Object Serialization • Object should implementjava.io.Serialization interface • Built-in types can be serialized basically • Member variables should implement the Serializable interface or be declared as transient • Static member variables can not be serialized Java Programming II 14 JRMP Class Marshall/Unmarshall (Object Serialization) Virtual connectio n Network connection Managing connection to remote object Java Programming II Creation of reference to remote object 15 Callback between Client and Server  Implementation of callback between a client and a server through reference passing  What is callback? • Usually, a server provide service objects with methods When a client invoke a method of the server, the server executes the invocation and return result to the client • For the callback, a client (or invoker) provide services (methods) for server (or program to be invoked) The callback is that the server (callee) invokes client (caller)’s methods  If you are interested in an example for callback to a client’s method, refer to the “/home/course/java2/codes/RMI/RMI-ComputeEngine”  A Simple Callback Example Java Programming II 16 RMI Callback Example Interface import java.rmi.Remote; import java.rmi.RemoteException; public interface Callback extends Remote { public String speakJapanese() throws RemoteException; public String speakEnglish() throws RemoteException; public String greeting(String s) throws RemoteException; } Java Programming II 17 RMI Callback Example RMI Server (CallbackImpl) import import import import java.rmi.Naming; java.rmi.RemoteException; java.rmi.RMISecurityManager; java.rmi.server.UnicastRemoteObject; public class CallbackImpl extends UnicastRemoteObject implements Callback { public CallbackImpl() throws RemoteException { super(); } public String greeting(String lang) throws RemoteException { CallbackServer server = new CallbackServer(); return server.sayHello(this, lang); } public String speakJapanese(){ return new String("Konnichiwa!"); } public String speakEnglish(){ return new String("How are you!"); } public static void main(String args[]) { // Create and install a security manager if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try { CallbackImpl obj = new CallbackImpl(); // Bind this object instance to the name "MyCallbackServer" Naming.rebind("MyCallbackServer", obj); System.out.println("MyCallbackServer bound in registry"); } catch (Exception e) { System.out.println("CallbackImpl err: " + e.getMessage()); e.printStackTrace(); } } } Call sayHello with “this” for callback routine Java Programming II 18 RMI Callback Example Callback Server import java.rmi.Remote; import java.rmi.RemoteException; public class CallbackServer { Code for callback passed from the caller routine public String sayHello(Callback callback, String lang) throws RemoteException { String message = null; if(lang.equals("JAPANESE")) { message = callback.speakJapanese(); System.out.println("In CallbackServer, " + message); return message; } else { message = callback.speakEnglish(); System.out.println("In CallbackServer, " + message); return message; } } } Java Programming II 19 ... Skeleton Creation : % javac Hello .java % javac HelloImpl .java % rmic HelloImpl Java Programming II Hello Example : RMI A Client Application import java. rmi.Naming; import java. rmi.RemoteException;... } Java Programming II Hello Example : RMI “HelloImpl” object Implementation (Server) try { import import import import java. rmi.Naming; java. rmi.RemoteException; java. rmi.RMISecurityManager; java. rmi.server.UnicastRemoteObject;... development Java Programming II Introduction to RMI Distributed Processing on Network Define of Remote Interface Object Serialization java. rmi and java. rmi.server Create Stub and Skeleton Java Programming

Ngày đăng: 27/10/2017, 11:13

Mục lục

  • Slide 1

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan