51
Java Remote Method Invocation (RMI) )

Java Remote Method Invocation (RMI)

  • Upload
    fairly

  • View
    65

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Java Remote Method Invocation (RMI)

Java Remote MethodInvocation (RMI)

)

Page 2: Java Remote Method Invocation (RMI)

Distributed Systems

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

Page 3: Java Remote Method Invocation (RMI)

Models of Distribution

� Message passing� Distributed objects

� Event-based architectures� Space-based paradigms

Page 4: Java Remote Method Invocation (RMI)

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 Object Computing� Enable any object in the local system to directly interact

with an object on a remote host� Goals:

� Let any object reside anywhere in the network, and allow anapplication 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)

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)

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)

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)

Remote Objects (Diagram)Java Virtual Machine

ClientObject

Client

TCP

Java Virtual Machine

RemoteObject

Server

Page 10: Java Remote Method Invocation (RMI)

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)

RMI Architecture in the OSI modelApplication Layer

Presentation Layer

Session Layer

Transport Layer

Network LayerData-link

layerPhysical Layer

User Application

Stub Skeleton

Remote Reference Layer

TCP

IP

Hardware Interface

Network

Page 12: Java Remote Method Invocation (RMI)

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)

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)

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)

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)

Remote Interfaces and StubsRemote Interface

implements implements

Remote ObjectClient Stub Skeleton(Server)

11/18/2003

Page 17: Java Remote Method Invocation (RMI)

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)

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)

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)

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)

RMI System ArchitectureClient Virtual Machine

Client

Stub

Client

“Fred”

Server Virtual Machine

RemoteObject

SkeletonServer

Server

RegistryRegistry Virtual Machine

Page 22: Java Remote Method Invocation (RMI)

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)

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)

RMI FlowClient Virtual Machine

Client

5

Stub6

Server Virtual Machine

RemoteObject7

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)

Part II: RMI Usage

11/18/2003 and Distributed Programming

Page 26: Java Remote Method Invocation (RMI)

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)

Remote Interface Exampleimport java.rmi.*;

public interface Adderextends Remote

{public int add(int x, int y)throws RemoteException;

}

ECE 451:Introduction to Parallel11/18/2003

Page 28: Java Remote Method Invocation (RMI)

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)

Inheritance Diagram in JavaObject Remote

RemoteObject

RemoteStub RemoteServer

UnicastRemoteObject

Page 30: Java Remote Method Invocation (RMI)

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)

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)

Registering Remote Classes� start the registry

� running process� Unix:rmiregistry &

� Windows:start /m rmiregistry

Page 33: Java Remote Method Invocation (RMI)

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)

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)

RMI Server Exampletry {

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)

Launch the Server% java AdderServer &Adder bound

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

Page 37: Java Remote Method Invocation (RMI)

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)

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)

RMI URLsrmi://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)

RMI Client ExampleSystem.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)

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)

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)

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)

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)

java.io.Serializable� Objects that implement the

java.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)

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)

RMI Security Managers� AppletSecurityManager

� stub can only do what an applet can do� RMISecurityManager� disables all functions except class definition and

access� 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)

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)

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)

RMI Chat Server ObjectsMessage

interfaceMessageReceiver

- receiveMessage(Message)

ChatClient

implements

interfaceChatServer

- login(MessageReceiver)- sendMessage(Message)

ChatServerImpl

Dispatcherremote reference MessageQueuelocal reference

Page 51: Java Remote Method Invocation (RMI)

Summary� RMI is a very clean API

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