Distributed Programming in Java Distribution (2)
Remote Method Invocation Remote Method Invocation (RMI) Primary design goal for RMI is transparency Should be able to invoke remote objects with same syntax and semantics as local ones However, some important differences exit Refresher: in the introduction to this course we presented the key differences...
Remote is not Like Local Latency data transfer time is several orders of magnitude slower Memory access refs in local address space are not valid in a remote address space Concurrency may be unexpected and unplanned interaction of computing tasks Partial failure can be indeterminacy due to the lack of a central authority Famous paper by Waldo, J., et al, A Note on Distributed Computing, http:// research.sun.com/techrep/1994/smli_tr-94-29.pdf
Detailed Comparison (1) Object behavior Behavior exported by a remote object is defined in an interface Object implementation Class that implements this interface Object creation Clients cannot directly create instances of a remote class (unless using activation)
Detailed Comparison (2) Object access Remote objects accessed through remote reference variable (a client proxy) Active references Remote objects are considered to have an active reference if they have been accessed recently (during a certain lease period) Exceptions Remote exceptions exposed to client
Separation of Behavior and Implementation Remote object behavior defined in an interface (Service) Remote object behavior implemented in a server class (ServiceImpl) Proxy sends request to remote object Service service() Client Proxy service() ServiceImpl service() Also see Broker pattern
Client Server RMI Stubs Sekeletons Architecture Stub Sekeleton Layers Remote Reference RemoteRef Unicast RemoteObject Naming Transport «protocol» JRMP «protocol» RMI-IIOP Also see Layers pattern
Stubs and Skeletons Stub Represents server object on client side Implements remote interface Knows how to forward method calls Skeleton Represents client object on server side Calls services on the service object Knows how to forward results to client Skeletons are no longer required, as the JDK invokes the server methods via reflection
Using RMI 1. Define interfaces for remote objects 2. Define implementations for interfaces 3. Create stub (and skeleton) classes 4. Develop a server that creates an instance of the service implementation 5. Develop a client that uses the service provided by the remote interface 6. Start the RMI registry, server and client
1. Define Remote Interface Define an interface of the remote object Must extend the Remote interface Contract between a server and its clients Methods may throw a RemoteException public interface Service extends Remote { public void service(...) throws RemoteException; // other services }
2. Implement Interface Implementation of the remote interface Extends UnicastRemoteObject class (or exports a remote reference explicitly) Constructor of base class exports object public class ServiceImpl extends UnicastRemoteObject } implements Service { public void service(...) throws RemoteException; { } // implementation // implementation of other services
3. Create Stubs Generate stub (and skeleton) classes using the RMI compiler (rmic) > rmic -keep -v1.2 ServiceImpl Generates a stub classe ServiceImpl_Stub (implements interface) -keep option keeps stub source
4. Develop Server Create an instance of the service (that is, the implementation of the remote interface) Registers the service in naming service Service service = new ServiceImpl(...); Naming.rebind(name, service);
Registry RMI can use different naming services A simple service is the Registry Each RMI service is identified by an URL with the protocol rmi [rmi:][//host[:port]/]name Binding: registration of the service with the registry so remote clients can locate it
rmi.naming bind(name, obj) Binds remote object to name rebind(name, obj) Binds remote object to name, even if bound unbind(name) Removes binding of name lookup(url) Returns remote object bound to url list(url) Returns list of names in registry
5. Develop Client Develop a client that uses the service provided by the remote interface Can be a local or a remote object Client must first locate a remote object before its methods can be invoked Remote remote = Naming.lookup(name); Service service = (Service) remote; //... service.service(); // remote method invocation
6. Starting the Application rmiregistry [<port>] Default port: 1099 java -Djava.security.policy=server.policy Server Registers instance under assigned name We need to specify a security policy java -Djava.security.policy=client.policy Client
Security Policy Security policies introduced in JDK 1.2 The java.security package introduces the notion of policy A policy specifies which permissions (e.g. to read or write files, read from sockets) are available to code from different sources Policy for an application specified in policy file, typically using policytool in JDK
Example Policy Grant permissions to all sources to connect and accept on sockets for the given ports Restricts what actions downloaded classes (here, stubs) can perform Connect/accept on ports 1024-65535 Connect on port 80 (web servers) grant { permission java.net.socketpermission "*:1024-65535", "connect,accept"; permission java.net.socketpermission "*:80", "connect"; };
Print Server Implements a print server to which one can submit print jobs, list, and cancel them Users will be notified when job is complete We will start with a design without notification, that is, only printer classes need to expose a remote interface...... to add notification we need to support callbacks on the client (also interface)
Print Server Remote UnicastRemoteObject Client PrintJob document copies Printer submit() jobs() cancel() PrinterImpl submit() jobs() cancel() PrinterServer Must be serializable
Printer Defines remote interface public interface Printer extends Remote { public void submit(printjob job) throws RemoteException; public Vector jobs() throws RemoteException; public void cancel(printjob job) throws RemoteException; } PrintJobs must be serializable public class PrintJob implements Serializable { public String owner; public String document; public int copies; public Date timestamp; }