Top Banner
11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming Java Remote Method Java Remote Method Invocation (RMI) Invocation (RMI) Viraj Bhat Viraj Bhat ( ( [email protected] [email protected] ) ) Cristina Schmidt Cristina Schmidt ([email protected]) ([email protected])
65

Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

May 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Java Remote Method Java Remote Method Invocation (RMI)Invocation (RMI)

Viraj Bhat Viraj Bhat (([email protected]@caip.rutgers.edu))

Cristina SchmidtCristina Schmidt ([email protected])([email protected])

Page 2: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Distributed SystemsDistributed Systems

a collection of independent computers that a collection of independent computers that appears to its users as a single coherent appears to its users as a single coherent systemsystem

Page 3: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Models of DistributionModels of Distribution

Message passingMessage passingDistributed objectsDistributed objectsEventEvent--based architecturesbased architecturesSpaceSpace--based paradigmsbased paradigms

Page 4: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Distributed Object ModelDistributed Object Model

Views a distributed system as a series of Views a distributed system as a series of interacting objectsinteracting objectsBased on some underlying message Based on some underlying message passing protocol invisible to the passing protocol invisible to the programmerprogrammerThree main technologies: Three main technologies: RMIRMI, CORBA , CORBA and DCOMand DCOM

Page 5: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Distributed Object ComputingDistributed Object Computing

Enable any object in the local system to directly interact Enable any object in the local system to directly interact with an object on a remote hostwith an object on a remote hostGoals:Goals:

Let any object reside anywhere in the network, and allow an Let any object reside anywhere in the network, and allow an application to interact with these objects in the same way as application to interact with these objects in the same way as they do with a local object.they do with a local object.Provide the ability to construct an object on one host and Provide the ability to construct an object on one host and transmit it to another host.transmit it to another host.Enable an agent on one host to create an object on another Enable an agent on one host to create an object on another host.host.

Page 6: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

What Is RMI?What Is RMI?

A mechanism that allows the invocation of A mechanism that allows the invocation of a method that exists in another address a method that exists in another address spacespaceJavaJava--toto--Java Java onlyonlyClientClient--Server ProtocolServer ProtocolHighHigh--level APIlevel APITransparentTransparentLightweightLightweight

Page 7: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Examples of UseExamples of Use

Database accessDatabase accessComputationsComputationsAny custom protocolAny custom protocolNotNot for standard protocols (HTTP, FTP, for standard protocols (HTTP, FTP, etc.)etc.)

Page 8: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Related TechnologiesRelated Technologies

RPCRPC (“Remote Procedure Calls”)(“Remote Procedure Calls”)Developed by SunDeveloped by SunPlatformPlatform--specificspecific

CORBACORBA (“Common Object Request Broker Architecture”)(“Common Object Request Broker Architecture”)Developed by OMGDeveloped by OMGAccess to nonAccess to non--Java objects (as well as Java)Java objects (as well as Java)

DCOMDCOM (“Distributed Common Object Model”)(“Distributed Common Object Model”)Developed by MicrosoftDeveloped by MicrosoftAccess to Win32 objectsAccess to Win32 objects

LDAPLDAP (“Lightweight Directory Access Protocol”)(“Lightweight Directory Access Protocol”)Finding resources on a networkFinding resources on a network

Page 9: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Part I: RMI ConceptsPart I: RMI Concepts

Page 10: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMIRMI

Client Client –– the process that invokes a method on a the process that invokes a method on a remote objectremote objectServer Server –– the process that owns the remote the process that owns the remote objectobjectObject Registry Object Registry –– a name server that relates a name server that relates objects with namesobjects with names

Objects are registered with the Object Registry, under Objects are registered with the Object Registry, under a unique name.a unique name.The Object Registry is used to obtain access to The Object Registry is used to obtain access to remote objects, using their names.remote objects, using their names.

Page 11: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote Objects (Diagram)Remote Objects (Diagram)

TCP

Java Virtual Machine

Client Object

Java Virtual Machine

Remote Object

Client Server

Page 12: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI LayersRMI Layers

TCPRemote Reference Layer

Transport Layer

Java Virtual Machine

Client Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Page 13: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI Architecture in the OSI modelRMI Architecture in the OSI modelUser Application

Stub Skeleton

Remote Reference Layer

TCP

IP

Hardware Interface

Network

Application Layer

Presentation Layer

Session Layer

Transport Layer

Network Layer

Data-link layerPhysical Layer

Page 14: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

TCPRemote Reference Layer

Transport Layer

Java Virtual MachineClient Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

Remote ObjectsRemote Objects

Remote ObjectsRemote ObjectsLive on serverLive on serverAccessed as if they were localAccessed as if they were local

Page 15: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Stubs and SkeletonsStubs and SkeletonsTCP

Remote Reference Layer

Transport Layer

Java Virtual MachineClient Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

StubStublives on clientlives on clientpretends to be remote object pretends to be remote object -- a proxy for the remote objecta proxy for the remote object

SkeletonSkeletonlives on serverlives on serverreceives requests from stub, talks to the remote object and receives requests from stub, talks to the remote object and delivers response to stubdelivers response to stub

Stubs and skeletons are not written by the programmer! Stubs and skeletons are not written by the programmer! They are generated by a special compiler “rmic”They are generated by a special compiler “rmic”

Page 16: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Stubs and SkeletonsStubs and SkeletonsTCP

Remote Reference Layer

Transport Layer

Java Virtual MachineClient Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

Stub Stub –– responsibilitiesresponsibilitiesInitiate remote calls Initiate remote calls Marshals arguments to be sent Marshals arguments to be sent Informs the remote reference layer that a call Informs the remote reference layer that a call should be invoked on the server should be invoked on the server Unmarshals a return value (or exception) Unmarshals a return value (or exception) Informs the remote reference layer that the Informs the remote reference layer that the call is complete call is complete

Page 17: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Stubs and SkeletonsStubs and SkeletonsTCP

Remote Reference Layer

Transport Layer

Java Virtual MachineClient Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

Skeleton Skeleton –– responsibilitiesresponsibilitiesUnmarshals incoming arguments Unmarshals incoming arguments Calls the actual remote object implementation Calls the actual remote object implementation Marshals return values for transport to the Marshals return values for transport to the client client

Marshaling Marshaling –– definitiondefinitionThe process of converting native The process of converting native programming language data types to a format programming language data types to a format suitable for transmission across a network suitable for transmission across a network

Page 18: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote Interfaces and StubsRemote Interfaces and Stubs

Remote Interface

Stub Remote Object(Server)Client Skeleton

implements implements

Page 19: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote InterfacesRemote Interfaces

Declare exposed methods Declare exposed methods –– the methods the methods that can be called from remote locationsthat can be called from remote locationsExtend Extend java.rmi.Remotejava.rmi.RemoteThe remote object implements this The remote object implements this interfaceinterfaceAct like a proxy for the remote objectAct like a proxy for the remote object

Page 20: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote Reference LayerRemote Reference LayerTCP

Remote Reference Layer

Transport Layer

Java Virtual MachineClient Object

Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

Sets up connections to remote address Sets up connections to remote address spaces spaces Manages connections Manages connections Listens for incoming calls Listens for incoming calls Communicates via TCP/IPCommunicates via TCP/IP

Page 21: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Object RegistriesObject Registries

Name and look up remote objectsName and look up remote objectsRemote objects register by nameRemote objects register by nameClients obtain a remote reference to the Clients obtain a remote reference to the remote objectremote objectA registry is a A registry is a running processrunning process on the on the same host as the RMI serversame host as the RMI server

Page 22: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

HTTP TunnelingHTTP Tunneling

Cool: if it can’t make the connection Cool: if it can’t make the connection normally, it will tunnel through port 80normally, it will tunnel through port 80Allows clients behind firewall to make Allows clients behind firewall to make remote calls to serverremote calls to serverNote: does not work server Note: does not work server --> client> client

Page 23: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI System ArchitectureRMI System ArchitectureClient Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“Fred”

Server

ServerClient

Registry

Page 24: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI FlowRMI FlowClient Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“Fred”

Server

1

2

1. Server Creates Remote Object2. Server Registers Remote Object

Page 25: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI FlowRMI FlowClient Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“Fred”

Server4

3. Client requests object from Registry4. Registry returns remote reference

3

Page 26: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI FlowRMI FlowClient Virtual Machine

Client

Server Virtual Machine

Stub

Remote Object

Skeleton

Registry Virtual Machine

“Fred”

Server

6

5. Client invokes stub method6. Stub talks to skeleton7. Skeleton invokes remote object

method

5 7

Page 27: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Part II: RMI UsagePart II: RMI Usage

Page 28: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Creating Remote ObjectsCreating Remote Objects

Define a Remote InterfaceDefine a Remote Interfaceextends java.rmi.Remoteextends java.rmi.Remote

Define a class that implements the Define a class that implements the Remote InterfaceRemote Interface

extends java.rmi.RemoteObjectextends java.rmi.RemoteObjector java.rmi.UnicastRemoteObjector java.rmi.UnicastRemoteObject

Page 29: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote Interface ExampleRemote Interface Example

import java.rmi.*;import java.rmi.*;

public interface Adder public interface Adder

extends Remoteextends Remote

{{

public int add(int x, int y)public int add(int x, int y)

throws RemoteException;throws RemoteException;

}}

Page 30: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote Class ExampleRemote Class Exampleimport java.rmi.*;import java.rmi.*;import java.rmi.server.*;import java.rmi.server.*;public class AdderImpl public class AdderImpl extends UnicastRemoteObjectextends UnicastRemoteObject

implements Adderimplements Adder{{

public AdderImpl() throws RemoteExceptionpublic AdderImpl() throws RemoteException{{}}public int add(int x, int y) public int add(int x, int y)

throws RemoteExceptionthrows RemoteException{{

return x + y;return x + y;}}

}}

Page 31: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Inheritance Diagram in JavaInheritance Diagram in JavaObject

UnicastRemoteObject

RemoteObject

RemoteServerRemoteStub

Remote

Page 32: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Compiling Remote ClassesCompiling Remote Classes

Compile the Java classCompile the Java classjavacjavac

reads .java filereads .java fileproduces .class fileproduces .class file

Compile the Stub and SkeletonCompile the Stub and Skeletonrmicrmic

reads .class filereads .class fileproduces _Skel.class and _Stub.classproduces _Skel.class and _Stub.class

Page 33: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Compiling Remote Classes Compiling Remote Classes (Diagram)(Diagram)

Adder.java(interface)

Adder.class(interface classfile)

javac

AdderImpl.java(remote class)

AdderImpl.class(classfile)

javacrmic

AdderImpl_Skel.class(skeleton classfile)

AdderImpl_Stub.class(stub classfile)

Page 34: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Registering Remote ClassesRegistering Remote Classes

start the registrystart the registryrunning processrunning process

Unix: Unix: rmiregistry &rmiregistry &

Windows: Windows: start /m rmiregistrystart /m rmiregistry

Page 35: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Registry CLASSPATHRegistry CLASSPATHRegistry VM needs to be able to find stub file(s)Registry VM needs to be able to find stub file(s)You must set the CLASSPATH to include the You must set the CLASSPATH to include the directory containing the stub filedirectory containing the stub fileAn easy way to check CLASSPATH is to use the An easy way to check CLASSPATH is to use the javapjavap command, supplying command, supplying a fully package qualified class name. It uses the current CLASSPa fully package qualified class name. It uses the current CLASSPATH to find ATH to find

and print the interface to a class.and print the interface to a class.

Or, your server needs to specify the Or, your server needs to specify the java.rmi.server.codebase System property (more java.rmi.server.codebase System property (more later)later)

Page 36: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Create the serverCreate the server

Creates a new instance of the remote Creates a new instance of the remote objectobjectRegisters it in the registry with a nameRegisters it in the registry with a nameThat’s itThat’s it

Page 37: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI Server ExampleRMI Server Example

try {try {

AdderImpl adder = AdderImpl adder = newnew AdderImpl();AdderImpl();

Naming.Naming.rebindrebind("adder", adder);("adder", adder);

System.out.println("Adder bound");System.out.println("Adder bound");

}}

catch (RemoteException re) {catch (RemoteException re) {

re.printStackTrace();re.printStackTrace();

}}

catch (MalformedURLException me) {catch (MalformedURLException me) {

me.printStackTrace();me.printStackTrace();

}}

Page 38: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Launch the ServerLaunch the Server

% java AdderServer &% java AdderServer &

Adder boundAdder bound

Page 39: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Server LoggingServer Logging

invoke from command lineinvoke from command linejava java

--Djava.rmi.server.logCalls=true Djava.rmi.server.logCalls=true YourServerImplYourServerImpl

or enable inside programor enable inside programRemoteServer.setLog(System.err);RemoteServer.setLog(System.err);

Page 40: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Creating an RMI ClientCreating an RMI Client

Install a Security ManagerInstall a Security Managerto protect from malicious stubsto protect from malicious stubs

Find a registryFind a registryuse java.rmi.Naminguse java.rmi.Naming

Lookup the name, returns a referenceLookup the name, returns a referenceCast the reference to the appropriate Cast the reference to the appropriate Remote InterfaceRemote InterfaceJust use it!Just use it!

Page 41: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI URLsRMI URLs

rmi://rmi://hosthost[:[:portport]/]/namename

default port is 1099default port is 1099Specifies hostname of Specifies hostname of registryregistrycan also use relative URLs can also use relative URLs

name onlyname onlyassumes registry is on local hostassumes registry is on local host

Page 42: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI Client ExampleRMI Client Example

System.setSecurityManager(System.setSecurityManager(

new RMISecurityManager());new RMISecurityManager());

Adder a = (Adder) Adder a = (Adder) Naming.Naming.lookuplookup("adder");("adder");

int sum = a.add(2,2);int sum = a.add(2,2);

System.out.println("2+2=" + sum);System.out.println("2+2=" + sum);

Page 43: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Remote Interfaces vs. Remote Remote Interfaces vs. Remote ClassesClasses

Remember that the reference is to an Remember that the reference is to an interfaceinterfaceYou must make references, arrays, etc. out of You must make references, arrays, etc. out of the the interfaceinterface type, not the implementation typetype, not the implementation typeYou can’t cast the remote reference to a normal You can’t cast the remote reference to a normal referencereferenceSo name your Remote Objects with “So name your Remote Objects with “ImplImpl” (so ” (so you don’t get confused)you don’t get confused)

Page 44: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Parameter PassingParameter PassingAll parameters are passed by valueAll parameters are passed by value

Primitive typesPrimitive typespassed by valuepassed by value

ObjectsObjectspassed by valuepassed by valueuse Java Object Serializationuse Java Object Serialization

Page 45: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Object SerializationObject Serialization

saves the state (data) of a particular saves the state (data) of a particular instance of an objectinstance of an objectserializeserialize -- to saveto saveunserialize unserialize -- to loadto load

Page 46: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Java SerializationJava Serialization

writes object as a sequence of byteswrites object as a sequence of byteswrites it to a Streamwrites it to a Streamrecreates it on the other endrecreates it on the other endcreates a brand new object with the old creates a brand new object with the old datadata

Page 47: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

java.io.Serializablejava.io.SerializableObjects that implement the Objects that implement the java.io.Serializable interface are marked as java.io.Serializable interface are marked as serializableserializableAlso subclassesAlso subclassesempty interface empty interface -- just a marker just a marker –– no need no need to implement any special methodsto implement any special methods

Page 48: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Not All Objects Are SerializableNot All Objects Are SerializableAny object that doesn’t implement SerializableAny object that doesn’t implement SerializableAny object that would pose a security riskAny object that would pose a security risk

e.g. FileInputStreame.g. FileInputStream

Any object whose value depends on VMAny object whose value depends on VM--specific informationspecific information

e.g. Threade.g. Thread

Any object that Any object that containscontains a a (non(non--static, nonstatic, non--transient)transient)

unserializable object unserializable object (recursively)(recursively)

Page 49: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

NotSerializableExceptionNotSerializableException

thrown if you try to serialize or unserialize thrown if you try to serialize or unserialize an unserializable objectan unserializable objectmaybe you subclassed a Serializable maybe you subclassed a Serializable object and added some unserializable object and added some unserializable membersmembers

Page 50: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Incompatible ChangesIncompatible Changes

If class has members added or removed, it If class has members added or removed, it becomes becomes incompatibleincompatiblejava.io.InvalidClassException thrown if you java.io.InvalidClassException thrown if you try to deserialize an incompatible object try to deserialize an incompatible object streamstream

Page 51: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Serial VersionSerial Version

If the changes were actually compatibleIf the changes were actually compatiblefind out the Serial Version UID of the find out the Serial Version UID of the originaloriginalclassclass

use the use the serialverserialver utilityutility

add a member variable to the add a member variable to the changedchanged classclassprotected static final long serialVersionUID = protected static final long serialVersionUID = --

2215190743590612933L;2215190743590612933L;

now it’s marked as compatible with the old classnow it’s marked as compatible with the old class

Page 52: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Using readObjectUsing readObjectif you need to force an object to be if you need to force an object to be compatiblecompatibleimplement readObject() method to make implement readObject() method to make compatible changescompatible changes

private void readObject(ObjectInputStream private void readObject(ObjectInputStream stream) throws java.io.IOExceptionstream) throws java.io.IOException

{{defaultReadObject(stream);defaultReadObject(stream);// do compatible stuff// do compatible stuff

} }

Page 53: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

CallbacksCallbacks

The server calls a remote method on the The server calls a remote method on the client => the server becomes the client, client => the server becomes the client, and the client is now the serverand the client is now the serverThe client registers a notification method The client registers a notification method with the serverwith the serverServer invokes the method (“calls back”) Server invokes the method (“calls back”) to notify the clientto notify the clientRegistry is out of the loopRegistry is out of the loop

Page 54: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI SecurityRMI Security

Server is untrustedServer is untrustedStubs could be maliciousStubs could be maliciousrmic is OK, but someone could customrmic is OK, but someone could custom--code an evil stub: it’s just a .class filecode an evil stub: it’s just a .class file

Page 55: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI Security ManagersRMI Security ManagersAppletSecurityManagerAppletSecurityManager

stub can only do what an applet can dostub can only do what an applet can do

RMISecurityManagerRMISecurityManagerdisables all functions except class definition and disables all functions except class definition and accessaccessA downloaded class is allowed to make a connection A downloaded class is allowed to make a connection if the connection was initiated via the RMI if the connection was initiated via the RMI transport. transport.

NoneNoneStub loading disabledStub loading disabledStubs still work if they are in local classpathStubs still work if they are in local classpath

Page 56: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Codebase PropertyCodebase PropertyStub classpaths can be confusingStub classpaths can be confusing

3 VMs, each with its own classpath3 VMs, each with its own classpathServer vs. Registry vs. ClientServer vs. Registry vs. Client

The RMI class loader always loads stubs from the The RMI class loader always loads stubs from the CLASSPATH firstCLASSPATH firstNext, it tries downloading classes from a web Next, it tries downloading classes from a web serverserver

(but only if a security manager is in force)(but only if a security manager is in force)

java.rmi.server.codebase specifies which web java.rmi.server.codebase specifies which web serverserver

Page 57: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Stub File ConfigurationStub File Configuration

The best way to configure it is as follows.The best way to configure it is as follows.NEVER have stub class files in ANY classpathNEVER have stub class files in ANY classpathmake the stub files accessible via a web servermake the stub files accessible via a web serverset the java.rmi.server.codebase property, in the set the java.rmi.server.codebase property, in the application creating the server object, to the web application creating the server object, to the web server's URLserver's URL

The stubs will be downloaded from your HTTP The stubs will be downloaded from your HTTP server on demandserver on demand(Thanks to [email protected] for advice)(Thanks to [email protected] for advice)

Page 58: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Codebase and Thread SafetyCodebase and Thread Safety

There’s a thread problem with codebase There’s a thread problem with codebase With a single remote server, and multiple With a single remote server, and multiple remote objects on that server, each with its remote objects on that server, each with its own stub codebaseown stub codebaseOne server can step on the codebase property One server can step on the codebase property and foul up the registration for the other and foul up the registration for the other serverserverIn practice, won’t happen very oftenIn practice, won’t happen very often

More likely to happen in servletsMore likely to happen in servlets

Page 59: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Limitations of RMILimitations of RMI

JavaJava--onlyonlybut you can use JNI on the serverbut you can use JNI on the server

Uses TCP, not UDPUses TCP, not UDPAt least two sockets per connectionAt least two sockets per connectionUntested for huge loadsUntested for huge loads

Page 60: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI vs. COMRMI vs. COM

Very similarVery similarremote interfaces ~ type librariesremote interfaces ~ type librariesCOM is Win32COM is Win32--only (for now)only (for now)

Page 61: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Sun vs. MicrosoftSun vs. MicrosoftRMI is not shipped as part of Microsoft’s RMI is not shipped as part of Microsoft’s productsproductsRMI will still work in applicationsRMI will still work in applications

include java.rmi.* class files in your classpathinclude java.rmi.* class files in your classpathdownload rmi.zip from ftp.microsoft.comdownload rmi.zip from ftp.microsoft.com

RMI will work in applets RMI will work in applets include java.rmi.* class files (or rmi.zip) in your include java.rmi.* class files (or rmi.zip) in your codebasecodebaseextra download timeextra download time

Page 62: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

RMI Chat Server ObjectsRMI Chat Server ObjectsMessage

interface

MessageReceiver- receiveMessage(Message)

ChatClient

interface

ChatServer- login(MessageReceiver)- sendMessage(Message)

ChatServerImpl

Dispatcher MessageQueueimplementsremote referencelocal reference

Page 63: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

SummarySummary

RMI is a very clean APIRMI is a very clean APIEasy way to write distributed programsEasy way to write distributed programsWire protocol may need improvement for Wire protocol may need improvement for largelarge--scale problemsscale problems

Page 64: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

Where to get more informationWhere to get more information

Core Java Volume II by Cay S Horstmann and Core Java Volume II by Cay S Horstmann and Gray CornellGray CornellHarold, Harold, Java Network ProgrammingJava Network Programming (O’Reilly)(O’Reilly)rmirmi--users mailing list users mailing list (rmi([email protected])[email protected])

http://www.developer.com/ (Gamelan)http://www.developer.com/ (Gamelan)http://www.javaworld.com/ (magazine)http://www.javaworld.com/ (magazine)http://www.stinky.com/java/ (original Author’s http://www.stinky.com/java/ (original Author’s site)site)

Page 65: Java Remote Method Invocation (RMI) - Rutgers ECEirodero/classes/09-10/... · 11/18/2003 ECE 451:Introduction to Parallel and Distributed Programming RMI Client – the process that

11/18/200311/18/2003ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel

and Distributed Programmingand Distributed Programming

My sincere thanksMy sincere thanks

Alex Chaffee’s Purple Technology and his Alex Chaffee’s Purple Technology and his RMI slides which I have used in my RMI slides which I have used in my PresentationPresentation