Lecture 18 Inside Java RMI

Similar documents
Lecture 17 Java Remote Method Invoca/on

JAVA RMI. Remote Method Invocation

Generic architecture

Object Interaction. Object Interaction. Introduction. Object Interaction vs. RPCs (2)

Remote Method Invocation in Java

CC755: Distributed and Parallel Systems

Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma. Distributed and Agent Systems RMI

Component-Based Software Engineering

5.4. Events and notifications

JAVA RMI Java, summer semester

Architecture of So-ware Systems RMI and Distributed Components. Mar<n Rehák

SUMMARY INTRODUCTION REMOTE METHOD INVOCATION

Remote Method Invocation

RMI Example RMI. CmpE 473 Internet Programming RMI

Questions and Answers. A. RMI allows us to invoke a method of java object that executes on another machine.

Remote Method Invocation

RMI. (Remote Method Invocation)

Serialisa(on, Callbacks, Remote Object Ac(va(on. CS3524 Distributed Systems Lecture 03

RMI (Remote Method Invocation) Over the year, there have been 3 different approaches to application development:

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

IBD Intergiciels et Bases de Données

Distributed object component middleware I - Java RMI

Last Class: Network Overview. Today: Distributed Systems

Contents. Java RMI. Java RMI. Java RMI system elements. Example application processes/machines Client machine Process/Application A

Distributed object component middleware I - Java RMI

5 Distributed Objects: The Java Approach

Remote Objects and RMI

Distributed Systems. 02r. Java RMI Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

Distributed Programming in Java. Distribution (2)

RMI Case Study. A Typical RMI Application

presentation DAD Distributed Applications Development Cristian Toma

Distributed Systems. 5. Remote Method Invocation

Remote Procedure Call

Written by: Dave Matuszek

Remote Method Invocation R.M.I.

CS 5523 Operating Systems: Remote Objects and RMI

REMOTE METHOD INVOCATION INTRODUCTION TO RMI, A JAVA API FOR RPC-STYLE INVOCATION OF REMOTE OBJECT METHODS

Info 408 Distributed Applications programming 2 nd semester of Credits: 5 Lecturer: Antoun Yaacoub Ph.D.

RMI. Remote Method Invocation. 16-Dec-16

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

Lab 2 : Java RMI. request sayhello() Hello interface remote object. local object. response "Hello world"

Remote Method Invocation

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

Distributed Objects SPL/ SPL 201 / 0 1

How are classes loaded into the Java Virtual Machine (JVM)? from the local file system (CLASSPATH). by an instance of ClassLoader

A Typical RMI Application. Case Study

RPC flow. 4.3 Remote procedure calls IDL. RPC components. Procedure. Program. sum (j,k) int j,k; {return j+k;} i = sum (3,7); Local procedure call

Dynamic code downloading using Java TM (Using the java.rmi.server.codebase Property)

Introduction & RMI Basics. CS3524 Distributed Systems Lecture 01

COMP 6231: Distributed System Design

Distributed Objects. Chapter Distributing Objects Overview

Distributed Software Systems

Distributed Systems. Distributed Object Systems 2 Java RMI. Java RMI. Example. Applet continued. Applet. slides2.pdf Sep 9,

Grid Computing. Java Remote Method Invocation (RMI) RMI Application. Grid Computing Fall 2006 Paul A. Farrell 9/5/2006

A Typical RMI Application

Distributed Computing

Distributed Applications Programming. Lab 4

Verteilte Systeme (Distributed Systems)

JAC444 - Lecture 11. Remote Method Invocation Segment 2 - Develop RMI Application. Jordan Anastasiade Java Programming Language Course

Chapter 15: Distributed Communication. Sockets Remote Procedure Calls (RPCs) Remote Method Invocation (RMI) CORBA Object Registration

THE UNIVERSITY OF WESTERN AUSTRALIA SAMPLE EXAM QUESTIONS 2007 WITH SOLUTIONS SCHOOL OF COMPUTER SCIENCE CITS3213 CONCURRENT PROGRAMMING (PART II)

The UNIVERSITY of EDINBURGH. SCHOOL of INFORMATICS. CS4/MSc. Distributed Systems. Björn Franke. Room 2414

Concurrent Programming (RIO) Lesson 9. Ch 8 [BenA 06] Messages Channels Rendezvous RPC and RMI. X=f(..); send X to B. OS kernel.

COMP 6231 Distributed Systems Design. Tutorial 2 by Alexandre Hudon January 21 st, 2013

Outline. EEC-681/781 Distributed Computing Systems. The OSI Network Architecture. Inter-Process Communications (IPC) Lecture 4

DISTRIBUTED OBJECTS AND REMOTE INVOCATION

Programming with RMI Reminder

Message Passing vs. Distributed Objects. 5/15/2009 Distributed Computing, M. L. Liu 1

Developing RMI Based Server (ChatServer)

Distributed Systems. 6. Remote Method Invocation. Werner Nutt

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

Concurrency Control in Distributed Environment

Outlook. Process Concept Process Scheduling Operations on Processes. IPC Examples

Lecture 06: Distributed Object

Distributed Systems COMP 212. Lecture 10 Othon Michail

CSci Introduction to Distributed Systems. Communication: RPC In Practice

Course Snapshot. The Next Few Classes

THE RMI PROXY USER GUIDE

Lecture 5: Object Interaction: RMI and RPC

Java Remote Method Invocation Specification

Java Remote Method Invocation Specification

What is the problem?

Are Sockets a Good Programming Paradigm? EDA095 Remote Method Invocation. The Remote Procedure Call Model. The Remote Procedure Call Architecture

Course Snapshot. The Next Few Classes. Parallel versus Distributed Systems. Distributed Systems. We have covered all the fundamental OS components:

IJESRT. http: //

Remote Invocation Vladimir Vlassov and Johan Montelius

Last Class: RPCs. Today:

Concurrent Object-Oriented Programming

Reflection/RMI 4/28/2009

Distributed Applications Programming. Lab 7

Chapter 5 Distributed Objects and Remote Invocation

Server and Threads. Shan Hung Wu & DataLab CS, NTHU

EDA095 Remote Method Invocation

EDA095 Remote Method Invocation

Distributed Information Systems

Remote Method Invocation. Benoît Garbinato

Chapter 4 Remote Procedure Calls and Distributed Transactions

Communication and Distributed Processing

Java WebStart, Applets & RMI

CS193k, Stanford Handout #12. Threads 4 / RMI

Transcription:

CMSC 433 Fall 2014 Sec/on 0101 Mike Hicks (slides due to Rance Cleaveland) Lecture 18 Inside Java RMI

Recall Java RMI applica/ons consist of three en//es Remote object servers Host remote objects Handle communica/ons coming into / going out from these objects Clients Issue calls to methods in remote objects Handle communica/ons going out to / coming in from these objects Object registries Maintain bindings between names, remote objects Have a bootstrapping func/on Provide ini/al access point for clients needing remote objects Once a client has stub for such an object, no need to consult registry! We ve seen how some of this works Marshaling / unmarshaling via serializa/on Registra/on of remote objects in registries Stubs

RMI in More Detail Remote- izing (aka expor&ng) an object Server for a remote object must set up infrastructure for unmarshaling arguments to remote object, marshaling results Server must also invoke method in remote object Registering a remote object Server must provide name, stub for remote object Name / object binding must be made available to poten/al clients Accessing a remote object Client must be able to access remote object Client must be able to marshal arguments to remote object method, unmarshal results

Expor/ng an Object Recall: exportable objects must come from a class that implements the Remote interface What does an exported object need? Incoming method- invoca/on requests need to be listened for Arguments need to be unmarshaled Methods in the actual object need to be invoked Results need to be marshaled, returned to calling side java.rmi.server.unicastremoteobject provides support for this A class of remote objects Constructor sets up infrastructure for listening for incoming method- invoca/ons, marshaling / unmarshaling, actual method invoca/on Class also includes static RemoteStub exportobject(remote obj) This method exports any object matching the Remote interface, returning a stub Stub matches same (sub)interface of Remote that original object does Stub is used on client side So: two ways to export an object Make a class that extends UnicastRemoteObject; objects created in such a class are automa/cally exported Make a class that implements Remote, then export using UnicastRemoteObject.exportObject

Behind the Scenes with exportobject() What does exportobject() do? A new connec/on (server socket) to a port on the host is created A thread is created to listen for connec/ons on the socket Infrastructure is also created to allocate other threads to handle incoming calls (this enables first thread to con/nue listening) A stub is created for use on the client side Contains informa/on about which socket the remote object listens on Implements argument mashaling, result unmarshaling A skeleton is created for use on the host side Handles argument umarshaling, result marshaling of results, actual call of method Note: mul/ple threads can access a remote object! If there are mul/ple method invoca/ons in progress, more than one thread on the host will be accessing the object You must ensure remote objects are thread- safe!

More on Thread Safety Synchronizing on a stubs to the same object, but on two different machines is dangerous Possible behavior: delegates synchroniza/on to the source object s monitor. Thus: safe. Actual behavior: synchronizes on the stub s own monitor. Thus: threads on different machines could get concurrent access. Note: Thread iden/ty is not preserved across calls Synchronizing on O in thread T1 at one host, Which makes a remote call another host, Which makes a remote call back to the first, And then tries to sync on O again will self- deadlock The thread that makes the second sync is not the same as the first!

Stubs and Skeletons Both are objects in classes that are (as of Java 5) created automa/cally from the class of the object being exported If the class of the remote object is Foo then the class for stubs is Foo_Stub and the class for skeletons is Foo_Skel Before Java 5 the programmer had to create these using a command rmic Both objects handle marshaling, unmarshaling Stub class also implements same Remote (sub)interface that original class does

Object Registra/on Purpose of registra/on is to make it easier for clients to use remote objects RMI uses object registries to handle this task Object registry is a separate thread from server Ojen, it even runs in a different JVM from the server, but it must run on the same host as server This last fact implies that there can be several different registries running in a given applica/on The registry binds names (strings) to stubs for remote objects Clients looking for remote objects must first locate a registry

Crea/ng Object Registries Approach 1: rmiregistry A command distributed with Java that can be run at the OS command line to start a registry Takes an op/onal argument: the port on which to listen for requests (default is 1099) The registry created in this fashion con/nues to run even if the server JVM terminates Such a registry can be used by several servers If a server terminates, any references to remote objects on the server in such a registry will con/nue to exist If a client tries to access such a nonexistent remote object, an excep/on is thrown Approach 2: LocateRegistry.createRegistry() LocateRegistry is a class in java.rmi.registry static Registry createregistry (int port) (From Java 7 documenta/on): Creates and exports a Registry instance on the local host that accepts requests on the specified port Exports here means makes the object remote What is returned is a stub for the remote Registry instance!

Other Useful Methods in java.rmi.registry.locateregistry LocateRegistry also includes methods for finding exis/ng registries Samples: public static Registry getregistry()throws RemoteException (From the Java 7 documenta/on) Returns a reference to the remote object Registry for the local host on the default registry port of 1099 public static Registry getregistry(int port) throws RemoteException Like above version, except that the given port is used public static Registry getregistry(string host, int port) throws RemoteException Live previous versions, except that given host name is used rather than local host

Useful Methods in java.rmi.registry.registry Registry is an interface that extends Remote Registry objects allow remote objects to be bound to / unbound from names, names to be looked up, etc. Useful methods (quoted from Java 7 documenta/on) void bind(string name, Remote obj) Binds a remote reference (i.e. stub) to the specified name in this registry String[] list() Returns an array of the names bound in this registry Remote lookup(string name) Returns the remote reference (i.e. stub) bound to the specified name in this registry void rebind(string name, Remote obj) Replaces the binding for the specified name in this registry with the supplied remote reference void unbind(string name) Removes the binding for the specified name in this registry

rmi.naming Registry objects allow names to be looked up, bound, etc. These are instance methods, so the registry object must first be retrieved, e.g. Registry registry = LocateRegistry.getRegistry(); registry.rebind(name, object); Remote registries ojen accessed via URLs and port numbers, e.g. Registry registry = LocateRegistry.getRegistry( www.cs.umd.edu,1099); The class Naming includes shortcuts for these registry manipula/ons The versions of bind / rebind / etc. assume the string argument is a URL including a host name, port and object name The appropriate registry is also obtained automa/cally So, instead of Registry registry = LocateRegistry.getRegistry( www.cs.umd.edu,1099); registry.rebind( foo, object); You can do this: Naming.rebind( //www.cs.umd.edu:1099/foo, object); If you leave off the //www.cs.umd.edu:1099 part, Naming.rebind will look for the registry running on the local host on the default port (1099)

CLASSPATH Considera/ons Registries associate stubs to names Stubs are objects created from classes that are automa/cally constructed from the remote object s class For a registry to store an object, it needs access to the object s class! Objects store fields Classes store methods How to do this? If the registry and the server are on the same host, make sure the CLASSPATH of the registry includes the directories used by the server If the registry and server are on different hosts, use the java.rmi.server.codebase property of the JVM that is running the server

Property?? java.rmi.server.codebase?? Proper/es are name/value pairs that define informa/on about a JVM and its environment E.g. library paths, OS, etc. Inside Java, to get current proper/es, can execute System.getProperties() Codebase property of JVM gives (space- separated) list of URLs from which class files published by JVM can be downloaded Code base proper/es can be set in special profile files, inside Java program, or at command line E.g. Following command- line entry specifies the given URL as a code base java - Djava.rmi.server.codebase=hop://webline/public/mystuff.jar foo Following command in Java does the same: System.setProperty ( java.rmi.server.codebase, hop://webline/public/mystuff.jar ); How does this help? When a JVM publishes a stub to a registry, the codebase of the object s class is also provided as an annota/on To find the class of the stub object the registry JVM will first try to consult its own CLASSPATH If it cannot find the class in its CLASSPATH, it next looks in the object s codepath annota/on (provided by the server)! BE CAREFUL! IF THE REGISTRY JVM S CLASSPATH CONTAINS A CLASS OF THE SAME NAME AS THE STUB, THE REGISTRY WILL GET ITS LOCAL CLASS RATHER THAN THE (CORRECT) REMOTE ONE!!

Security Downloading remote code poses security risks The code may be buggy The code may be malicious Java enables the defini/on of security managers and policies to limit the access downloaded code has to system resources These can be defined in system proper/es like java.security.manager and java.security.policy Your JVM may have these set already, and they may interfere with remote downloading You can also set a security manager in your Java program using System.setSecurityManager()

Accessing Remote Objects To use a remote object, a client must: Get a registry that has a name assigned to the object Perform a lookup on the registry using the given object s name Note: registry.lookup() returns objects of type Remote You must cast the object to the par/cular subclass of Remote that the object s class implements If successful, the registry will return a stub to the given object To invoke the remote object s method, invoke the method of the same name on the stub! E.g. (from TestStringClient.java; recall TestString extends Remote) Registry registry = LocateRegistry.getRegistry(host); TestString stub1 = (TestString) registry.lookup("test1"); String response = stub1.getteststring(); Same CLASSPATH considera/ons as for registries apply. Also If client uses local classes to create objects to pass (via marshaling / unmarshaling) to server then client s class files must be accessible to server (either via CLASSPATH or codepath property of client)