RMI Fundamentals

1,641 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,641
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
119
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

RMI Fundamentals

  1. 1. RMI Fundamentals Chung-Kai Chen -1-
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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-
  16. 16. Recommanded Readings • http://java.sun.com/docs/books/tutorial/rmi /index.html • http://developer.java.sun.com/developer/on lineTraining/rmi/RMI.html • http://java.sun.com/j2se/1.4.1/docs/guide/r mi/index.html -16-

×