360 likes | 367 Views
Explore the RMI framework for Java distributed software engineering, simplifying method calls and exception handling between remote objects. Learn about RMI architecture, server-client relationships, and remote object behaviors. See how to implement RMI in Java objects, abstract methods across JVMs, and create remote stubs for transparent communication.
E N D
RMI Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
RMI • A framework within which Java objects in distinct JVM can interact. • Uses • Java objects and classes • Object serialization • Network support over TCP/IP Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Simple RMI architecture Host client JVM Remote interface Naming Service (RMI Registry) server GUI server Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
RMI abstraction • Reduces the complexities of distributed computing: • Locating server • Creating client sockets • Establishment of connection • Data transfer • Synchronization • Error propagation • To a simple method call and exception handler, as if in the same JVM. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
RMI abstraction try{ result = remoteInterface.method(args); } catch(RemoteException ex){ // … } • RMI: remote method invocation is the invocation of a method in a remote object. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Server-client relationship in RMI • In Java the design, implementation and use of objects is uniform and symmetrical for servers and clients: • A Java object A can be a server to another object B while A is being a client of another object C. • Syntax/semantics of objects do not make a distinction between server or client classes. • In RMI there is always one service provider, the server, and one service receiver the client. • Both are Java objects, BUT • Server must be designed as such to be prepared for remote access before being used by a client. • The relationship is asymmetrical. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Java object design and distribution • Distributed objects do not behave in the same way as local objects. • RMI objects do not inherit from Object but from RemoteObject. • This causes some methods from Object to be overloaded to adapt them to distributed systems. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Remote object • Way of abstracting client/server approach. • It’s an object whose remote methods can be invoked via a remote interface from another JVM. • A remote object has • State • Methods, including clone, equals, hashCode, toString. • Behavior of methods designed for a remote object. • Remote method is defined in a remote interface; it’s invoked via that interface. • A remote interface: • Interface that extends java.rmi.Remote • Methods must throw RemoteException Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Remote object • To be accessible via RMI: • Object must implement a remote interface • Be exported to the RMI system. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Remote Stub • Given a remote interface located in the server side, the client has a corresponding “remote stub” • Client uses the remote stub as an instance of the remote interface implemented by the remote object. • A remote stub is a proxy for the remote interface. • Stub is a class that automatically translates remote method calls into network communication setup and parameter passing. • The skeleton is a corresponding class residing on the remote JVM accepting network communication and translates them into actual method calls on the actual objects. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
class EchoServer{ public Object echo(Object obj){ return obj; } } class EchoClient { public static void main( String[] args) throws Exception { EchoServer echo = new EchoServer(); System.output.println(echo.echo(“Hellllloooo”); } } Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Abstracting public interface Echo { Object echo (Object obj); } class EchoServer implements Echo{ public Object echo(Object obj){ return obj; } } class EchoFactory { public static Echo getEcho() { return EchoServer(); } } Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
class EchoClient { public static void main(String args) throws Exception { Echo echo = EchoFactory.getEcho(); System.out.println(echo.echo(“Helllllooooo”); } } An RMI version of the Echo service is only a slight modification of this example. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
To RMI • Changes to be made to example: • Import RMI packages • Echo interface extends java.rmi.Remote, and methods throw RemoteException • EchoServer class extends java.rmi.server.UnicastRemoteObject • EchoServer has a main. • EchoFactory uses RMI registry to instantiate an EchoServer object. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Abstracting public interface RemoteEcho extends Remote{ Object echo (Object obj) throws RemoteException; } class RemoteEchoServer extends UnicastRemote Object implements RemoteEcho{ public RemoteEchoServer() throws RemoteException { } public Object echo(Object obj) throws RemoteException { return obj; } public static void main(String args) throws Exception { RemoteEchoServer server = new RemoteEchoServer(); Naming.rebind(RemoteEcho.class.getName().server); } } Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
class RemoteEchoFactory { public static RemoteEcho getEcho() throws Exception{ return (RemoteEcho)Naming.lookup ( RemoteEcho.class.getName()); } } class RemoteEchoClient { public static void main(String args) throws Exception { Echo echo = EchoFactory.getEcho(); System.out.println(echo.echo(“Helllllooooo”); } } Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Remote method invocation semantics • A method can only be invoked as remote method via a remote interface which declares it. • Remote method declared to throw remote exception • Clients of remote method much catch and deal with remote exception • Aregumens of object type to a remote method are passed by deep copy. • Any result of object type of a a remote method is returned by deep copy. • Any exception thrown by a remote method is returned by deep copy. • An exported remote object is passed or returned by remote reference, not by deep copy. • Semantics of Object are specialized for remote objects and remote references to them. • RMI system assures that when a remote invocation returns (normally or via an exception), the remote method has been invoked at most once. • Remote objects are subject to distributed garbage collection. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Remote method invocation semantics • A remote method can modify parameters or other objects, but any such modifications are not visible to client. • A remote object can only communicate with its caller via return values or exceptions. • This has important consequences for the design of remote methods. • RMI • Passes primitive types by value • Passes objects types by deep copy • Passes exported remote objects by remote references or remote stubs. • A normal return of an invocation of a remote method is guarantee to have executed exactly once. • A remote invocation that throws a remote exception may or may not have executed at all. • Execution of a remote method is asynchronous. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Remote interface • It is a marker interface. • A user defined remote interface • extends Remote • Every method must throw RemoteException • When a remote object can be marshalled as a parameter or result of a remote method, must be declared as its remote interface. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Proxy • A proxy is an object at the client, which acts as the implementation of a remote interface • It communicates with the real object over the network. • RMI proxies are also named stubs. • The class definition of a remote stub is automatically generated from the corresponding remote server class by the rmic compiler. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Naming – RMI registry • Registry is a remote object that maps names to remote objects. • Given a name by client, registry returns stub of the remote server. • Remote object must register with naming service to allow clients to locate where is running. • Client connects to a naming registry and asks for a reference to a service registered under a name. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Bind, unbind, lookup • Bind add an entry – service-name/address pair to registry • Unbind removes a service’s entry from registry by name • Lookup finds service address by name. • Rebind, like bind but overrides. • List • The reference to a service includes: • Host where remote object is running. • Port on which is listening. • Object’s internal RMI name. • To start the naming registry: rmicregistry • Registry is a simple RMI client, must be run with the location of the stub files in its CLASSPATH. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Naming Registry Remote Object naming client rmi://server/name lookup Internet name name Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Unicast Servers • An RMI server is a remote object which • Implements a remote interface • Is exported to the RMI system. • RMI provides several base class which can be used to define server classes: • RemoteObject • Provides basic remote object semantics for servers and stubs. • RemoteServer • Provides getClientHost, getLog methods used by servers. • UnicastRemoteObject • Supports simple transient point-to-point RMI servers. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Unicast remote objects • A remote server object such that: • References-remote stubs- to such objects are only valid for at most the life of the process that creates the remote object. • Communication with remote objects uses TCP • Invocations, parameters and results use a stream protocol for communicating between client and server. • Unicast remote object is a TCP/IP, point-to-point server which listens at a TCP/IP port. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Unicast remote objects • Consider the following interface: Import java.rmi.*; Import java.rmi.server.*; Interface MyRemoteInterface extends Remote{ void remoteMethod() throws RemoteException; } Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Server using extend UnicastRemoteObject class ExtendedUnicastServer extends UnicastRemoteObject implements MyRemoteInterface { public ExtendedUnicastServer () throws RemoteException { //auto-export happens here super(); } public void remoteMethod() throws RemoteException { } } • When a UnicastRemoteObject is constructed: • It is automatically exported • Registered with the RMI system • Made to listen to a TCP port. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
UnicastRemoteObject • The various constructors allow to choose: • Exporting on a default port chosen at runtime • Exporting on a specific port • Exporting on a specific port with specified client and server socket factories. • All constructors throw RemoteException • A server extending UnicastRemoteObject • Inherits remote object semantics from RemoteObject • Cloning and serialization from UnicastRemoteObject • UnicastRemoteObject implements Object.clone by cloning the entire state of the remote object and exports it as another listerner on same port. • But class does not implement Cloneable Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
RemoteServer, or RemoteObject class ExtendedUnicastServer extends RemoteServer implements MyRemoteInterface { public void remoteMethod() throws RemoteException { } } class ExtendedUnicastServer extends RemoteObject implements MyRemoteInterface { public void remoteMethod() throws RemoteException { } } Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
class ExtendedUnicastServer extends RemoteServer implements MyRemoteInterface { public void remoteMethod() throws RemoteException { } } class ExtendedUnicastServer extends RemoteObject implements MyRemoteInterface { public void remoteMethod() throws RemoteException { } } • Server inherits remote object semantics from RemoteObject • Behavior under cloning and serialization is up to you. • It inherits various public static methods • No much difference between choices. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Server implementations and threads • A method dispatched by Rmi runtime to a remote object implementation may or may not execute in a separate thread. RMI runtime makes no guarantees w.r.t. mapping object invocations to threads.Since remote method invocations on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread safe. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Building the server • Each RMI server must be • Compiled by Java compiler • Processed by rmic that generates the remote stub used by clients to access remote server. • It also generates the skeleton for the server. • From JDK1.3 class files are produced in the same directory as source. • Stub files are required for both clients and servers. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Simple example: RMI date server • Define remote interface. • Implement remote interface. This should register itself in a naming service. • Generate stub and skeleton via rmic • Write client: • it locates server • Makes remote calls. • Start naming registry using rmiregistry. • Start server • Run client. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
RMI clients • Simple adjustements to this environment: • Must intercept remote exceptions • Must establish an initial connection to a remote object. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
Simple example: RMI date server • Start the registry on the server machine, from a directory containing the stub classes for objects that will be registered. • Start server on same machine: • And instance of server is created and registered with the local registry. • Run client on any machine: • Specify the name of host running the registry. Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads
RMI class file locations • Files of an RMI-based application • Client classes • Server classes • Remote interfaces • Stub classes • Skeleton classes • RMI client distribution classes: • Client classes • Remote interface classes • Stub classes • RMI server distribution classes: • Server implementation classes • Remote interfaces • Stub classes • Skeleton classes Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads