RMI Fundamentals

16
RMI Fundamentals Chung-Kai Chen -1-

description

 

Transcript of RMI Fundamentals

Page 1: RMI Fundamentals

RMI Fundamentals

Chung-Kai Chen

-1-

Page 2: RMI Fundamentals

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-

Page 3: RMI Fundamentals

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-

Page 4: RMI Fundamentals

public class B {public void method1() {

...}public int method2(double d) {

...}...

}

...

...B b = new B();......

...

...b.method1();......m.k(b);......

Local Remote

-4-

Page 5: RMI Fundamentals

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-

Page 6: RMI Fundamentals

public class B implements R {public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

...

...B b = new B();......

...

...R r;......try {

r.method1();} catch (RemoteException e) {

...}......m.k(r);......

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

Extract the interface

-6-

Page 7: RMI Fundamentals

public class Bextends UnicastRemoteObjectimplements R {

public B() throws RemoteException {super();

}public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

...

...try {

B b = new B();} catch (RemoteException e) {

...}......

...

...R r;......try {

r.method1();} catch (RemoteException e) {

...}......m.k(r);......

Export the service (1)

-7-

Page 8: RMI Fundamentals

...

...try {

B b = new B();UnicastRemoteObject

.exportObject(b);} catch (RemoteException e) {

...}......

Local Remote

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) {

...}......m.k(r);......

Export the service (2)

-8-

Page 9: RMI Fundamentals

public class Bextends UnicastRemoteObjectimplements R {

public B() throws RemoteException {super();

}public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

...

...try {

B b = new B();Naming.rebind("rmi://host:port/name", b);

} catch (Exception e) {...

}......

...

...R r;......try {

r.method1();} catch (RemoteException e) {

...}......m.k(r);......

rmiregistry &

Registration (1)

-9-

Page 10: RMI Fundamentals

public class Bextends UnicastRemoteObjectimplements R {

public B() throws RemoteException {super();

}public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

...

...try {

B b = new B();Registry rmiR = LocateRegistry.createRegistry(1099);rmiR.rebind("rmi://host:port/name", b);

} catch (Exception e) {...

}......

...

...R r;......try {

r.method1();} catch (RemoteException e) {

...}......m.k(r);......

Registration (2)

-10-

Page 11: RMI Fundamentals

public class Bextends UnicastRemoteObjectimplements R {

public B() throws RemoteException {super();

}public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

...

...R r;try {

r = (R) (Naming.lookup("rmi://host:port/name"));

r.method1();} catch (Exception e) {

...}......m.k(r);......

...

...try {

B b = new B();Naming.rebind("rmi://host:port/name", b);

} catch (Exception e) {...

}......

rmiregistry &

Get the stub (1)

-11-

Page 12: RMI Fundamentals

public class Bextends UnicastRemoteObjectimplements R {

public B() throws RemoteException {super();

}public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

...

...R r;try {

Registry rmiR = LocateRegistry.getRegistry(1099);

r = (R) (rmiR.lookup("rmi://host:port/name));

r.method1();} catch (Exception e) {

...}......m.k(r);......

...

...try {

B b = new B();Registry rmiR = LocateRegistry.createRegistry(1099);rmiR.rebind("rmi://host:port/name", b);

} catch (Exception e) {...

}......

Get the stub (2)

-12-

Page 13: RMI Fundamentals

public class Bextends UnicastRemoteObjectimplements R {

public B() throws RemoteException {super();

}public void method1()

throws RemoteException {...

}public int method2(double d)

throws RemoteException {...

}...

}

Local Remote

public interface R extends Remote {public void method1() throws RemoteException;public int method2(double d) throws RemoteException;

}

...

...R r;try {

Registry rmiR = LocateRegistry.getRegistry(1099);

r = (R) (rmiR.lookup("rmi://host:port/name));

r.method1();} catch (Exception e) {

...}......m.k(r);......

...

...try {

B b = new B();Registry rmiR = LocateRegistry.createRegistry(1099);rmiR.rebind("rmi://host:port/name", b);

} catch (Exception e) {...

}......

B_stub.class rmic

Generate the Stub

-13-

Page 14: RMI Fundamentals

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-

Page 15: RMI Fundamentals

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-

Page 16: RMI Fundamentals

Recommanded Readings

• http://java.sun.com/docs/books/tutorial/rmi/index.html

• http://developer.java.sun.com/developer/onlineTraining/rmi/RMI.html

• http://java.sun.com/j2se/1.4.1/docs/guide/rmi/index.html

-16-