1 / 40

Remote Method Invocation

Remote Method Invocation. Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University. Contents. RMI Concept from Java Network Programming RMI (Sun) http://java.sun.com/j2se/1.3/docs/guide/rmi/index.html

Download Presentation

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. Remote Method Invocation Instructors: Fu-Chiung Cheng (鄭福炯) Associate Professor Computer Science & Engineering Tatung University

  2. Contents • RMI Concept from Java Network Programming • RMI (Sun) • http://java.sun.com/j2se/1.3/docs/guide/rmi/index.html • http://developer.java.sun.com/developer/onlineTraining/rmi/RMI.html • Examples: • JBuilder Simple RMI • RMI Calculator

  3. RMI Concepts • Two fundamental network applications • File and data transfer (FTP, SMTP (email), HTTP, …) • Allowing one host to run programs on another host (telnet, rlogin Remote procedure call (RPC), CGI.. • Remote Method Invocation (RMI) allows Java programs call other Java programs on the other machines. • RMI == > distributed/parallel computing

  4. Remote Method Invocation • The remote method invocation API let Java objects on different hosts communicate with each other • A remote lives on a server • Each remote object implements a remote interface that specifies which of its methods can be invoked by clients. • Clients invoke the methods of the remote object almost exactly as they invoke local mthods.

  5. Remote Method Invocation • Example: • An object running on a local client can pass a database query as a String argument to a method in a database object running on a remote server to ask it to sum up a series of records. • The server can return the result to the client as a double. • This is more efficient than download all the records and summing them up locally.

  6. Remote Method Invocation • From the programmer’s perspective, remote objects and methods work just like the local objects and methods • All the implementation details are hidden. • You just • import one package, • look up the remote object in a registry and • make sure that you catch the RemoteException when you call the object’s methods

  7. Remote Method Invocation • A remote object is an object whose methods may be invoked from a different Java virtual machine. • Each remote object implements one or more remote interfaces that declare which methods of the remote object can be invoked by the foreign system. • RMI is the facility by which a Java program running on one machine can invoke a method in an object on a complete different machine.

  8. Security • The prospect of remote hosts invoking methods in the local host’s object raises many security issues • The activities that a remote object can perform are limited in much the same way an applet’s activity is limited • A Security Manager object checks all operations to make sure they’re allowed • Custom security managers can be defined for specific applications

  9. Security • Public key authentication can be used to verify a user’s identity and allow different users different levels of access to a remote object. • For example: • General public may be allowed to query a database but not update it. • Users from inside a company might be allowed to both query and update the database.

  10. Object Serialization • When a object is passed to or returned from a Java method, what is really transferred is a reference to the object • Reference or object: • A special remote reference to the object is passed • A copy of the object can be passed • Reference: the local machine passes a remote object to the remote machine • Copied object: the local machine passes its own object (copy the object and send the copied object.)

  11. Object Serialization • To copy an object, you need a way to convert the object into a stream of bytes. • May not be easy: Objects may include other objects • Object Serialization is a scheme by which objects can be converted into a byte stream and then passed around to other machines, which rebuild the original objects from to bytes • These bytes can also be written to disk and read back from disk at a later time.

  12. Object Serialization • For security reasons, Java places some limitations on which objects can be serialized • All Java primitive types can be serialized • Object Java objects that implement java.io.Serializable interface • Serializable objects: • String and Component • Vector • Integer, Float (inherits from Number which is serializable) • Exceptions • Most AWT and Swing

  13. RMI • The fundamental difference between remote objects and local objects is that remote objects resides in different VM • Method call: pass references (local machine is ok) • Pass arguments (RMI way) • Primitive types are passed by values • Reference to remote objects are passed as a remote reference • Local objects are passed by values (object serialization)

  14. RMI • RMI Layer model (see Fig 18.1 on page 599)

  15. RMI • RMI Layer model (see Fig 18.1 on page 599) • The client appears to talk directly to the server • In reality, the client program talks only to the a stub. • The stub passes that conversation along to the remote reference layer • The remote reference layer talks to the transport layer • The transport layer on the client passes the data across the Internet to the transport layer on the server • The transport layer on the server then communicates with the server’s remote reference layer • The remote reference layer talks to the skeleton • The skeleton communicates with the server.

  16. RMI • RMI applications are often comprised of two separate programs: a server and a client. • RMI provides the mechanism by which the server and the client communicate and pass information back and forth. • Such an application is sometimes referred to as a distributed object application.

  17. Java RMI Architecture • The RMI architecture is based on one important principle: the definition of behavior and the implementation of that behavior are separate concepts. • RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs.

  18. Java RMI Architecture • Specifically, in RMI, the definition of a remote service is coded using a Java interface. • The implementation of the remote service is coded in a class. • Therefore, the key to understanding RMI is to remember that interfaces define behavior and classes define implementation.

  19. Java RMI Architecture Client Program Server Program Interface implementation

  20. Java RMI Architecture Server rmiregistry Client 1. bind/rebind 2. lookup Stub class Stub class 3. Remote Procedure Call

  21. Naming (java.rmi.Naming) • staticvoid bind(Stringname, Remoteobj) • Binds the specified name to a remote object. • staticString[] list(Stringname) • Returns an array of the names bound in the registry. • staticRemotelookup(Stringname) • Returns a reference, a stub, for the remote object associated with the specified name.

  22. Naming (java.rmi.Naming) • staticvoid rebind(Stringname, Remoteobj) • Rebinds the specified name to a new remote object. • staticvoid unbind(Stringname) • Destroys the binding for the specified name that is associated with a remote object.

  23. Creating RMI (Distributed) Applications • Design and implement the components of your distributed application. • Compile sources and generate stubs. • Make classes network accessible. • Start the application.

  24. Creating RMI (Distributed) Applications • Design and implement the components of your distributed application. • Job: decide on your application architecture and determine local objects and remote objects. • Defining the remote interfaces: • A remote interface specifies the methods that can be invoked remotely by a client. • Implementing the remote objects: • Remote objects must implement one or more remote interfaces.

  25. Creating RMI (Distributed) Applications • Design and implement the components of your distributed application. • Implementing the clients: • Clients that use remote objects can be implemented at any time after the remote interfaces are defined. • Clients can be invoked after the remote objects have been deployed.

  26. Creating RMI (Distributed) Applications 2. Compile sources and generate stubs. • use the javac to compile the source files, which contain the implementation of the remote interfaces and implementations, the server classes, and the client classes. • use the rmic to create stubs for the remote objects. • RMI uses a remote object's stub class as a proxy in clients so that clients can communicate with a particular remote object.

  27. Creating RMI (Distributed) Applications 3. Make classes network accessible. • Make the class files associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients accessible via a Web server.

  28. Creating RMI (Distributed) Applications 4. Start the application. • Starting the application includes running the RMI remote object registry, the server, and the client • 1. Start rmiregistry • 2. Start server (bind services) • 3. Start client (use services)

  29. RMI componets • A working RMI system is composed of several parts: • Interface definitions for the remote services • Implementations of the remote services • Stub and Skeleton files • A server to host the remote services • An RMI Naming service that allows clients to find the remote services • A class file provider (an HTTP or FTP server) • A client program that needs the remote services

  30. RMI Example • RMI Calculator client/server application • Interface (define remote services): Calculator.java • Implementation of remote services: CalculatorImpl.java • Stub and Skeleton: CalculatorImpl_Stub.java and CalculatorImpl_Skel.java • Server program: CalculatorServer.java • Client program: CalculatorClient.java

  31. RMI Calculator: Interface • The Calculator interface defines all of the remote features offered by the service: public interface Calculator extends java.rmi.Remote { public long add(long a, long b) throws RemoteException; public long sub(long a, long b) throws RemoteException; public long mul(long a, long b) throws RemoteException; public long div(long a, long b) throws RemoteException; } • >javac Calculator.java

  32. RMI Calculator: Implementation public class CalculatorImpl extends UnicastRemoteObject implements Calculator { public CalculatorImpl() throws java.rmi.RemoteException { super(); } public long add(long a, long b) throws RemoteException { return a + b; } public long sub(long a, long b) throws RemoteException { return a - b; } public long mul(long a, long b) throws RemoteException { return a * b; } public long div(long a, long b) throws RemoteException { return a / b; } }

  33. RMI Calculator: Implementation • The implementation class uses UnicastRemoteObject to link into the RMI system. • This is not a requirement. A class that does not extend UnicastRemoteObject may use its exportObject() method to be linked into RMI. • When a class extends UnicastRemoteObject, it must provide a constructor that declares that it may throw a RemoteException object. • When this constructor calls super(), it activates code in UnicastRemoteObject that performs the RMI linking and remote object initialization.

  34. RMI Calculator: Stub and Skeleton • use the RMI compiler, rmic, to generate the stub and skeleton files. • The compiler runs on the remote service implementation class file. • >rmic CalculatorImpl • After you run rmic you should find the file • Calculator_Stub.class (both 1.1 and 1.2) • Calculator_Skel.class (only 1.1).

  35. Command rmic Usage: rmic <options> <class names> <options> includes: -keep Do not delete intermediate generated source files -keepgenerated (same as "-keep") -g Generate debugging info -depend Recompile out-of-date files recursively -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -classpath <path> Specify where to find input source and class files -d <directory> Specify where to place generated class files -J<runtime flag> Pass argument to the java interpreter

  36. Command rmic Usage: rmic <options> <class names> <options> includes: The Java 2 platform version of rmic add three new options: -v1.1 Create stubs/skeletons for JDK 1.1 stub protocol version -vcompat (default) Create stubs/skeletons compatible with both JDK 1.1 and Java 2 stub protocol versions -v1.2 Create stubs for Java 2 stub protocol version only

  37. RMI Calculator: Server program • Remote RMI services must be hosted in a server process. • The class CalculatorServer is a very simple server that provides the bare essentials for hosting

  38. RMI Calculator: Server program import java.rmi.Naming; public class CalculatorServer { public CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind(" rmi://localhost:1099/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); } } public static void main(String args[]) { new CalculatorServer(); } }

  39. RMI Calculator: Client program public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator) Naming.lookup( "rmi://remotehost/CalculatorService"); System.out.println( c.sub(4, 3) ); System.out.println( c.add(4, 5) ); System.out.println( c.mul(3, 6) ); System.out.println( c.div(9, 3) ); } catch (MalformedURLException murle) { …. }

  40. Running RMI Applications • Start with the Registry. • $rmiregistry & (unix) or >start rmiregistry (windows) • Start the server hosting the CalculatorService • >java CalculatorServer • Start the client program. • >java CalculatorClient

More Related