RMI (Remote Method Invocation) in Java: Distributed Computing and Remote Objects - Prof. A, Study notes of Programming Languages

A series of notes from a university course, cmsc 433, taught by alan sussman at the university of maryland. The notes cover the topic of remote method invocation (rmi) in java, which is a way to achieve distributed computation by making function calls that appear local but are actually executed on another machine. The concept of remote objects and interfaces, the rmi compiler, passing arguments, downloading code, and security considerations. It also includes examples of an rmi chat server and client.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-1c8-1
koofers-user-1c8-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 433, Alan Sussman, U. Maryland (via
Bill Pugh) 1
CMSC433, Spring 2001
Programming Language
Technology and Paradigms
Java RMI
Alan Sussman
October 25, 2001
CMCS 433, Fall 2001 -Alan Sussman 2
Administrivia
Read Chapter 15, pages 973-979, on RMI
Project 3 due tomorrow
Project 2 grades out, but will need some
regrading
Project 4 due November 7
distributed chat server, using RMI
posted by tomorrow, central server running
soon
CMCS 433, Fall 2001 -Alan Sussman 3
Last time
Multithreaded programming
and the Java memory model
Motto is: Be careful!
CMCS 433, Fall 2001 -Alan Sussman 4
Different Approaches to
Distributed Computation
High-performance, parallel scientific apps
Connecting via sockets
custom protocols for each application
RPC/DCOM/CORBA/RMI
make what looks like a normal function call
function is actually invoked on another machine
Arguments are marshalled for transport
return value is marshalled as well
CMCS 433, Fall 2001 -Alan Sussman 5
Remote Method Invocation
Easy way to get distributed computation
Have stub for remote object
calls to stub get translated into network call
Arguments and return values can be passed
over network
CMCS 433, Fall 2001 -Alan Sussman 6
Remote Objects and Interfaces
Remote Objects are those that can be
referenced remotely
extend java.rmi.UnicastRemoteObject
constructor throws java.rmi.RemoteException
Remote interfaces describe services that can
be provided remotely
extend java.rmi.Remote interface
all methods throw java.rmi.RemoteException
pf3
pf4

Partial preview of the text

Download RMI (Remote Method Invocation) in Java: Distributed Computing and Remote Objects - Prof. A and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Alan Sussman, U. Maryland (via

CMSC433, Spring 2001

Programming Language

Technology and Paradigms

Java RMI

Alan Sussman

October 25, 2001

CMCS 433, Fall 2001 - Alan Sussman 2

Administrivia

• Read Chapter 15, pages 973-979, on RMI

• Project 3 due tomorrow

• Project 2 grades out, but will need some

regrading

• Project 4 due November 7

  • distributed chat server, using RMI
  • posted by tomorrow, central server running

soon

CMCS 433, Fall 2001 - Alan Sussman 3

Last time

• Multithreaded programming

  • and the Java memory model
  • Motto is: Be careful!

CMCS 433, Fall 2001 - Alan Sussman 4

Different Approaches to

Distributed Computation

• High-performance, parallel scientific apps

• Connecting via sockets

  • custom protocols for each application

• RPC/DCOM/CORBA/RMI

  • make what looks like a normal function call
  • function is actually invoked on another machine
  • Arguments are marshalled for transport
  • return value is marshalled as well

CMCS 433, Fall 2001 - Alan Sussman 5

Remote Method Invocation

• Easy way to get distributed computation

• Have stub for remote object

  • calls to stub get translated into network call

• Arguments and return values can be passed

over network

CMCS 433, Fall 2001 - Alan Sussman 6

Remote Objects and Interfaces

• Remote Objects are those that can be

referenced remotely

  • extend java.rmi.UnicastRemoteObject
  • constructor throws java.rmi.RemoteException

• Remote interfaces describe services that can

be provided remotely

  • extend java.rmi.Remote interface
  • all methods throw java.rmi.RemoteException

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Fall 2001 - Alan Sussman 7

RMIC - RMI Compiler

• Generates stub code for a class

  • For 1.1, also generates skeleton class
  • skeleton not needed for 1.2+

• Generates stubs for all methods declared in

remote interfaces

  • other methods don’t get a stub

CMCS 433, Fall 2001 - Alan Sussman 8

Passing arguments

• Can pass arbitrary values as arguments

• Can return arbitrary values as results

• To pass a value, it must either be

  • Serializable , or
  • Remote

• Passing the same Serializable object in

different calls

  • will materialize different objects at receiver

CMCS 433, Fall 2001 - Alan Sussman 9

Downloading code

• When you pass a reference to a remote class

  • receiver (the method being called) needs stub

class code (class file generated by rmic )

• When you pass a ref to serializable class

  • receiver needs class (the class file from javac )

• References to remote and serializable

classes annotated with RMI codebase

  • where code can be loaded from

CMCS 433, Fall 2001 - Alan Sussman 10

SecurityManager

• Must install some Security Manager to

allow download of classes from RMI

codebase

• Can use RMISecurityManager

System.setSecurityManager(new

RMISecurityManager());

• Modify policy file to grant permissions

  • example of that on Lectures web page

CMCS 433, Fall 2001 - Alan Sussman 11

Naming.lookup

  • Naming.lookup is used to bootstrap RMI

communication

  • Get your first reference to a remote object
  • Someone runs an RMIRegistry
  • a separate Java VM (or can run inside a JVM also running something else)
  • that listens to a particular port (default 1099)
  • Can bind/unbind/rebind name on localhost (the

one running the registry)

  • Can lookup name on any host CMCS 433, Fall 2001 - Alan Sussman 12

RMI Chat server example

• Server

  • runs the chat room

• Client

  • participant in chat room
  • receives messages from others in room

• Connection

  • uniquely identifies a client
  • used to speak in chat room

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Fall 2001 - Alan Sussman 19

Server Lookup on Client host

ClientImpl

Server s = (Server) Naming.lookup(“//host/ChatServer”);

Hosted Remote Objects

c

s

ServerImpl Stub

Hosted Remote Objects

ServerImpl CMCS 433, Fall 2001 - Alan Sussman 20

Invoke message on Server from Client host

prepare call on client

ClientImpl

Connection conn = s.logon(“Alan”, c);

Hosted Remote Objects

c

s

ServerImpl Stub

Marshalled arguments Serialized String “Alan” Serialized Stub for c

target

method: logon

CMCS 433, Fall 2001 - Alan Sussman 21

Unmarshalling arguments

on Server host

“Alan”

ClientImpl Stub

ServerImpl

Marshalled arguments Serialized String “Alan” Serialized Stub for c

target

method: logon logon

CMCS 433, Fall 2001 - Alan Sussman 22

Execution on Server host

“Alan”

ClientImpl Stub

ServerImpl

Hosted Remote Objects

name

client

conn

Connection Impl

CMCS 433, Fall 2001 - Alan Sussman 23

Marshalling return value on

Server host

“Alan”

ClientImpl Stub

ServerImpl

Hosted Remote Objects

Connection Impl

Marshalled return value:

Serialized Stub for conn

conn

CMCS 433, Fall 2001 - Alan Sussman 24

Unmarshalling return value on Client host

ClientImpl

Hosted Remote Objects

c

s

ServerImpl Stub

Marshalled return value Serialized Stub for conn

ConnImpl Stub

conn