Top Banner
Java Remote Method Invocation (RMI) )
51

Java Remote Method Invocation (RMI) ). Distributed Systems a collection of independent computers that appears to its users as a single coherent system.

Jan 02, 2016

Download

Documents

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) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Java Remote MethodInvocation (RMI)

)

Page 2: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Distributed Systems

� a collection of independent computers thatappears to its users as a single coherentsystem

Page 3: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Models of Distribution

� Message passing� Distributed objects

� Event-based architectures� Space-based paradigms

Page 4: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Distributed Object Model

� Views a distributed system as a series ofinteracting objects

� Based on some underlying messagepassing protocol invisible to theprogrammer

� Three main technologies: RMI, CORBAand DCOM

Page 5: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Distributed Object Computing

� Enable any object in the local system to directly interactwith an object on a remote host

� Goals:� Let any object reside anywhere in the network, and allow an

application to interact with these objects in the same way asthey do with a local object.� Provide the ability to construct an object on one host and

transmit it to another host.� Enable an agent on one host to create an object on another

host.

Page 6: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

What Is RMI?

� A mechanism that allows the invocation ofa method that exists in another addressspace

� Java-to-Java only� Client-Server Protocol� High-level API

� Transparent� Lightweight

Page 7: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Related Technologies

� RPC (“Remote Procedure Calls”)� Developed by Sun� Platform-specific

� CORBA (“Common Object Request Broker Architecture”)� Developed by OMG

� Access to non-Java objects (as well as Java)� DCOM (“Distributed Common Object Model”)

� Developed by Microsoft� Access to Win32 objects

� LDAP (“Lightweight Directory Access Protocol”)� Finding resources on a network

Page 8: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI

� Client – the process that invokes a method on aremote object

� Server – the process that owns the remoteobject

� Object Registry – a name server that relatesobjects with names� Objects are registered with the Object Registry, undera unique name.

� The Object Registry is used to obtain access toremote objects, using their names.

Page 9: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Objects (Diagram)Java Virtual Machine

ClientObject

Client

TCP

Java Virtual Machine

RemoteObject

Server

Page 10: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI LayersJava Virtual Machine

ClientObject

Stub

Remote Reference Layer

Transport Layer

11/18/2003

TCP

Java Virtual Machine

RemoteObject

Skeleton

Remote Reference Layer

Transport Layer

Page 11: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Architecture in the OSI modelApplication Layer

Presentation Layer

Session Layer

Transport Layer

Network Layer

Data-link layerPhysical Layer

User Application

Stub Skeleton

Remote Reference Layer

TCP

IP

Hardware Interface

Network

Page 12: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Objects

� Remote Objects� Live on server

Java Virtual MachineClientObject

Stub

Remote Reference Layer

Transport Layer

Client

Java Virtual MachineRemoteObject

Skeleton

Remote Reference LayerTCPTransport Layer

Server

� Accessed as if they were local

Page 13: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Stubs and Skeletons

� Stub� lives on client

Java Virtual MachineClientObject

Stub

Remote Reference Layer

Transport Layer

Client

Java Virtual MachineRemoteObject

Skeleton

Remote Reference LayerTCPTransport Layer

Server

� pretends to be remote object - a proxy for the remote object

� Skeleton� lives on server

� receives requests from stub, talks to the remote object anddelivers response to stub

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

Page 14: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Stubs and Skeletons

� Stub – responsibilities� Initiate remote calls

Java Virtual MachineClientObject

Stub

Remote Reference Layer

Transport Layer

Client

Java Virtual MachineRemoteObject

Skeleton

Remote Reference LayerTCPTransport Layer

Server

� Marshals arguments to be sent

� Informs the remote reference layer that a callshould be invoked on the server

� Unmarshals a return value (or exception)

� Informs the remote reference layer that thecall is complete

Page 15: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Stubs and Skeletons

� Skeleton – responsibilities

Java Virtual MachineClientObject

Stub

Remote Reference Layer

Transport Layer

Client

Java Virtual MachineRemoteObject

Skeleton

Remote Reference LayerTCPTransport Layer

Server

� Unmarshals incoming arguments� Calls the actual remote object implementation� Marshals return values for transport to theclient

� Marshaling – definition� The process of converting native

programming language data types to a formatsuitable for transmission across a network

Page 16: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Interfaces and Stubs

Remote Interface

implements implements

Remote ObjectClient Stub Skeleton(Server)

11/18/2003

Page 17: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Interfaces

� Declare exposed methods – the methodsthat can be called from remote locations

� Extend java.rmi.Remote� The remote object implements thisinterface

� Act like a proxy for the remote object

Page 18: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Reference Layer

Java Virtual MachineClientObject

Stub

Remote Reference Layer

Transport Layer

Client

Java Virtual MachineRemoteObject

Skeleton

Remote Reference LayerTCPTransport Layer

Server

� Sets up connections to remote addressspaces

� Manages connections� Listens for incoming calls� Communicates via TCP/IP

Page 19: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Object Registries

� Name and look up remote objects� Remote objects register by name

� Clients obtain a remote reference to theremote object� A registry is a running process on thesame host as the RMI server

Page 20: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

HTTP Tunneling

� Cool: if it can’t make the connectionnormally, it will tunnel through port 80� Allows clients behind firewall to makeremote calls to server

� Note: does not work server -> client

Page 21: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI System ArchitectureClient Virtual Machine

Client

Stub

Client

“Fred”

Server Virtual Machine

RemoteObject

SkeletonServer

Server

Registry

Registry Virtual Machine

Page 22: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Flow1. Server Creates Remote Object

2. Server Registers Remote ObjectClient

Stub

“Fred”

Server Virtual Machine

RemoteObject

1

SkeletonServer

2

Registry Virtual Machine

Page 23: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI FlowClient Virtual Machine

Client

Stub

3 4

Server Virtual Machine

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

SkeletonServer

“Fred”

Registry Virtual Machine

Page 24: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI FlowClient Virtual Machine

Client

5

Stub6

Server Virtual Machine

RemoteObject

7

SkeletonServer

5. Client invokes stub method6. Stub talks to skeleton

7. Skeleton invokes remote objectmethod

Registry Virtual Machine

Page 25: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Part II: RMI Usage

11/18/2003 and Distributed Programming

Page 26: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Creating Remote Objects

� Define a Remote Interface� extends java.rmi.Remote

� Define a class that implements theRemote Interface

� extends java.rmi.RemoteObject� or java.rmi.UnicastRemoteObject

Page 27: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Interface Example

import java.rmi.*;

public interface Adder

extends Remote

{

public int add(int x, int y)

throws RemoteException;

}

ECE 451:Introduction to Parallel11/18/2003

Page 28: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Class Exampleimport java.rmi.*;

import java.rmi.server.*;public class AdderImpl extends UnicastRemoteObject

implements Adder{

public AdderImpl() throws RemoteException{

}public int add(int x, int y)

throws RemoteException{

return x + y;}

}

Page 29: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Inheritance Diagram in JavaObject Remote

RemoteObject

RemoteStub RemoteServer

UnicastRemoteObject

Page 30: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Compiling Remote Classes

� Compile the Java class� javac

� reads .java file� produces .class file

� Compile the Stub and Skeleton� rmic

� reads .class file� produces _Skel.class and _Stub.class

Page 31: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Compiling Remote Classes(Diagram)

Adder.java(interface)

javacAdder.class(interface classfile)

javac

AdderImpl_Skel.class(skeleton classfile)

AdderImpl.java(remote class)

AdderImpl.class(classfile) rmic

AdderImpl_Stub.class(stub classfile)

ECE 451:Introduction to Parallel11/18/2003 and Distributed Programming

Page 32: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Registering Remote Classes

� start the registry� running

process

� Unix:rmiregistry &

� Windows:start /m rmiregistry

Page 33: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Registry CLASSPATH� Registry VM needs to be able to find stub file(s)� You must set the CLASSPATH to include thedirectory containing the stub file

� An easy way to check CLASSPATH is to use the javap command, supplyinga fully package qualified class name. It uses the current CLASSPATH to find

and print the interface to a class.

� Or, your server needs to specify thejava.rmi.server.codebase System property (morelater)

Page 34: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Create the server

� Creates a new instance of the remoteobject

� Registers it in the registry with a name� That’s it

ECE 451:Introduction to Parallel11/18/2003 and Distributed Programming

Page 35: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Server Example

try {

AdderImpl adder = new AdderImpl();

Naming. rebind ("adder", adder);

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

}catch (RemoteException re) {

re.printStackTrace();}

catch (MalformedURLException me) {me.printStackTrace();

}

Page 36: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Launch the Server

% java AdderServer &

Adder bound

ECE 451:Introduction to Parallel11/18/2003 and Distributed Programming

Page 37: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Server Logging

� invoke from command linejava

-Djava.rmi.server.logCalls=trueYourServerImpl

� or enable inside programRemoteServer.setLog(System.err);

Page 38: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Creating an RMI Client

� Install a Security Manager� to protect from malicious

stubs� Find a registry� use java.rmi.Naming

� Lookup the name, returns a reference� Cast the reference to the appropriateRemote Interface

� Just use it!

Page 39: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI URLs

rmi://host[:port]/

name

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

� name only� assumes registry is on local host

Page 40: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Client Example

System.setSecurityManager(

new RMISecurityManager());

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

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

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

Page 41: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Remote Interfaces vs. RemoteClasses

� Remember that the reference is to an interface� You must make references, arrays, etc. out ofthe interface type, not the implementation type� You can’t cast the remote reference to a normalreference

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

Page 42: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Parameter Passing� All parameters are passed by value

� Primitive types� passed by

value

� Objects� passed by value

� use Java Object Serialization

Page 43: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Object Serialization

� saves the state (data) of a particularinstance of an object

� serialize - to save� unserialize - to load

Page 44: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Java Serialization

� writes object as a sequence of bytes� writes it to a Stream

� recreates it on the other end� creates a brand new object with the olddata

Page 45: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

java.io.Serializable

� Objects that implement thejava.io.Serializable interface are marked asserializable

� Also subclasses� empty interface - just a marker – no needto implement any special methods

Page 46: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Security

� Server is untrusted� Stubs could be malicious

� rmic is OK, but someone could custom-code an evil stub: it’s just a .class file

Page 47: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Security Managers� AppletSecurityManager

� stub can only do what an applet can

do

� RMISecurityManager� disables all functions except class definition andaccess

� A downloaded class is allowed to make a connectionif the connection was initiated via the RMItransport.

� None� Stub loading disabled

� Stubs still work if they are in local classpath

Page 48: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Limitations of RMI

� Java-only� but you can use JNI on the

server

� Uses TCP, not UDP� At least two sockets per connection� Untested for huge loads

Page 49: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Sun vs. Microsoft

� RMI is not shipped as part of Microsoft’sproducts

� RMI will still work in applications� include java.rmi.* class files in your classpath� download rmi.zip from ftp.microsoft.com

� RMI will work in applets� include java.rmi.* class files (or rmi.zip) in yourcodebase

� extra download time

Page 50: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

RMI Chat Server ObjectsMessage

interface

MessageReceiver- receiveMessage(Message)

ChatClient

implements

interface

ChatServer- login(MessageReceiver)- sendMessage(Message)

ChatServerImpl

Dispatcherremote reference MessageQueuelocal reference

Page 51: Java Remote Method Invocation (RMI) ). Distributed Systems  a collection of independent computers that appears to its users as a single coherent system.

Summary

� RMI is a very clean API

� Easy way to write distributed programs� Wire protocol may need improvement forlarge-scale problems