1 / 31

Technical Stream Session 1: Introduction to Java Remote Method Invocation

CSC 253 Paul Grace. Technical Stream Session 1: Introduction to Java Remote Method Invocation. Distributed Systems. Overview of the Session. Introduce the architecture of Java RMI Demonstrate how to build a simple distributed system using Java RMI

colum
Download Presentation

Technical Stream Session 1: Introduction to Java Remote Method Invocation

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. CSC 253 Paul Grace Technical Stream Session 1:Introduction to Java Remote Method Invocation Distributed Systems

  2. Overview of the Session • Introduce the architecture of Java RMI • Demonstrate how to build a simple distributed system using Java RMI • Investigate the advanced features of Java RMI • Objective:To introduce the skills required to do the coursework assignments

  3. Additional Reading Material • Oberg,R., "Mastering RMI", John Wiley & sons, 2001. • 2 copies in the library • The JGuru short course on RMI • On-line tutorial • Link available from the course web site

  4. The Goal of RMI • To extend the Java Object model to support programming with distributed Objects • The intention is to make distributed programming as easy as standard Java programming • Focus on application logic not distribution See lecture on RPC and RMI 4

  5. A Simple Overview • Java RMI allows one Java object to call methods on another Java object in a different JVM Method parameters Client JVM Local Object Remote Object Result or exception Server JVM 5

  6. Distributed Programming • Java RMI is interface based • A remote object (or distributed service) is specified by its interface • “interfaces define behaviour and classes define implementations” • Termed Remote Interfaces Interface Implementation Client Program Server Program RMI System 6

  7. Distributed Programming (cont.) • Before you invoke a method on a remote object you need a reference to this object • Look for an object with a specific interface type • There are many ways to find this information • Discovery Protocols • Naming Service e.g. RMI Registry See lecture on Jini 7

  8. The RMI Registry • The RMI Registry is a naming service • Separately Running service • Initiated using Java’s “rmiregistry” tool • Server programs register remote objects • Give the object a name it can be found using • Client programs lookup object references that match this service name • Registry names have a URL format • rmi://<hostname>:<port>/<ServiceName> • E.g. rmi://localhost:1099/CalculatorService • E.g. rmi://194.80.36.30:1099/ChatService 8

  9. The RMI Registry Interface 9

  10. Lookup in Java RMI Interface Remote Object Client Program Server Program naming.lookup(“rmi://localhost:1099/ TestService”) naming.rebind(“rmi://localhost:1099/TestS ervice”, RemoteObjectReference) Server Client RMIRegistry Local Machine 10

  11. The RMI Architecture 11

  12. Stubs and skeletons are generated from the remote interface Using the “rmic” Java tool Stub communicates with a skeleton rather than the remote object This a Proxy approach Marshalls the parameters and results to be sent across the wire After java 1.1 skeletons were made obsolete and RMI now uses reflection to direct a request to an object Interface Client Server Stub Skel Stubs and Skeleton Layer 12

  13. Parameter Passing • Parameter Passing in Java RMI is different from standard Java • Reminder: In Java, primitives are passed by value, Objects are passed by reference • In Java RMI • Objects and primitives are passed by value • Remote objects are passed by reference • You must be aware of the differences! • Otherwise you'll generate logical bugs that are difficult to identify 13

  14. Parameter Passing (2) • RMI-Pass by Value • All ordinary objects and primitives are serialised and a copy is passed • Any changes to the copy do not affect the original • It is your job to ensure your Objects can be serialised! • RMI-Pass by Reference • Remote Object is the parameter, a stub (reference) is sent • the stub is used to modify the object, the original object is modified 14

  15. Server Chas: Hello Dave: ASL? ChatServerImpl Login(..) Chat(..) ChatServer ChatServer ChatServer Client A Client B A Chat Server Example 15

  16. Building a Java RMI system An RMI system must be composed of the following parts: 1. An interface definition of the remote services; 2. The implementations of the remote services; 3. Stub and skeleton files; 4. A server to host the remote services; 5. An RMI Naming service 6. A client program that uses the remote services. 16

  17. Step 1 - Define the Remote Interface • Declare the methods you'd like to call remotely • This interface must extend java.rmi.Remote • Each method must declare java.rmi.RemoteException in its throws clause • You can have multiple remote interfaces • Remember about parameter passing • Remote objects must be passed as remote interface types • Local objects must be serializable 17

  18. Example ChatServer Interface public interface ChatServer extends java.rmi.Remote { public void login(String name, String password) throws java.rmi.RemoteException; public void logout(String name) throws java.rmi.RemoteException; public void chat(String name, String message) throws java.rmi.RemoteException; } 18

  19. Step 2 - Implement the remote service • Your class must implement the Remote interface • Extend this class with UnicastRemoteObject • Must provide a constructor that throws a RemoteException. • Call super() in the constructor • This activates code in UnicastRemoteObject that performs the RMI linking and remote object initialization. 19

  20. Example Remote Object (ChatServiceImpl) public class ChatServerImpl extends java.rmi.server.UnicastRemoteObject implements ChatServer{ public ChatServerImpl() throws java.rmi.RemoteException { Super(); } public void login(String name, String pass) throws java.rmi.RemoteException{ // Method Implementation } public void logout(String name) throws java.rmi.RemoteException{ // Method Implementation } public void chat(String name, String msg) throws java.rmi.RemoteException{ // Method Implementation } } 20

  21. Step 3 – Generate Stubs & Skeletons • Generate the Remote Interface stub • This stub contains information that allows it to connect to a remote object, which contains the implementation of the methods • RMI provides a tool called rmic to generate stubs • Use rmic on the remote object • e.g. rmic ChatServerImpl • The files: *impl_Stub.class and *impl_Skel.class will be created ChatServerImpl_Stub.class ChatServer RMIC ChatServerImpl_Skel.class 21

  22. Step 4 – Create the Server • The server is a Java application • Creates one or more instances of remote objects • Binds at least one of the remote objects to a name in the RMI registry • Uses the Naming.rebind() operation 22

  23. Example Chat Server public class ChattingServer { public ChattingServer() { try { ChatServer c = new ChatServerImpl(); Naming.rebind("rmi://localhost/ChatService", c); } catch (Exception e) { System.out.println("Server Error: " + e); } } public static void main(String args[]) { //Create the new Calculator server new ChattingServer(); } } Create the remote object RMIRegistry Register the object 23

  24. Step 5 – Create the Client • Get a remote reference by calling Naming.lookup() • Remember - Lookup by service name • Receives a stub object for the requested remote object • Loads code for the stub either locally or remotely • Invoke methods directly on the reference • Much like on standard Java objects Stub class File System OR Network 24

  25. Example Chat Client public class calculatorclient { public static void main(String[] args) { try { // Get a reference to the remote object through the rmiregistry ChatServer c = (ChatServer) Naming.lookup("rmi://localhost/ChatService"); // Now use the reference c to call remote methods c.login(“Chas”,”*****”); c.chat(“Chas”, “Hello”); // Catch the exceptions that may occur - rubbish URL, Remote exception } catch (RemoteException re) { System.out.println("RemoteException“+re); } } } Interface 25

  26. Java RMI Advanced Features • Serialisation • Callbacks • Remote Activation 26

  27. Serialisation • To pass user created objects as parameters in RMI they must be serialisable • This is easy in Java – simply make the class implement the Serializable interface • If you want to optimise the serialisation you can overide the methods of serializable with your own implementation e.g. ObjectInput(Output)Stream • Transforming an Object in a stream of bytes • Can be sent across the network BYTES 27

  28. Callbacks • In many applications the server may want to callback the client • All messages in the chat system are displayed on a central server • You could change the server to callback every client with each new chat message allowing the message to be displayed on each client • Callbacks are just a reverse RMI • You create a remote object on the client and pass this reference to the server, who can invoke it directly 28

  29. Chas: Hello Dave: ASL? Chas: Hello Dave: ASL? Server ChatServerImpl Display(Name. Msg) Login(.., Callback Ref) Chat(..) ChatServer ChatServer Callback ChatServer Callback Callback Impl Callback Impl Client A Client B Using Callbacks in Chat 29

  30. Remote Activation • Server hosting remote objects continuously execute • This wastes system resources • Ideally, a Remote Object would be dormant until it was invoked • Java RMI Remote Activation provides this process • Rather than a server program, you register each object with the rmid daemon • Unfortunately this is complex to program! • See on-line tutorials on web site to find out how to do it • Ask the demonstrators 30

  31. Security Disclaimer • I haven't talked about security • Francois will introduce RMI security next week • When you build distributed systems in practice you must include security implementation See technical session 2 lecture 31

More Related