Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme.

Similar presentations


Presentation on theme: "Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme."— Presentation transcript:

1 Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme

2 Programs/objects running on different virtual machines: separately compiled/linked allocated in different address spaces  object by value message passing Approach: (very natural in object technology) definition of behavior  interface implementation of behavior  class

3 The concept client object client object server object server object STUB SKELETON

4 RMI architecture layers transport layer remote reference layer stubs & skeletons client remote reference layer stubs & skeletons server RMI system

5 Stubs: plays the role of PROXY Skeletons: helper class: generated automatically (Java 2: new protocol + reflection classes  no skeleton class)

6 Remote Reference Layer (RRL) RRL defines the invocation semantics Connections: unicast, point-to-point (currently) Stages: server instantiates service exporting to RMI optional: naming and registering (Java 2: activable remote object (i.e. “on-demand instantiation”)

7 Transport layer JRE Host OS Network layer JRE Host OS Network layer network cable On TCP/IP: Java Remote Method Protocol (JRMP) Other proprietary protocols are also in use!! (Java 2 v1.3: RMI-IIOP (straight link to CORBA))

8 Naming The big question: How can a client find a particular service? RMI stubs & skeletons client stubs & skeletons server RMI registry register lookup

9 RMI can use various directory services. RMI includes: rmiregistry service: - the registry service runs on each machine, which hosts remote objects and accepts requests for services - default TCP/IP port: 1099 Client: the RMI registry service provides lookup method specification of required remote object service: URL rmi:// [: ] /

10 Using RMI Components of an RMI based application: interface definitions for remote services implementations of remote services stub and skeleton files server(s) to host the services RMI naming service client program optional: class file provider Example: remote calculator service and client program

11 Step 1: write and compile service interfaces import java.rmi.Remote; import java.rmi.RemoteException; public interface Calculator extends Remote { public long add(long a, long b) throws RemoteException; public long sub(long a, long b) throws RemoteException; public long mul(long a, long b) throws RemoteException; public long div(long a, long b) throws RemoteException; }

12 Step 2: write and compile service implementation import java.rmi.server.UnicastRemoteObject; import java.rmi.RemoteException; public class CalculatorImpl extends UnicastRemoteObject implements Calculator { // Implementations must have an explicit constructor // in order to declare the RemoteException exception public CalculatorImpl() throws RemoteException { super(); } public long add(long a, long b) throws RemoteException { return a + b; } public long sub(long a, long b) throws RemoteException { return a - b; } public long mul(long a, long b) throws RemoteException { return a * b; } public long div(long a, long b) throws RemoteException { return a / b; }

13 Step 3: generate stub and skeleton classes RMI compiler: part of JDK rmic CalculatorImpl Result: CalculatorImpl_Stub.class CalculatorImpl_Skel.class

14 Step 4: write remote service class import java.rmi.Naming; public class CalculatorServer { public CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind("rmi://localhost:1099/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); } public static void main(String args[]) { new CalculatorServer(); }

15 Step 5: write client program import java.rmi.Naming; import java.rmi.RemoteException; import java.net.MalformedURLException; import java.rmi.NotBoundException; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator) Naming.lookup("rmi://localhost/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 (MalformedURLException murle) { } catch (RemoteException re) { } catch (NotBoundException nbe) { } catch (java.lang.ArithmeticException ae){ }

16 Remarks on parameter passing in RMI Parameters in a single JVM: primitive types: by value object types: “object reference by value” Parameters in RMI: primitive types: by value object types: (of course) by value  object serialization local reference to a remote object: reference to the proxy


Download ppt "Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme."

Similar presentations


Ads by Google