750 likes | 1.05k Views
Block 8: Remote Method Invocation (RMI). Jin Sa. Outline. Message Passing vs. Distributed Objects Distributed Object Paradigm An Archetypal Distributed Objects System Distributed Object System Java Remote Method Invocation The Java RMI Architecture The API for the Java RMI
E N D
Block 8: Remote Method Invocation (RMI) Jin Sa Client-server programming
Outline • Message Passing vs. Distributed Objects • Distributed Object Paradigm • An Archetypal Distributed Objects System • Distributed Object System • Java Remote Method Invocation • The Java RMI Architecture • The API for the Java RMI • Remote Interface • The Remote Interface Implementation • Stub and Skeleton • The Object Server • The RMI registry • The Client-side Software • Steps for building an RMI application • Comparison of the RMI and the socket APIs • The HelloWorld example • RMI security Client-server programming
Message Passing vs. Distributed Objects Client-server programming
Message Passing • The message-passing paradigm is a natural model for distributed computing, in the sense that it mimics inter-human communications. It is an appropriate paradigm for network services where processes interact with each other through the exchanges of simple messages. • However, the abstraction provided by this paradigm does not meet the needs of the complexity of sophisticated network applications. Client-server programming
Message Passing (skip) • The message-passing paradigm is data-oriented. Each message contains data marshaled in a mutually agreed upon format, and is interpreted as a request or response according to the protocol. The receiving of each message triggers an action in the receiving process. It is inadequate for complex applications involving a large mix of requests and responses. In such an application, the task of interpreting the messages can become overwhelming. Client-server programming
Local Objects vs. Distributed Objects • Local objects are those whose methods can only be invoked by a local process, a process that runs on the same computer on which the object exists. • A remote object is one whose methods can be invoked by a remote process, a process running on a computer connected via a network to the computer on which the object exists. Client-server programming
Distributed Object Paradigm Distributed object paradigm is action-oriented: the focus is on the invocation of the operations, while the data passed takes on a secondary role. Client-server programming
The Distributed Objects Paradigm Client-server programming
The Distributed Object Paradigm (skip) • The distributed object paradigm is based on objects that exist in a distributed system. Network resources are represented by distributed objects. • To request service for a network resource, a process invokes one of its methods, passing data as parameters to the method. • The method is executed on the remote host, and the response is sent back to the requesting process as a return value. Client-server programming
The Distributed Object Paradigm Client-server programming
The Distributed Object Paradigm (skip) • A process running on host A makes a method call to a distributed object on host B, passing with the call data for the parameters, if any. • The method call invokes an action performed by the method on host B, and a return value, if any, is passed from host B to host A. • A process which makes use of a distributed object is said to be a client process of that object, and the methods of the object are called remote methods to the client process. Client-server programming
An Archetypal Distributed Objects System Client-server programming
Distributed Object System • A distributed object is provided, or exported, by a process, here called the object server. • A facility, here called an object registry, must be present in the system architecture for the distributed object to be registered. • To access a distributed object, a process –an object client – looks up the object registry for a reference to the object. This reference is used by the object client to make calls to the methods. Client-server programming
Distributed Object System • Logically, the object client makes a call directly to a remote method. • In reality, the call is handled by a software component, called a client proxy, which interacts which the software on the client host that provides the runtime support for the distributed object system. • The runtime support is responsible for the interprocess communication needed to transmit the call to the remote host, including the marshalling of the argument data that needs to be transmitted to the remote object. Client-server programming
Distributed Object System • A similar architecture is required on the server side, where the runtime support for the distributed object system handles the receiving of messages and the unmarshalling of data, and forwards the call to a software component called the server proxy. • The server proxy interfaces with the distributed object to invoke the method call locally, passing in the unmarshalled data for the arguments. Client-server programming
Distributed Object System • The method call results in the performance of some tasks on the server host. The outcome of the execution of the method, including the marshalled data for the return value, is forwarded by the server proxy to the client proxy, via the runtime support and network support on both sides. Client-server programming
RMI The distributed object paradigm has been widely adopted in distributed applications, for which a large number of mechanisms based on the paradigm are available. We will concentrate on Java Remote Method Invocation (RMI). Client-server programming
Java Remote Method Invocation Client-server programming
The Java RMI Architecture Client-server programming
Java Remote Method Invocation • Using RMI, an object server exports a remote object and registers it with a directory service. The object provides remote methods, which can be invoked in client programs. Client-server programming
Java RMI API Client-server programming
The API for the Java RMI • The Remote Interface • The Server-side Software • The Remote Interface • The Remote Interface Implementation • Stub and Skeleton Generations (not needed if using JDK 1.5) • The Object Server • The Client-side Software • The Remote Interface • Stub • The client program Client-server programming
Remote Interface Template import java.rmi.* public interface SomeInterface extends Remote { // signature of first remote method public String someMethod1( ) throws RemoteException; … … } // end interface Client-server programming
Remote interface • The java.rmi.Remote Exception must be listed in the throws clause of each method signature. • This exception may occur during the processing of a remote method call. • Causes of such exceptions include exceptions that may occur during interprocess communications, such as access failures and connection failures, as well as problems unique to remote method invocations, including errors resulting from the object, the stub, or the skeleton not being found. Client-server programming
The Server-side Software • An object server • creates some remote objects that implement each of the remote methods specified in the interface, • registers an object which contains the implementation with a directory service. Client-server programming
The Remote Interface Implementation A class which implements the remote interface should be provided. The syntax is similar to a class that implements a local interface. Client-server programming
The Remote Interface implementation template import java.rmi.*; import java.rmi.server.*; public class SomeImpl extends UnicastRemoteObject implements SomeInterface { public SomeImpl() throws RemoteException { super( ); } public String someMethod1( ) throws RemoteException { // code to be supplied } … … } // end class Client-server programming
Stub and Skeleton Generations-- skip this if using JDK1.5 • In RMI, each distributed object requires a proxy each for the object server and the object client, known as the object’s skeleton and stub respectively. • These proxies are generated from the implementation of a remote interface using the RMI compiler rmic. For example: rmic SomeImpl Two proxy files will be generated, each prefixed with the implementation class name: SomeImpl_skel.class SomeImpl_stub.class • If JDK 1.5 is used, no need to do this. Client-server programming
The stub file for the object -- skip if using JDK1.5 • The stub file for the object and the remote interface file are required for the client program to compile. • A copy of each file may be provided to the object client by hand or by “stub downloading” which allows a stub file to be obtained by a client dynamically. Client-server programming
The Object Server The object server class is a class whose code instantiates and exports an object of the remote interface implementation. Client-server programming
The Object Server template import java.rmi.*; public class SomeServer { public static void main(String args[]) { try{ … … SomeImpl exportedObj = new SomeImpl(); startRegistry(RMIPort); registryURL = "rmi://localhost:"+RMIPort+"/some"; Naming.rebind(registryURL, exportedObj); }// end try } // end main Client-server programming
The Object Server template private static void startRegistry(int RMIPortNum) throws RemoteException{ try { Registry registry= LocateRegistry.getRegistry(RMIPortNum); registry.list( ); } catch (RemoteException ex) { // No valid registry at that port; create one Registry registry= LocateRegistry.createRegistry(RMIPortNum); } } // end startRegistry Client-server programming
The Object Server • In our object server template, the code for creating and exporting an object is a follows: SomeImpl exportedObj = new SomeImpl(); registryURL = "rmi://localhost:" + RMIport + "/some"; Naming.rebind(registryURL, exportedObj); • The Naming class provides methods for storing and obtaining references from the registry. The rebind method allow an object reference to be stored in the registry with a URL in the form of rmi://<hostname>:<portnumber>/<referencename> • The reference name is a name of your choice, and should be unique in the registry. Client-server programming
The RMI Registry • The RMIRegistry is a server located at port 1099 by default. • It registers remote objects and provides naming services for looking up objects. Client-server programming
The RMI Registry • The RMIRegistry can be started dynamically in the server class: import java.rmi.registry.LocateRegistry; … LocateRegistry.createRegistry(RMIport); • Alternatively, an RMI registry can be activated by hand as follows: rmiregistry <port number> If no port number is specified, port number 1099 is assumed. Client-server programming
The Object Server • When an object server is executed, the exporting of the remote object causes the server process to begin to listen and wait for clients to connect and request the service of the object. • An RMI object server is a concurrent server: each request from an object client is serviced using a separate thread of the server. Client-server programming
The Client-side Software The program for the client class is like any other Java class. The syntax needed for RMI involves • locating the RMI Registry in the server host, • looking up the remote reference for the remote object; the reference can then be cast to the remote interface class and the remote methods invoked. Client-server programming
The Client-side Software template public class SomeClient { public static void main(String args[]) { try { … … String registryURL= "rmi://localhost:" + RMIport + "/some"; SomeInterface h = (SomeInterface)Naming.lookup(registryURL); String message = h.method1(); … … } // end try catch (Exception e) { …… } } //end main }//end class Client-server programming
Looking up the remote object The lookup method of the Naming class is used to retrieve the object reference in the registry. Note that the retrieved reference must be casted to the remote interface class. SomeInterface h = (SomeInterface)Naming.lookup(registryURL); Client-server programming
Invoking the Remote Method • The remote interface reference can be used to invoke any of the methods in the remote interface, as in the example: String message = h.method1(); System.out.println(message); • Note that the syntax for the invocation of the remote methods is the same as for local methods. Client-server programming
Steps for building an RMI application Client-server programming
Steps for developing the server-side software • Specify and compile the remote interface in SomeInterface.java. • Implement and compile the interface in SomeImpl.java • (Skip if using JDK 1.5) Use the RMI compiler rmic to process the implementation class and generate the stub file and skeleton file for the remote object: rmic SomeImpl The files generated can be found in the directory as SomeImpl_Skel.class and SomeImpl_Stub.class. (not visible if using JDK1.5.) Steps 2 and 3 must be repeated each time that a change is made to the interface implementation. • Create the object server program SomeServer.java. • Run the object server Client-server programming
Steps for developing the client-side software • Obtain a copy of the remote interface class file. • (Skip if using JDK1.5) Obtain a copy of the stub file for the implementation of the interface: SomeImpl_Stub.class. • Develop and compile the client program SomeClient.java. • Run the client. Client-server programming
Placement of files for a RMI application (skip) Client-server programming
Comparison of the RMI and the socket APIs The remote method invocation API is an efficient tool for building network applications. It can be used in lieu of the socket API in a network application. Client-server programming
Comparison of the RMI and the socket APIs • The socket API is closely related to the operating system, and hence has less execution overhead. For applications which require high performance, this may be a consideration. • The RMI API provides the abstraction which eases the task of software development. Programs developed with a higher level of abstraction are more comprehensible and hence easier to debug. Client-server programming
The HelloWorld Example Client-server programming
Source files for the Hello application • HelloInterface.java • HelloImpl.java • HelloClient.java • HelloServer.java Client-server programming