remote method invocation onlinetraining/rmi/rmi.html

20
Remote Method Invocation http://developer.java.sun.com/develo per/ onlineTraining/rmi/RMI.html

Upload: phoebe-watts

Post on 04-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Remote Method Invocation  onlineTraining/rmi/RMI.html

Remote Method Invocation

http://developer.java.sun.com/developer/

onlineTraining/rmi/RMI.html

Page 2: Remote Method Invocation  onlineTraining/rmi/RMI.html

RMI Abstraction

Local object Remote Object

Client Server

Method call

Return Argument

Page 3: Remote Method Invocation  onlineTraining/rmi/RMI.html

Behavior Vs. Implementation

Interface Implementation

Client program Server program

RMI System

Page 4: Remote Method Invocation  onlineTraining/rmi/RMI.html

How RMI works?

Page 5: Remote Method Invocation  onlineTraining/rmi/RMI.html

Example : Remote Calculator

Page 6: Remote Method Invocation  onlineTraining/rmi/RMI.html

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; }

Page 7: Remote Method Invocation  onlineTraining/rmi/RMI.html

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()}

Page 8: Remote Method Invocation  onlineTraining/rmi/RMI.html

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

Page 9: Remote Method Invocation  onlineTraining/rmi/RMI.html

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(); }}

Page 10: Remote Method Invocation  onlineTraining/rmi/RMI.html

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); }

}

Page 11: Remote Method Invocation  onlineTraining/rmi/RMI.html

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

Page 12: Remote Method Invocation  onlineTraining/rmi/RMI.html

Running it all….

• At server machine% rmiregistry &

% java CalculatorServer

• At client machine% java CalculatorClient

Page 13: Remote Method Invocation  onlineTraining/rmi/RMI.html

Parameter Passing &Return Values

Page 14: Remote Method Invocation  onlineTraining/rmi/RMI.html

Single JVM

• Primitive data types– Pass by value

• Objects– Pass by reference– All objects in Java are on the heap.

Page 15: Remote Method Invocation  onlineTraining/rmi/RMI.html

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?

Page 16: Remote Method Invocation  onlineTraining/rmi/RMI.html

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.

Page 17: Remote Method Invocation  onlineTraining/rmi/RMI.html

Remote Object Passing

Client A

Server

Client B

Naming.lookup()

Page 18: Remote Method Invocation  onlineTraining/rmi/RMI.html

Remote Object Passing

Client A

Server

Client B

Naming.lookup()

Return Value ofanother RMI

Page 19: Remote Method Invocation  onlineTraining/rmi/RMI.html

Remote Object Passing

Client A

Server

Client B

Naming.lookup()

Page 20: Remote Method Invocation  onlineTraining/rmi/RMI.html

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.