1 / 16

RMI Remote Method Invocation

RMI Remote Method Invocation. Introduction. Sun developed a simple mechanism called Remote Method Invocation just for communicating between Java applications. Remote Method Invocation (RMI) technology provides a framework for building distributed Java systems.

Télécharger la présentation

RMI 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. RMI Remote Method Invocation Y. Daniel Liang, Introduction to Java Programming

  2. Introduction Sun developed a simple mechanism called Remote Method Invocation just for communicating between Java applications. Remote Method Invocation (RMI) technology provides a framework for building distributed Java systems. Using RMI , a Java object on one system can invoke a method in an object on another system on the network. A distributed Java system can be defined as a collection of cooperative distributed objects on the network. The method appears local to the programmer. Java.rmi package provides a standard way for a server to permit method invocations on its object. An RMI server creates local objects and makes them available to RMI clients as remote objects. Y. Daniel Liang, Introduction to Java Programming

  3. Benefits of RMI Remote Method Invocation (RMI) applications are scalable and easy to maintain RMI provides a mechanism that frees you from writing tedious code for handling parameter passing and calling remote methods. JDK 1.5 simplified RMI development and deployment. You can change the RMI server or move it to another machine without changing the client program with one exception. You must reset the URL to find the server. To overcome this exception, you can pass the URL as a command-line parameter to the client program. RMI clients can directly invoke the server method, while socket-level programming Is limited to passing values. Socket-level programming is very primitive. Avoid using it to develop client/server applications Socket-level programming is similar to programming in assembly language while RMI is similar to programming in a high-level language. . Y. Daniel Liang, Introduction to Java Programming

  4. Benefits continued • Another important benefit of RMI is that it supports callbacks, which enable the server to invoke methods on the client With this feature, you can develop interactive distributed applications. Y. Daniel Liang, Introduction to Java Programming

  5. Fundamental RMI classes Object RemoteObject Remote <<interface>> RemoteStub RemoteServer (an abstract class) Unicast RemoteObject resides on a server Y. Daniel Liang, Introduction to Java Programming

  6. The Remote Interface • To create a remote interface, define an interface that extends interface java.rmi.Remote • Remote is a tagging interface and it does not declare any methods Y. Daniel Liang, Introduction to Java Programming

  7. The Remote Interface • Every method in a Remote interface must specify that it throws a RemoteException • All method arguments and return values must be either primitive types (char, int, double, or boolean). They • are passed-by-value like a local call • Local object types, like java.lang.String are also passed-by-value • Other objects types must implement the Serializable interface. Note can not pass the object reference because the address on one machine is meaningless to a different JVM. Y. Daniel Liang, Introduction to Java Programming

  8. RMI Basics • RMI is the Java Distributed Object Model that provides an efficient way for objects to communicate with distributed objects. • RMI is a higher-level API built on top of sockets. • Socket-level programming allows you to pass data through sockets among computers. • RMI enables you to not only pass data among objects on different systems, but it also allows you to invoke methods in a remote object. • Remote objects can be manipulated as if they were residing on the local host. The transmission of data among different machines is handled transparently by the JVM. Y. Daniel Liang, Introduction to Java Programming

  9. RMI Basics continued • RMI is an evolution of the client/server technology • A clients makes a request for a services. For example, The clients retrieves the Student. scores from an RMI server. • A server honors the request and return the service to the client. The findScore method retrieves a student score. • RMI maintains the concept of client and server. But its capability is more flexible and powerful than the client server paradigm An RMI component can be both a client and a server based on the required scenario. An RMI system can pass functionality from a server to a client, and vice versa. A client/server system usually passes only data between the two. Y. Daniel Liang, Introduction to Java Programming

  10. RMI works as follows: • A server object is registered with the RMI registry • A client looks through the RMI registry for the remote object. • Once the remote object is located, its stub is returned to the client. The stub method resides on the client machine not on the server machine. • The stub packages(converts) the parameters used in the remote method into a group of bytes suitable to transport from one machine to another. This conversion is called marshalling. • The remote object can be used in the same way as a local object. Communications between the client and the server is handled through the stub and the skeleton. Y. Daniel Liang, Introduction to Java Programming

  11. The Ways RMI Works RMIC Compiler creates Client Host Server Object Interface Client Server Program Stub Server Host Server Object Interface Server Server Skeleton Object (4) Data Communication (3) Return Server Stub • Register Server • Object by name RMI Registry Host RMI Registry (2) Look for Server Object by name Java RMI uses a registry to provide naming services for remote objects, and Uses stub and the skeleton to facilitate communications between client and server. Y. Daniel Liang, Introduction to Java Programming

  12. See Programs, UML Diagrams and instructions in the RMI folder:: SmartAlex.java and .class ResponseClient.java and .class Rudness.java and .class SmartAlex_stub.class See SmartAlexResponseClient Rudness DiagramNew.doc See SmartAlex Dos Run.doc for instructions on how to run the programs. Y. Daniel Liang, Introduction to Java Programming

  13. Application to Retrieve Student Scores from an RMI Server The Steps to develop the RMI Application Step 1. Define Object Interface interface StudentServerInterface Step2. Define Server Implementation Class ServerInterfaceImp.java Step3. Create and Register Server Object RegisterWithRMIServer.java Step4. Develop a Client class StudentServerInterface.java Y. Daniel Liang, Introduction to Java Programming

  14. Instructions to Run the RMI Application • Place all the programs on the Desktop. Compile all the programs . Run all the programs from a different Command window on the desktop • Start the RMI Registry by typing “ rmiregistry” at a DOS prompt from the • Desktop folder • OR • By default, the port number 1099 is used by rmiregistry. To use a different port • number, simply type the command “ rmiregistry portnumber” at a DOS • prompt • Run the RMIC compiler: on the server code in a different DOS window • Desktop> RMIC StudentServerInterfaceImpl • Start the server RegisterWithRMIServer using the following command at • the Desktop folder: • Desktop> java RegisterWithRMIServer Y. Daniel Liang, Introduction to Java Programming

  15. Instructions to Run the RMI Application contd. 3. Run the client StudentServerInterfaceClient as an application on the Desktop: Desktop> java StudentServerInterfaceClient 4. Run the client StudentServerInterface.html from the applevtiewer. Y. Daniel Liang, Introduction to Java Programming

  16. Summary • RMI allows us to distribute our program across a network • We can bind an object in the rmiregistry server so that an object in another application can look it up and invoke its methods • The rmic compiler creates the skeleton and stub needed to call remote methods • Remote objects use interfaces to declare their remote methods Y. Daniel Liang, Introduction to Java Programming

More Related