1 / 19

Java Programming II

Java Programming II. Java Network II (Distributed Objects in Java). Contents. Distributed Objects Java RMI Communication of Remote Object and Client Writing Java RMI Application Hello Example RMI Structure If room (RMI Callback). Distributed Objects.

tia
Download Presentation

Java Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming II Java Network II (Distributed Objects in Java) Java Programming II

  2. 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

  3. 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

  4. Java Remote Method Invocation (RMI) • Can use objects on remote different run-time 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

  5. 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

  6. Communication of Remote Object and Client JVM JVM Remote Object Client Stub Skeleton RMI Client Application RMI Server Application Network Java Programming II

  7. 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 • Run the Client Java Programming II

  8. Hello Example : RMI Interface import java.rmi.Remote; import java.rmi.RemoteException; public interface Hello extends Remote { String sayHello() throws RemoteException; } Java Programming II

  9. Hello Example : RMI “HelloImpl” object Implementation (Server) import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import 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()); } try { 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

  10. 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

  11. Hello Example : RMI File “policy” grant { // Allow everything for now permission java.security.AllPermission; }; Start Registry Server & Run Server and Client Start rmiregistry. Default port is 1099. % rmiregistry & % java –Djava.security.policy=policy HelloImpl % javac examples/hello/HelloClient.java % java [–Djava.security.policy=policy] HelloClient Run the RMI Server Compile the Client Please ensure there is the “policy” file in the current directory Run the Client Java Programming II

  12. 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

  13. Stub and Skeleton JVM JVM Remote Method Invocation Remote Method Invocation Remote Object Client Stub Skeleton Data Transformation Remote Method Return RMI Client Application RMI Server Application Unmarshalling Unmarshalling Marshalling Marshalling Object Serialization Network Java Programming II

  14. 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

  15. JRMP Class Marshall/Unmarshall (Object Serialization) Virtual connection Network connection Creation of reference to remote object Managing connection to remote object Java Programming II

  16. 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/ByTopics/RMI/RMI-ComputeEngine” • A Simple Callback Example Java Programming II

  17. 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

  18. RMI Callback Example RMI Server (CallbackImpl) import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import 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

  19. RMI Callback Example Callback Server import java.rmi.Remote; import java.rmi.RemoteException; public class CallbackServer { 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; } } } Code for callback passed from the caller routine Java Programming II

More Related