remote method invocation onlinetraining/rmi/rmi.html
TRANSCRIPT
Remote Method Invocation
http://developer.java.sun.com/developer/
onlineTraining/rmi/RMI.html
RMI Abstraction
Local object Remote Object
Client Server
Method call
Return Argument
Behavior Vs. Implementation
Interface Implementation
Client program Server program
RMI System
How RMI works?
Example : Remote Calculator
The Interfacepublic interface Calculator extends java.rmi.Remote { public long add(long a, long b) throws java.rmi.RemoteException; public long sub(long a, long b) throws java.rmi.RemoteException; public long mul(long a, long b) throws java.rmi.RemoteException; public long div(long a, long b) throws java.rmi.RemoteException; }
Remote Object Implementationpublic class CalculatorImpl
extends java.rmi.server.UnicastRemoteObject implements Calculator { // Must have an explicit constructor
public CalculatorImpl() throws java.rmi.RemoteException { super(); } public long add(long a, long b) throws java.rmi.RemoteException { return a + b; } public long sub(long a, long b) throws java.rmi.RemoteException { return a - b; }
// Similarly declare mul() and div()}
Creating Stubs and Skeletons
Compile the interface
% javac Calculator.java
Compile the implementation
% javac CalculatorImpl.java
Create Stubs and Skeletons
% rmic CalculatorImplThis creates CalculatorImpl_Stub.class and CalculatorImpl_Skel.class
Server Codeimport java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() { try { Calculator c = new CalculatorImpl();
Naming.rebind("rmi://sbpub1:1099/CalculatorService", c);
} catch (Exception e) { System.out.println("Trouble: " + e); } }
public static void main(String args[]) { new CalculatorServer(); }}
Client Codeimport java.rmi.*;
public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator)Naming.lookup( "rmi://sbpub1/CalculatorService"); System.out.println( c.sub(4, 3) ); System.out.println( c.add(4, 5) ); System.out.println( c.mul(3, 6) ); System.out.println( c.div(9, 3) ); } catch (Exception e) {
System.out.println("Trouble: " + e); }
}
Running it all
• Copy the following to server machine– CalculatorServer.class– CalculatorImpl_Skel.class– CalculatorImpl_Stub.class– Calculator.class
• Copy the following to client machine– CalculatorClient.class– CalculatorImpl_Stub.class– Calculator.class
Running it all….
• At server machine% rmiregistry &
% java CalculatorServer
• At client machine% java CalculatorClient
Parameter Passing &Return Values
Single JVM
• Primitive data types– Pass by value
• Objects– Pass by reference– All objects in Java are on the heap.
Between Remote JVMsUsing RMI
• Primitive data types– By value
• Local Objects – By value again.– No common heap between remote JVMs!– But what’s the problem in passing objects by
value?
Serialization
• Flatten the object being passed and any objects it references.
• Need to “marshall” (copy) all members fields of objects being passed.
• Fields may be object references!
• So follow the object reference and perform a deep-copy.
Remote Object Passing
Client A
Server
Client B
Naming.lookup()
Remote Object Passing
Client A
Server
Client B
Naming.lookup()
Return Value ofanother RMI
Remote Object Passing
Client A
Server
Client B
Naming.lookup()
Local Vs. Remote Objects• Object reference
points to local object.
• Object arguments in methods are passed by reference.
• Garbage collection when no more local references.
• Object reference points to a proxy (stub) object.
• Object arguments in methods are passed by value.
• Garbage collection when no more local or remote references.