Java Remote Method Java Remote Method Invocation (RMI) Invocation - - PowerPoint PPT Presentation

java remote method java remote method invocation rmi
SMART_READER_LITE
LIVE PREVIEW

Java Remote Method Java Remote Method Invocation (RMI) Invocation - - PowerPoint PPT Presentation

Java Remote Method Java Remote Method Invocation (RMI) Invocation (RMI) Viraj Bhat ( Viraj Bhat (virajb@caip.rutgers.edu virajb@caip.rutgers.edu) ) Cristina Schmidt (cristins@caip.rutgerts.edu) Cristina Schmidt (cristins@caip.rutgerts.edu)


slide-1
SLIDE 1

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 (

(virajb@caip.rutgers.edu virajb@caip.rutgers.edu) )

Cristina Schmidt Cristina Schmidt (cristins@caip.rutgerts.edu)

(cristins@caip.rutgerts.edu)

slide-2
SLIDE 2

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Distributed Systems Distributed 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 system system

slide-3
SLIDE 3

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Models of Distribution Models of Distribution

  • Message passing

Message passing

  • Distributed objects

Distributed objects

  • Event

Event-

  • based architectures

based architectures

  • Space

Space-

  • based paradigms

based paradigms

slide-4
SLIDE 4

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Distributed Object Model Distributed Object Model

  • Views a distributed system as a series of

Views a distributed system as a series of interacting objects interacting objects

  • Based on some underlying message

Based on some underlying message passing protocol invisible to the passing protocol invisible to the programmer programmer

  • Three main technologies:

Three main technologies: RMI RMI , CORBA , CORBA and DCOM and DCOM

slide-5
SLIDE 5

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Distributed Object Computing Distributed 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 host with an object on a remote host

  • Goals:

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.

slide-6
SLIDE 6

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and 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 space space

  • Java

Java-

  • to

to-

  • Java

Java only

  • nly
  • Client

Client-

  • Server Protocol

Server Protocol

  • High

High-

  • level API

level API

  • Transparent

Transparent

  • Lightweight

Lightweight

slide-7
SLIDE 7

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Examples of Use Examples of Use

  • Database access

Database access

  • Computations

Computations

  • Any custom protocol

Any custom protocol

  • Not

Not for standard protocols (HTTP, FTP, for standard protocols (HTTP, FTP, etc.) etc.)

slide-8
SLIDE 8

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Related Technologies Related Technologies

  • RPC

RPC

(“Remote Procedure Calls”) (“Remote Procedure Calls”)

  • Developed by Sun

Developed by Sun

  • Platform

Platform-

  • specific

specific

  • CORBA

CORBA

(“Common Object Request Broker Architecture”) (“Common Object Request Broker Architecture”)

  • Developed by OMG

Developed by OMG

  • Access to non

Access to non-

  • Java objects (as well as Java)

Java objects (as well as Java)

  • DCOM

DCOM

(“Distributed Common Object Model”) (“Distributed Common Object Model”)

  • Developed by Microsoft

Developed by Microsoft

  • Access to Win32 objects

Access to Win32 objects

  • LDAP

LDAP

(“Lightweight Directory Access Protocol”) (“Lightweight Directory Access Protocol”)

  • Finding resources on a network

Finding resources on a network

slide-9
SLIDE 9

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Part I: RMI Concepts Part I: RMI Concepts

slide-10
SLIDE 10

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI RMI

  • Client

Client – – the process that invokes a method on a the process that invokes a method on a remote object remote object

  • Server

Server – – the process that owns the remote the process that owns the remote

  • bject
  • bject
  • Object Registry

Object Registry – – a name server that relates a name server that relates

  • bjects with names
  • bjects 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.

slide-11
SLIDE 11

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Objects (Diagram) Remote Objects (Diagram)

TCP Java Virtual Machine Client Object Java Virtual Machine Remote Object

Client Server

slide-12
SLIDE 12

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Layers RMI Layers

TCP

Remote Reference Layer

Transport Layer Java Virtual Machine Client Object

Remote Reference Layer

Transport Layer Java Virtual Machine Stub Remote Object Skeleton

slide-13
SLIDE 13

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Architecture in the OSI model RMI Architecture in the OSI model

User Application Stub Skeleton Remote Reference Layer TCP IP Hardware Interface Network Application Layer Presentation Layer Session Layer Transport Layer Network Layer Data-link layer Physical Layer

slide-14
SLIDE 14

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

TCP

Remote Reference Layer

Transport Layer

Java Virtual Machine

Client Object Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

Remote Objects Remote Objects

  • Remote Objects

Remote Objects

  • Live on server

Live on server

  • Accessed as if they were local

Accessed as if they were local

slide-15
SLIDE 15

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Stubs and Skeletons Stubs and Skeletons

TCP

Remote Reference Layer

Transport Layer

Java Virtual Machine

Client Object Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

  • Stub

Stub

  • lives on client

lives on client

  • pretends to be remote object

pretends to be remote object -

  • a proxy for the remote object

a proxy for the remote object

  • Skeleton

Skeleton

  • lives on server

lives on server

  • receives requests from stub, talks to the remote object and

receives requests from stub, talks to the remote object and delivers response to stub delivers 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”

slide-16
SLIDE 16

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Stubs and Skeletons Stubs and Skeletons

TCP

Remote Reference Layer

Transport Layer

Java Virtual Machine

Client Object Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

  • Stub

Stub – – responsibilities responsibilities

  • Initiate 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

slide-17
SLIDE 17

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Stubs and Skeletons Stubs and Skeletons

TCP

Remote Reference Layer

Transport Layer

Java Virtual Machine

Client Object Remote Reference Layer

Transport Layer

Java Virtual Machine

Stub

Remote Object

Skeleton

Client Server

  • Skeleton

Skeleton – – responsibilities responsibilities

  • Unmarshals 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 – – definition definition

  • The 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

slide-18
SLIDE 18

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Interfaces and Stubs Remote Interfaces and Stubs

Remote Interface Stub Remote Object (Server) Client Skeleton implements implements

slide-19
SLIDE 19

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Interfaces Remote Interfaces

  • Declare exposed methods

Declare exposed methods – – the methods the methods that can be called from remote locations that can be called from remote locations

  • Extend

Extend java.rmi.Remote java.rmi.Remote

  • The remote object implements this

The remote object implements this interface interface

  • Act like a proxy for the remote object

Act like a proxy for the remote object

slide-20
SLIDE 20

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Reference Layer Remote Reference Layer

TCP

Remote Reference Layer

Transport Layer

Java Virtual Machine

Client 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/IP

Communicates via TCP/IP

slide-21
SLIDE 21

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Object Registries Object Registries

  • Name and look up remote objects

Name and look up remote objects

  • Remote objects register by name

Remote objects register by name

  • Clients obtain a remote reference to the

Clients obtain a remote reference to the remote object remote object

  • A registry is a

A registry is a running process running process on the

  • n the

same host as the RMI server same host as the RMI server

slide-22
SLIDE 22

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

HTTP Tunneling HTTP Tunneling

  • Cool: if it can’t make the connection

Cool: if it can’t make the connection normally, it will tunnel through port 80 normally, it will tunnel through port 80

  • Allows clients behind firewall to make

Allows clients behind firewall to make remote calls to server remote calls to server

  • Note: does not work server

Note: does not work server -

  • > client

> client

slide-23
SLIDE 23

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI System Architecture RMI System Architecture

Client Virtual Machine Client Server Virtual Machine Stub Remote Object Skeleton Registry Virtual Machine “Fred” Server

Server Client Registry

slide-24
SLIDE 24

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Flow RMI Flow

Client Virtual Machine Client Server Virtual Machine Stub Remote Object Skeleton Registry Virtual Machine “Fred” Server

1 2

  • 1. Server Creates Remote Object
  • 2. Server Registers Remote Object
slide-25
SLIDE 25

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Flow RMI Flow

Client Virtual Machine Client Server Virtual Machine Stub Remote Object Skeleton Registry Virtual Machine “Fred” Server

4

  • 3. Client requests object from Registry
  • 4. Registry returns remote reference

3

slide-26
SLIDE 26

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Flow RMI Flow

Client Virtual Machine Client Server Virtual Machine Stub Remote Object Skeleton Registry Virtual Machine “Fred” Server

6

  • 5. Client invokes stub method
  • 6. Stub talks to skeleton
  • 7. Skeleton invokes remote object

method

5 7

slide-27
SLIDE 27

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Part II: RMI Usage Part II: RMI Usage

slide-28
SLIDE 28

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Creating Remote Objects Creating Remote Objects

  • Define a Remote Interface

Define a Remote Interface

  • extends java.rmi.Remote

extends java.rmi.Remote

  • Define a class that implements the

Define a class that implements the Remote Interface Remote Interface

  • extends java.rmi.RemoteObject

extends java.rmi.RemoteObject

  • r java.rmi.UnicastRemoteObject
  • r java.rmi.UnicastRemoteObject
slide-29
SLIDE 29

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Interface Example Remote Interface Example

import java.rmi.*; import java.rmi.*; public interface Adder public interface Adder extends Remote extends Remote { { public int add(int x, int y) public int add(int x, int y) throws RemoteException; throws RemoteException; } }

slide-30
SLIDE 30

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Class Example Remote Class Example

import java.rmi.*; import java.rmi.*; import java.rmi.server.*; import java.rmi.server.*; public class AdderImpl public class AdderImpl extends UnicastRemoteObject extends UnicastRemoteObject implements Adder implements Adder { { public AdderImpl() throws RemoteException public AdderImpl() throws RemoteException { { } } public int add(int x, int y) public int add(int x, int y) throws RemoteException throws RemoteException { { return x + y; return x + y; } } } }

slide-31
SLIDE 31

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Inheritance Diagram in Java Inheritance Diagram in Java

Object Unicast RemoteObject RemoteObject RemoteServer RemoteStub Remote

slide-32
SLIDE 32

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Compiling Remote Classes Compiling Remote Classes

  • Compile the Java class

Compile the Java class

  • javac

javac

  • reads .java file

reads .java file

  • produces .class file

produces .class file

  • Compile the Stub and Skeleton

Compile the Stub and Skeleton

  • rmic

rmic

  • reads .class file

reads .class file

  • produces _Skel.class and _Stub.class

produces _Skel.class and _Stub.class

slide-33
SLIDE 33

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and 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)

javac rmic

AdderImpl_Skel.class (skeleton classfile) AdderImpl_Stub.class (stub classfile)

slide-34
SLIDE 34

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Registering Remote Classes Registering Remote Classes

  • start the registry

start the registry

  • running process

running process

  • Unix:

Unix:

rmiregistry & rmiregistry &

  • Windows:

Windows:

start /m rmiregistry start /m rmiregistry

slide-35
SLIDE 35

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Registry CLASSPATH Registry CLASSPATH

  • Registry 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 file directory containing the stub file

  • An easy way to check CLASSPATH is to use the

An easy way to check CLASSPATH is to use the javap javap command, supplying command, supplying a fully package qualified class name. It uses the current CLASSP a 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)

slide-36
SLIDE 36

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Create the server Create the server

  • Creates a new instance of the remote

Creates a new instance of the remote

  • bject
  • bject
  • Registers it in the registry with a name

Registers it in the registry with a name

  • That’s it

That’s it

slide-37
SLIDE 37

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Server Example RMI Server Example

try { try { AdderImpl adder = AdderImpl adder = new new AdderImpl(); AdderImpl(); Naming. Naming.rebind rebind("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(); } }

slide-38
SLIDE 38

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Launch the Server Launch the Server

% java AdderServer & % java AdderServer & Adder bound Adder bound

slide-39
SLIDE 39

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Server Logging Server Logging

  • invoke from command line

invoke from command line

java java

  • Djava.rmi.server.logCalls=true

Djava.rmi.server.logCalls=true YourServerImpl YourServerImpl

  • r enable inside program
  • r enable inside program

RemoteServer.setLog(System.err); RemoteServer.setLog(System.err);

slide-40
SLIDE 40

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Creating an RMI Client Creating an RMI Client

  • Install a Security Manager

Install a Security Manager

  • to protect from malicious stubs

to protect from malicious stubs

  • Find a registry

Find a registry

  • use java.rmi.Naming

use java.rmi.Naming

  • Lookup the name, returns a reference

Lookup the name, returns a reference

  • Cast the reference to the appropriate

Cast the reference to the appropriate Remote Interface Remote Interface

  • Just use it!

Just use it!

slide-41
SLIDE 41

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI URLs RMI URLs

rmi:// rmi://host host[: [:port port]/ ]/name name

  • default port is 1099

default port is 1099

  • Specifies hostname of

Specifies hostname of registry registry

  • can also use relative URLs

can also use relative URLs

  • name only

name only

  • assumes registry is on local host

assumes registry is on local host

slide-42
SLIDE 42

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Client Example RMI Client Example

System.setSecurityManager( System.setSecurityManager( new RMISecurityManager()); new RMISecurityManager()); Adder a = (Adder) Adder a = (Adder) Naming. Naming.lookup lookup("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);

slide-43
SLIDE 43

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Remote Interfaces vs. Remote Remote Interfaces vs. Remote Classes Classes

  • Remember that the reference is to an

Remember that the reference is to an interface interface

  • You must make references, arrays, etc. out of

You must make references, arrays, etc. out of the the interface interface type, not the implementation type type, not the implementation type

  • You can’t cast the remote reference to a normal

You can’t cast the remote reference to a normal reference reference

  • So name your Remote Objects with “

So name your Remote Objects with “Impl Impl” (so ” (so you don’t get confused) you don’t get confused)

slide-44
SLIDE 44

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Parameter Passing Parameter Passing

  • All parameters are passed by value

All parameters are passed by value

  • Primitive types

Primitive types

  • passed by value

passed by value

  • Objects

Objects

  • passed by value

passed by value

  • use Java Object Serialization

use Java Object Serialization

slide-45
SLIDE 45

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Object Serialization Object Serialization

  • saves the state (data) of a particular

saves the state (data) of a particular instance of an object instance of an object

  • serialize

serialize -

  • to save

to save

  • unserialize

unserialize -

  • to load

to load

slide-46
SLIDE 46

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Java Serialization Java Serialization

  • writes object as a sequence of bytes

writes object as a sequence of bytes

  • writes it to a Stream

writes it to a Stream

  • recreates it on the other end

recreates it on the other end

  • creates a brand new object with the old

creates a brand new object with the old data data

slide-47
SLIDE 47

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

java.io.Serializable java.io.Serializable

  • Objects that implement the

Objects that implement the java.io.Serializable interface are marked as java.io.Serializable interface are marked as serializable serializable

  • Also subclasses

Also subclasses

  • empty interface

empty interface -

  • just a marker

just a marker – – no need no need to implement any special methods to implement any special methods

slide-48
SLIDE 48

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Not All Objects Are Serializable Not All Objects Are Serializable

  • Any object that doesn’t implement Serializable

Any object that doesn’t implement Serializable

  • Any object that would pose a security risk

Any object that would pose a security risk

  • e.g. FileInputStream

e.g. FileInputStream

  • Any object whose value depends on VM

Any object whose value depends on VM-

  • specific information

specific information

  • e.g. Thread

e.g. Thread

  • Any object that

Any object that contains contains a a (non

(non-

  • static, non

static, non-

  • transient)

transient)

unserializable object unserializable object (recursively)

(recursively)

slide-49
SLIDE 49

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

NotSerializableException NotSerializableException

  • thrown if you try to serialize or unserialize

thrown if you try to serialize or unserialize an unserializable object an unserializable object

  • maybe you subclassed a Serializable

maybe you subclassed a Serializable

  • bject and added some unserializable
  • bject and added some unserializable

members members

slide-50
SLIDE 50

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Incompatible Changes Incompatible Changes

  • If class has members added or removed, it

If class has members added or removed, it becomes becomes incompatible incompatible

  • java.io.InvalidClassException thrown if you

java.io.InvalidClassException thrown if you try to deserialize an incompatible object try to deserialize an incompatible object stream stream

slide-51
SLIDE 51

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Serial Version Serial Version

  • If the changes were actually compatible

If the changes were actually compatible

  • find out the Serial Version UID of the

find out the Serial Version UID of the original

  • riginal

class class

  • use the

use the serialver

serialver utility

utility

  • add a member variable to the

add a member variable to the changed changed class class

protected static final long serialVersionUID = protected static final long serialVersionUID = -

  • 2215190743590612933L;

2215190743590612933L;

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

now it’s marked as compatible with the old class

slide-52
SLIDE 52

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Using readObject Using readObject

  • if you need to force an object to be

if you need to force an object to be compatible compatible

  • implement readObject() method to make

implement readObject() method to make compatible changes compatible changes

private void readObject(ObjectInputStream private void readObject(ObjectInputStream stream) throws java.io.IOException stream) throws java.io.IOException { { defaultReadObject(stream); defaultReadObject(stream); // do compatible stuff // do compatible stuff } }

slide-53
SLIDE 53

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Callbacks Callbacks

  • 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 server and the client is now the server

  • The client registers a notification method

The client registers a notification method with the server with the server

  • Server invokes the method (“calls back”)

Server invokes the method (“calls back”) to notify the client to notify the client

  • Registry is out of the loop

Registry is out of the loop

slide-54
SLIDE 54

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Security RMI Security

  • Server is untrusted

Server is untrusted

  • Stubs could be malicious

Stubs could be malicious

  • rmic is OK, but someone could custom

rmic is OK, but someone could custom-

  • code an evil stub: it’s just a .class file

code an evil stub: it’s just a .class file

slide-55
SLIDE 55

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Security Managers RMI Security Managers

  • AppletSecurityManager

AppletSecurityManager

  • stub can only do what an applet can do

stub can only do what an applet can do

  • RMISecurityManager

RMISecurityManager

  • disables all functions except class definition and

disables all functions except class definition and access access

  • A 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.

  • None

None

  • Stub loading disabled

Stub loading disabled

  • Stubs still work if they are in local classpath

Stubs still work if they are in local classpath

slide-56
SLIDE 56

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Codebase Property Codebase Property

  • Stub classpaths can be confusing

Stub classpaths can be confusing

  • 3 VMs, each with its own classpath

3 VMs, each with its own classpath

  • Server vs. Registry vs. Client

Server vs. Registry vs. Client

  • The RMI class loader always loads stubs from the

The RMI class loader always loads stubs from the CLASSPATH first CLASSPATH first

  • Next, it tries downloading classes from a web

Next, it tries downloading classes from a web server server

  • (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 server server

slide-57
SLIDE 57

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Stub File Configuration Stub 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 classpath

NEVER have stub class files in ANY classpath

  • make the stub files accessible via a web server

make the stub files accessible via a web server

  • set 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 URL server's URL

  • The stubs will be downloaded from your HTTP

The stubs will be downloaded from your HTTP server on demand server on demand

  • (Thanks to Kevin.Conner@UK.Sun.COM for advice)

(Thanks to Kevin.Conner@UK.Sun.COM for advice)

slide-58
SLIDE 58

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Codebase and Thread Safety Codebase 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

  • wn stub codebase
  • wn stub codebase
  • One 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 server server

  • In practice, won’t happen very often

In practice, won’t happen very often

  • More likely to happen in servlets

More likely to happen in servlets

slide-59
SLIDE 59

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Limitations of RMI Limitations of RMI

  • Java

Java-

  • only
  • nly
  • but you can use JNI on the server

but you can use JNI on the server

  • Uses TCP, not UDP

Uses TCP, not UDP

  • At least two sockets per connection

At least two sockets per connection

  • Untested for huge loads

Untested for huge loads

slide-60
SLIDE 60

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI vs. COM RMI vs. COM

  • Very similar

Very similar

  • remote interfaces ~ type libraries

remote interfaces ~ type libraries

  • COM is Win32

COM is Win32-

  • only (for now)
  • nly (for now)
slide-61
SLIDE 61

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Sun vs. Microsoft Sun vs. Microsoft

  • RMI is not shipped as part of Microsoft’s

RMI is not shipped as part of Microsoft’s products products

  • RMI will still work in applications

RMI will still work in applications

  • include java.rmi.* class files in your classpath

include java.rmi.* class files in your classpath

  • download rmi.zip from ftp.microsoft.com

download 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 codebase codebase

  • extra download time

extra download time

slide-62
SLIDE 62

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

RMI Chat Server Objects RMI Chat Server Objects

Message

interface

MessageReceiver

  • receiveMessage(Message)

ChatClient

interface

ChatServer

  • login(MessageReceiver)
  • sendMessage(Message)

ChatServerImpl Dispatcher MessageQueue implements remote reference local reference

slide-63
SLIDE 63

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Summary Summary

  • RMI is a very clean API

RMI is a very clean API

  • Easy way to write distributed programs

Easy way to write distributed programs

  • Wire protocol may need improvement for

Wire protocol may need improvement for large large-

  • scale problems

scale problems

slide-64
SLIDE 64

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

Where to get more information Where to get more information

  • Core Java Volume II by Cay S Horstmann and

Core Java Volume II by Cay S Horstmann and Gray Cornell Gray Cornell

  • Harold,

Harold, Java Network Programming Java Network Programming (O’Reilly) (O’Reilly)

  • rmi

rmi-

  • users mailing list

users mailing list (rmi

(rmi-

  • users@javasoft.com)

users@javasoft.com)

  • 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)

slide-65
SLIDE 65

11/18/2003 11/18/2003 ECE 451:Introduction to Parallel ECE 451:Introduction to Parallel and Distributed Programming and Distributed Programming

My sincere thanks My 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 Presentation Presentation