Java RMI l l RMI Remote Method Invocation

  • Slides: 16
Download presentation
Java RMI l l RMI = Remote Method Invocation. Allows Java programs to invoke

Java RMI l l RMI = Remote Method Invocation. Allows Java programs to invoke methods of remote objects. Only between Java programs. Several versions (JDK-1. 1, JDK-1. 2)

Remote Method Invocation Client Server Remote Object Implementation Remote Object Interface Skeleton RMI implements

Remote Method Invocation Client Server Remote Object Implementation Remote Object Interface Skeleton RMI implements this Stub You implement this Client

Interfaces (Transparency) l l To client, remote object looks exactly like a local object

Interfaces (Transparency) l l To client, remote object looks exactly like a local object (except that you must bind to it first). Using interfaces: l l you write interface for remote object you write implementation for remote object RMI creates stub class (implementing the remote object interface) client accesses stub exactly same way it would access a local copy of the remote object

RMI Registry l RMI needs a port mapper too: l l Called RMI registry

RMI Registry l RMI needs a port mapper too: l l Called RMI registry You must start it yourself (unlike RPC): l l servers can register contact address information clients can locate servers needs to be started on every machine that hosts server objects program called rmiregistry runs on port 1099 by default (but you can use ‘rmiregistry <port_nb>’) Programs can access the registry thanks to java. rmi. Naming class.

Example l Simple program: l l l write interface for remote object: Remote. java

Example l Simple program: l l l write interface for remote object: Remote. java implementation of object: Remote. Impl. java server to run object: Remote. Server. java client to access object: Client. java RMI compiler ‘rmic’ generates: l l client stub: Remote. Impl_Stub. class (already compiled) server skeleton: Remote. Impl_Skel. class (already compiled)

Example l Step 1: write interface Calculator. java import java. rmi. Remote; import java.

Example l Step 1: write interface Calculator. java import java. rmi. Remote; import java. rmi. Remote. Exception; public interface Calculator extends Remote { public long add(long a, long b) throws Remote. Exception; public long sub(long a, long b) throws Remote. Exception; public long mul(long a, long b) throws Remote. Exception; public long div(long a, long b) throws Remote. Exception; } l Few rules: l l l interface must extend java. rmi. Remote interface methods must throw java. rmi. Remote. Exception exception Compile: l $ javac Calculator. java

Example l Step 2: write remote object Calculator. Impl. java import java. rmi. server.

Example l Step 2: write remote object Calculator. Impl. java import java. rmi. server. Unicast. Remote. Object; import java. rmi. Remote. Exception; public class Calculator. Impl extends Unicast. Remote. Object implements Calculator { // Implementations must have an explicit constructor public Calculator. Impl() throws Remote. Exception { super(); } public long add(long a, long b) throws Remote. Exception { return a + b; } public long sub(long a, long b) throws Remote. Exception { return a - b; } public long mul(long a, long b) throws Remote. Exception { return a * b; } public long div(long a, long b) throws Remote. Exception { return a / b; } }

Example l Implementation class must respect a few constraints: l l must implement the

Example l Implementation class must respect a few constraints: l l must implement the interface (of course) must inherit from the java. rmi. server. Unicast. Remote. Object class must have explicit constructor which throws the java. rmi. Remote. Exception exception Compile: $ javac Calculator. Impl. java

Example l l Step 3: generate stub and skeleton RMI compiler: $ rmic Calculator.

Example l l Step 3: generate stub and skeleton RMI compiler: $ rmic Calculator. Impl l l Generates Calculator. Impl_Stub. class and Calculator. Impl_Skel. class files. Already compiled.

Example l Step 4: write server Calculator. Server. java import java. rmi. Naming; public

Example l Step 4: write server Calculator. Server. java import java. rmi. Naming; public class Calculator. Server { public Calculator. Server() { try { Calculator c = new Calculator. Impl(); Naming. rebind(“rmi: //localhost: 1099/Calculator. Service”, c); } catch (Exception e) { System. out. println(“Trouble: “ + e); } } public static void main(String args[]) { new Calculator. Server(); } }

Example l l Server program creates Calculator. Impl object. Registers object to local RMI

Example l l Server program creates Calculator. Impl object. Registers object to local RMI registry (‘rebind()’): l l rebind(String name, Remote obj) associates a name to an object names are in the form of a URL: rmi: //<host_name>[: port]/<service_name> l Server waits for incoming requests $ javac Calculator. Server. java

Example l Step 5: write Calculator. Client. java import java. net. Malformed. URLException; public

Example l Step 5: write Calculator. Client. java import java. net. Malformed. URLException; public class Calculator. Client { public static void main(String[] args) { try { Calculator c = (Calculator) Naming. lookup(“rmi: //wizard. cse. nd. edu/Calculator. Service”); System. out. println(c. add(4, 5)); System. out. println(c. sub(4, 3)); } catch (Exception e) { System. out. println(“Received Exception: ”); System. out. println(e); } } }

Example l Before invoking the server, the client must ‘lookup’ the registry: l l

Example l Before invoking the server, the client must ‘lookup’ the registry: l l must provide the URL for remote service gets back a stub which has exactly the same interface as the server can use it as a local object: long x = c. add(4, 5); Compile: $ javac Calculator. Client. java

Example l l Step 6: test it! Start the RMI registry: rmiregistry l l

Example l l Step 6: test it! Start the RMI registry: rmiregistry l l registry must have access to your classes either start the registry in the same directory as the classes or make sure directory is listed in $CLASSPATH variable Start server: java Calculator. Server Start client: $ java Calculator. Client 9 1 $

Using RMI in a Distributed Context l l First, test your program on a

Using RMI in a Distributed Context l l First, test your program on a single host. To use it on 2 machines: l l l server and rmiregistry need the following files: l Calculator. class (server object interface) l Calculator. Impl. class (server object implementation) l Calculator. Impl_Stub. class (stub) l Calculator. Server. class (server program) client needs: l Calculator. class (server object interface) l Calculator. Impl_Stub. class (stub) l Calculator. Client. class (client program) nobody needs the skeleton file Calculator. Impl_Skel. class l generated only for compatibility with JDK-1. 1

Method Parameters Transfer l l Base types (int, float, char) are transferred directly. Remote

Method Parameters Transfer l l Base types (int, float, char) are transferred directly. Remote objects (inheriting from java. rmi. Remote) are not transferred: l l l instead, a distributed reference to object is shipped any invocation to this object will result in a RMI request Non-remote objects are serialized and shipped: l l l the object itself plus every other object that it refers to (recursively) remote invocations pass objects by value (local by reference) very easy to transfer huge quantities of data without noticing (you have database in memory and you transfer an object which contains a reference to database)