View
214
Download
1
Tags:
Embed Size (px)
Citation preview
EEC-681/781EEC-681/781Distributed Computing SystemsDistributed Computing Systems
Lecture 5Lecture 5
Wenbing ZhaoWenbing ZhaoDepartment of Electrical and Computer EngineeringDepartment of Electrical and Computer Engineering
Cleveland State UniversityCleveland State University
[email protected]@ieee.org
22
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
OutlineOutline
• Case study: Java RMI– Material taken from
http://java.sun.com/developer/onlineTraining/rmi/RMI.html
33
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Java RMI ArchitectureJava RMI Architecture
• Design goal: to create a Java distributed object model that integrates naturally into the Java programming language and the local object model
• The definition of a remote service is coded using a Java interface
• The implementation of the remote service is coded in a class
• Interfaces define behavior and classes define implementation
44
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Java RMI Architecture LayersJava RMI Architecture Layers• Stub and Skeleton layer: intercepts method calls made
by the client to the interface and redirects these calls to a remote RMI service
• Remote reference layer: to interpret and manage references made from clients to the remote service objects
• Transport layer: provides basic connectivity, as well as some firewall penetration strategies
Transport Layer
Remote Reference Layer Remote Reference Layer
Stubs & SkeletonsStubs & Skeletons
Client Program Server Program
RMISystem
55
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Stub and Skeleton LayerStub and Skeleton Layer• Java RMI follows the Proxy pattern
– The stub class plays the role of the proxy– The remote service implementation class plays the role
of the RealSubject<<Interface>>
Subject
Request()
RealSubject
Request()
Proxy
Request()
66
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Remote Reference LayerRemote Reference Layer
• Defines and supports the invocation semantics of the RMI connection– This layer provides a RemoteRef object that
represents the link to the remote service implementation object
– The stub objects use the invoke()method in RemoteRef to forward the method call
– The RemoteRef object understands the invocation semantics for remote services
77
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Transport LayerTransport Layer
• The Transport Layer makes network connection between JVMs
• Java Remote Method Protocol (JRMP): Java RMI’s wire protocol (proprietary) on top of TCP/IP
88
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
RMI RegistryRMI Registry
• RMI Registry (rmiregistry): a simple naming service that comes with Java RMI– It runs on each machine that hosts remote service
objects, by default on port 1099– It accepts registration request only from the local RMI
servers– It accepts queries for services anywhere
99
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
RMI RegistryRMI Registry
• To access the remote server from client side:– Query a registry by invoking the lookup()method on
the static Naming class– The method lookup()accepts a URL that specifies
the server host name and the name of the desired service• rmi://<host_name>[:<name_service_port>]/<service_name>
– The method returns a remote reference to the service object
1010
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Steps to Create a Remote ServiceSteps to Create a Remote Service
• First create a local object that implements that service
• Next, export that object to Java RMI. When the object is exported, Java RMI creates a listening service that waits for clients to connect and request the service
• After exporting, register the object in the Java RMI Registry under a public name
1111
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Using Java RMIUsing Java RMI• A working Java RMI system is composed of
several parts:– Interface definitions for the remote services – Implementations of the remote services – Stub and Skeleton files – A server to host the remote services – An RMI Naming service that allows clients to find the
remote services – A class file provider (an HTTP or FTP server) – A client program that needs the remote services
1212
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Steps to Build a SystemSteps to Build a System
• Design your system• Write and compile Java code for interfaces• Write and compile Java code for implementation
classes• Generate Stub and Skeleton class files from the
implementation classes• Write Java code for a remote service host program• Develop Java code for Java RMI client program• Install and run Java RMI system
1313
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Service InterfaceService 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;
}
To compile it:> javac Calculator.java
1414
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Implementation of Remote ServiceImplementation of Remote Service
public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator { // Implementations must have an explicit constructor // in order to declare the RemoteException exception public CalculatorImpl() throws java.rmi.RemoteException {
super(); } public long add(long a, long b)
throws java.rmi.RemoteException { return a + b;
} …
1515
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Stubs and SkeletonsStubs and Skeletons
• To generate the stub and skeleton files, invoke the RMI compiler, rmic
• The compiler runs on the remote service implementation class file> rmic CalculatorImpl
• After you run rmic you should find the file CalculatorImpl_Stub.class
1616
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Host ServerHost Server
• Remote RMI services must be hosted in a server process
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(); } }
1717
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
ClientClient
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) );
} …
1818
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Running the RMI SystemRunning the RMI System
• Make sure you change to the directory that contains the classes you have written
• Launch a terminal, start Java RMI registry> rmiregistry
• Launch another terminal, start the server> java CalculatorServer
• Launch the 3rd terminal, start the client> java CalculatorClient
1919
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Parameter Passing in Java RMIParameter Passing in Java RMI
• When Java RMI calls involve passing parameters or accepting a return value– How does RMI transfer these between JVMs? – What semantics are used? – Does RMI support pass-by-value or pass-by-
reference?
2020
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Parameter Passing in Java RMIParameter Passing in Java RMI
• Primitive parameters: pass by value– For both input parameter and return type,
Java RMI makes a copy of a primitive data type and send it to the destination
• Object parameters: pass by value– The object to be passed, together with all the
objects it references, are serialized and copied over
2121
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Parameter Passing in Java RMIParameter Passing in Java RMI
• Remote object parameters: pass by reference– A client can obtain a reference to a remote
object through the Java RMI Registry program– A client can obtain a remote reference as a
result of making a remote method call
2222
Fall Semester 2008Fall Semester 2008 EEC-681: Distributed Computing SystemsEEC-681: Distributed Computing Systems Wenbing ZhaoWenbing Zhao
Distributed Garbage Collection in Distributed Garbage Collection in Java RMIJava RMI
• Java takes care of memory management• Distributed garbage collection is needed in Java
RMI– Many challenges to do so. Most prominent problem is
that a client might quit without notice– Solution: lease based. The resource is granted to a
client for certain period of time. It is the client’s responsibility to renew the lease. The resource is reclaimed if lease is not renewed