RMI (Remote Method Invocation) allows invoking methods on remote objects similarly to local objects. It works by having local stub objects that send requests to remote skeleton objects. The key steps are: 1) defining the remote interface, 2) implementing the interface on the remote object, 3) exporting the remote object, 4) registering the object, 5) obtaining the stub, and 6) invoking methods on the stub similarly to a local object. The stub handles marshalling/unmarshalling of parameters and results to pass data between local and remote sides.
2. What is RMI?
• Remote Method Invocation
• A way to invoke the methods of a object
that resides in a remote site.
• Similar with RPC, but incorporating OO
concepts.
-2-
3. The Primary Goal of RMI
• The remote objects is used with just the
same syntax and semantics as the local
objects!
• How do we achieve this goal?
-3-
4. public class B {
public void method1() {
...
}
public int method2(double d) {
...
}
...
}
...
... ...
b.method1(); ...
... B b = new B();
... ...
m.k(b); ...
...
...
Local Remote
-4-
5. The tricks
• Use a local object serving as an agent to
send our requests to the remote object and
receive any results.
• Such a object is called stub.
• There used to be an object called skeleton residing in the
remote side to handle the requests, but is no more
needed after Java 1.2.
-5-
6. Extract the interface
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B implements R {
public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
... }
... ...
R r; }
...
... ...
try { ...
r.method1();
} catch (RemoteException e) { B b = new B();
... ...
} ...
...
...
m.k(r);
...
...
Local Remote
-6-
7. Export the service (1)
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B
extends UnicastRemoteObject
implements R {
public B() throws RemoteException {
super();
}
public void method1()
throws RemoteException {
...
}
... public int method2(double d)
... throws RemoteException {
R r; ...
... }
... ...
try { }
r.method1();
} catch (RemoteException e) { ...
... ...
} try {
... B b = new B();
... } catch (RemoteException e) {
m.k(r); ...
... }
... ...
...
Local Remote
-7-
8. Export the service (2)
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B implements R {
public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
}
... ...
... }
R r;
...
... ...
try { ...
r.method1(); try {
} catch (RemoteException e) { B b = new B();
... UnicastRemoteObject
} .exportObject(b);
... } catch (RemoteException e) {
... ...
m.k(r); }
... ...
... ...
Local Remote
-8-
9. Registration (1)
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B
extends UnicastRemoteObject
implements R {
public B() throws RemoteException {
super();
}
public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
... }
... ...
R r; }
...
... rmiregistry &
...
try { ...
r.method1(); try {
} catch (RemoteException e) { B b = new B();
... Naming.rebind(quot;rmi://host:port/namequot;, b);
} } catch (Exception e) {
... ...
... }
m.k(r); ...
... ...
...
Local Remote
-9-
10. Registration (2)
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B
extends UnicastRemoteObject
implements R {
public B() throws RemoteException {
super();
}
public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
... }
... ...
R r; }
...
... ...
try { ...
r.method1(); try {
} catch (RemoteException e) { B b = new B();
... Registry rmiR = LocateRegistry.createRegistry(1099);
} rmiR.rebind(quot;rmi://host:port/namequot;, b);
... } catch (Exception e) {
... ...
m.k(r); }
... ...
... ...
Local Remote
-10-
11. Get the stub (1)
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B
extends UnicastRemoteObject
implements R {
public B() throws RemoteException {
super();
}
public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
... }
... ...
R r; }
try {
r = (R) (Naming.lookup( rmiregistry &
...
quot;rmi://host:port/namequot;)); ...
r.method1(); try {
} catch (Exception e) { B b = new B();
... Naming.rebind(quot;rmi://host:port/namequot;, b);
} } catch (Exception e) {
... ...
... }
m.k(r); ...
... ...
...
Local Remote
-11-
12. Get the stub (2)
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B
extends UnicastRemoteObject
implements R {
public B() throws RemoteException {
super();
}
public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
... }
... ...
R r; }
try {
Registry rmiR = ...
LocateRegistry. ...
getRegistry(1099); try {
r = (R) (rmiR.lookup( B b = new B();
quot;rmi://host:port/name)); Registry rmiR = LocateRegistry.createRegistry(1099);
r.method1(); rmiR.rebind(quot;rmi://host:port/namequot;, b);
} catch (Exception e) { } catch (Exception e) {
... ...
} }
... ...
... ...
m.k(r);
...
... Local Remote
-12-
13. Generate the Stub
public interface R extends Remote {
public void method1() throws RemoteException;
public int method2(double d) throws RemoteException;
}
public class B
extends UnicastRemoteObject
implements R {
public B() throws RemoteException {
super();
rmic }
B_stub.class public void method1()
throws RemoteException {
...
}
public int method2(double d)
throws RemoteException {
...
... }
... ...
R r; }
try {
Registry rmiR = ...
LocateRegistry. ...
getRegistry(1099); try {
r = (R) (rmiR.lookup( B b = new B();
quot;rmi://host:port/name)); Registry rmiR = LocateRegistry.createRegistry(1099);
r.method1(); rmiR.rebind(quot;rmi://host:port/namequot;, b);
} catch (Exception e) { } catch (Exception e) {
... ...
} }
... ...
... ...
m.k(r);
...
... Local Remote
-13-
14. More about this scheme
• The stub takes care of the marshelling of parameters and
the unmarshelling of returned results, so as the opposite
side.
• How data is passed around?
– Primitive data
– Objects
• Object serialization
• Stubs is also objects and can be passed as well.
• Where is the class?
-14-
15. Issues on RMI
• Create an object remotely?
– Remote Object Activation
• Garbage collection?
– Distributed Garbage Collection
• Portings of RMI?
– various way to substitute the transport layer with others
• Optimization of RMI?
– lots of researches
– KaRMI
-15-