George Blank University Lecturer. Java RMI Introduction to RMI.
RMI Fundamentals
-
Upload
elliando-dias -
Category
Technology
-
view
1.722 -
download
3
description
Transcript of RMI Fundamentals
RMI Fundamentals
Chung-Kai Chen
-1-
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-
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-
public class B {public void method1() {
...}public int method2(double d) {
...}...
}
...
...B b = new B();......
...
...b.method1();......m.k(b);......
Local Remote
-4-
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-
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-
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-
...
...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-
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-
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-
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-
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-
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-
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-
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-
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-