Presentation is loading. Please wait.

Presentation is loading. Please wait.

Florida State UniversityCOP5570 - Advanced Unix Programming Remote Method Invocation /rmi/index.html.

Similar presentations


Presentation on theme: "Florida State UniversityCOP5570 - Advanced Unix Programming Remote Method Invocation /rmi/index.html."— Presentation transcript:

1 Florida State UniversityCOP5570 - Advanced Unix Programming Remote Method Invocation http://java.sun.com/developer/onlineTraining /rmi/index.html

2 Florida State UniversityCOP5570 - Advanced Unix Programming RMI Abstraction Local object Remote Object Client Server Method call Return Argument

3 Florida State UniversityCOP5570 - Advanced Unix Programming Behavior Vs. Implementation Interface Implementation Client program Server program RMI System

4 Florida State UniversityCOP5570 - Advanced Unix Programming How RMI Works Client ProgramServer Program Server Stub Server Skeleton Remote Reference Layer TCP/IP

5 Florida State UniversityCOP5570 - Advanced Unix Programming 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.

6 Florida State UniversityCOP5570 - Advanced Unix Programming Example : Remote Calculator

7 Florida State UniversityCOP5570 - Advanced Unix Programming 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; }

8 Florida State UniversityCOP5570 - Advanced Unix Programming 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 implement mul() and div() }

9 Florida State UniversityCOP5570 - Advanced Unix Programming Alternative Remote Object Implementation public class CalculatorImpl implements Calculator //Doesn’t extend UnicastRemoteObject { // constructor doesn’t call super() – instead calls exportObject() public CalculatorImpl() { UnicastRemoteObject.exportObject(this); } 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 implement mul() and div() }

10 Florida State UniversityCOP5570 - Advanced Unix Programming 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

11 Florida State UniversityCOP5570 - Advanced Unix Programming Server Code import java.rmi.Naming; public class CalculatorServer { public static void main(String args[]) { try { Calculator c = new CalculatorImpl(); Naming.rebind( "rmi://linprog1/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); }

12 Florida State UniversityCOP5570 - Advanced Unix Programming Client Code import java.rmi.*; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator)Naming.lookup( "rmi://linprog1/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); }

13 Florida State UniversityCOP5570 - Advanced Unix Programming Running it all Copy the following to server machine –CalculatorServer.class –CalculatorImpl_Skel.class –CalculatorImpl.class –Calculator.class Copy the following to client machine –CalculatorClient.class –CalculatorImpl_Stub.class –Calculator.class

14 Florida State UniversityCOP5570 - Advanced Unix Programming Running it all…. At server machine % rmiregistry & % java CalculatorServer At client machine % java CalculatorClient

15 Florida State UniversityCOP5570 - Advanced Unix Programming Parameter Passing & Return Values

16 Florida State UniversityCOP5570 - Advanced Unix Programming Single JVM Primitive data types –Pass by value Objects –Pass by reference –All objects in Java are on the heap.

17 Florida State UniversityCOP5570 - Advanced Unix Programming 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?

18 Florida State UniversityCOP5570 - Advanced Unix Programming 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”.


Download ppt "Florida State UniversityCOP5570 - Advanced Unix Programming Remote Method Invocation /rmi/index.html."

Similar presentations


Ads by Google