310 likes | 501 Views
Java RMI. G53ACC Chris Greenhalgh. Contents. Java RMI overview A Java RMI example Overview Walk-through Implementation notes Argument passing File requirements RPC issues and RMI Other problems with RMI See also RMI tutorial, Farley p.71-83. Java RMI. Distributed Object System
E N D
Java RMI G53ACC Chris Greenhalgh
Contents • Java RMI overview • A Java RMI example • Overview • Walk-through • Implementation notes • Argument passing • File requirements • RPC issues and RMI • Other problems with RMI See also RMI tutorial, Farley p.71-83
Java RMI • Distributed Object System • Allows objects in one process to invoke methods on objects in another process. • Supports bind/find/execute service-oriented interaction • RMI registry is naming service • Java-specific • Optional CORBA interoperability
Java RMI system elements • RMI infrastructure = base classes and interfaces: • provides communication and naming. • RMI compiler: • generates client stubs, to translate client requests to standard form. • generates (optional) server stub (“skeleton”) to translate standard form to server implementation form.
Building an example distributed application • A “ticket” issuing service • E.g. next customer to be served, or first-come-first served • Managed resource = next ticket number • Centralised to control allocation, e.g. ensure no duplicates • Client: • Locates server • Requests the next ticket • Prints it on the screen
Example application processes/machines Server machine Client machine Process/Application A RMI registry Clientobject Process/Application B Find TicketServerobject Bind Client stub RMI objectsand classes Use RMI objectsand classes RemoteMethodInvocations (over TCP/IP connections)
1. Specify the common interface • Define the interface as a Java interface • Like a normal local interface • Set of methods with name, arguments, return type and exceptions • But… • must extend java.rmi.Remote interface • Hint to marshalling code • all methods throw java.rmi.RemoteException • NOTE: unavoidable complications - network-related errors (e.g. server unavailable or unreachable, timed out) • All types must be primitive (int etc.), implement java.io.Serializable or implement java.rmi.Remote
Example interface import java.rmi.*;public interface TicketServer extends Remote { public int getNextTicket (String name) throws RemoteException;}
Notes on defining the interface • What distinct requests can a client make? • Each request = 1 method • How can each request by concisely described? • = method name • What information does the client have that the server needs to perform the request? • = arguments – types and names • What might be anticipated to go wrong? • = exceptions • If it works, what information does the client need back? • = return type
2. Implement the service • Implement the service as a Java class • extend java.rmi.server.UnicastRemoteObject • Inherits server support, networking, etc. • Implement the define interface and its method • The “business logic” • (Typically) Implement a main method to: • Create a new instance of the server • Register (bind) this with the naming service (rmiregistry) so that clients can find it
Example server implementation • import java.rmi.*;import java.rmi.server.UnicastRemoteObject;public class TicketServerImpl extends UnicastRemoteObject implements TicketServer { // cons throws RemoteException TicketServerImpl() throws RemoteException { }; // cont…
// service state/resource int nextTicket=0; // business logic - implementation public int getNextTicket(String name) throws RemoteException { return nextTicket++; } // cont…
// app – start server public static void main(String [] args) { // install RMI security manager System.setSecurityManager (new RMISecurityManager()); try { String name = args[0]; //create new instance TicketServerImpl server = new TicketServerImpl(); // register with nameserver Naming.rebind(name, server); } catch(Exception e) { // errors... } } • }//end
3. Implement client • Client • installs appropriate security manager (and security policy) for client stubs • looks up server in name service • like URL: “rmi://machine:port/name” • defaults are “rmi://localhost:1099/” • “localhost” = “this machine” • obtains handle (client stub) which implements interface • performs remote operations • Handles errors (exceptions)
import java.rmi.*;public class TicketClient { public static void main(String [] args) { // install RMI security manager System.setSecurityManager (new RMISecurityManager()); try { // look up in nameserver String fullname = args[0]; TicketServer server = (TicketServer)Naming.lookup(fullname); // cont…
// get ticket - remote method! int ticket = server. getNextTicket("TicketClient"); System.out.println("Got ticket "+ ticket); } catch (Exception e) { /* error… */ } }}
4. Compile & Generate stubs • Compile server implementation (javac) and process with RMI compiler (rmic) • generates client stub class • (TicketServerImpl_Stub.class) • Required by client (only) • generates server stub (skeleton) class • Optionally required by server (only) (if not using reflection-based despatcher - see RPC notes) • See RMI Tutorial for details, e.g. command line options
5. Run the server • Run naming service (rmiregistry):rmiregistry [<port>] • Must be on the same machine as the server • Run server application, which • creates a server object instance and • registers with naming service • See Tutorial for command line options • E.g. security policy file
6. Run the client • Run client which • Looks up the server in the specified registry • Invokes operations on the server… • See Tutorial for command line options
Argument Passing in Java RMI (i) • All arguments must be one of: • basic types • Serialised, passed by value • E.g. int, long, short, byte, char, boolean, float, double • implement java.io.Serializable • serialised, passed by value • i.e. an identical copy of the object is made at the other end • Exact class must be found remotely • operations will be local to each copy
Serializable example • E.g. java.lang.String, or your own custom classes • E.g. public class MyPersonClass implements java.io.Serializable { public String firstName; public String lastName; public int ageYears; public MyPersonClass children[]; // sample (local) method public int getAge() { return ageYears; }}
Argument Passing in Java RMI (ii) • or: • implement java.rmi.Remote • be passed as a network reference • i.e. a network handle (copy of a client proxy object) is serialised and sent, not the object data itself • client stub class must be located by receiving process • operations on the received proxy object will themselves be remote • i.e. forwarded to the original object • e.g. “callbacks”
Unmarshalling serialized objects • For Serializable objects and stubs of Remote objects the receiving process JVM needs to find the same class (class file) • Could already be present at receiving end, in class path • Can be downloaded from a remote web server via HTTP • server is run with java.rmi.server.codebase property pointing to webserver URL where class files are put • Value is included in marshalled information • Used by receiving process IF ALLOWED • This is why RMI security manager is installed
Which files, where? • Client needs: • Client implementation • (must be local) • Interface • And any Serializable classes it uses • (must be local if directly used in the client implementation code) • Client stub • (can be downloaded since not directly referenced in client implementation class)
Server needs: • Server implementation • (must be local) • Interface • And any Serializable classes it uses • (must be local since server implements the interface and methods) • Server skeleton • (if used, must be local) • Client stub (to send to registry)
Rmiregistry needs: • Interface • And any Serializable classes it uses • Client stub • (can all be downloaded since the registry code does not explicitly refer to any of these classes)
RPC issues in RMI (ii) • Transparency • Pretty good – normal Java interface • But implements Remote and methods throw RemoteExceptions • Also no (easy) way to identify client or link client requests in “sessions”…
Client/session tracking in RMI interfaces • Client is not identified to server code • No automatic link between successive calls by the same client (cf. “sessions”) • Not like using TCP or UDP directly • Implications for security, e.g. when authentication must be done (each method) • Options: • Explicit client identification per message • From client, e.g. user id, or • Returned by initial server operation (cf. “login”/”start”) session id • Client-specific server returned by initial server operation (second interface, server object & stub)
RPC issues in RMI (ii) • Heterogeneity • Java is cross platform :-) • Java is not cross language… • However additional tool and runtime support allows • RMI interfaces to be translated to CORBA IDL interfaces and • RMI to use CORBA IIOP protocol • Allowing interoperability with any CORBA client/server
RPC issues in RMI (iii) • Concurrency • Client requests are always blocking • Needed to link RemoteException to call context • Server threads are provided by server runtime • Pool of despatcher threads • Server can handle multiple remote requests concurrently • Binding • Distributed object model, supported by rmiregistry and Remote arguments • See naming notes
Problems with RMI • Problems with class versioning (e.g. in the rmiregistry) • May not unload old versions of classes if interfaces are changed • No asynchronous invocations • No reply-less invocations (c.f. CORBA ‘oneway’) • => Blocking invocations • Failure of server can take 30s to several minutes to detect! • May need extra worker threads to ‘push’ invocations