Download presentation
Presentation is loading. Please wait.
Published byPhoebe Watts Modified over 9 years ago
1
Remote Method Invocation http://developer.java.sun.com/developer/ onlineTraining/rmi/RMI.html
2
RMI Abstraction Local object Remote Object Client Server Method call Return Argument
3
Behavior Vs. Implementation Interface Implementation Client program Server program RMI System
4
How RMI works?
5
Example : Remote Calculator
6
The Interface public 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; }
7
Remote Object Implementation public 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() }
8
Creating Stubs and Skeletons Compile the interface % javac Calculator.java Compile the implementation % javac CalculatorImpl.java Create Stubs and Skeletons % rmic CalculatorImpl This creates CalculatorImpl_Stub.class and CalculatorImpl_Skel.class
9
Server Code import 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(); }
10
Client Code import 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); }
11
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
12
Running it all…. At server machine % rmiregistry & % java CalculatorServer At client machine % java CalculatorClient
13
Parameter Passing & Return Values
14
Single JVM Primitive data types –Pass by value Objects –Pass by reference –All objects in Java are on the heap.
15
Between Remote JVMs Using 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?
16
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.
17
Remote Object Passing Client A Server Client B Naming.lookup()
18
Remote Object Passing Client A Server Client B Naming.lookup() Return Value of another RMI
19
Remote Object Passing Client A Server Client B Naming.lookup()
20
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.