Building Distributed Systems With RMI: Agenda
Building Distributed Systems With RMI: Agenda
Sara.Bouchenak@imag.fr S11
Multi-tier distributed systems (CM), S. Bouchenak , 13:30 – 15:00
Servlet-based distributed systems (TD), S. Bouchenak & D. Serrano, 15:15 – 18:30
http://sardes.inrialpes.fr/~bouchena/teaching/ Presentation of the project (CM), S. Bouchenak , 13:30 – 15:00
S12
Multi-tier distributed systems (TD), S. Bouchenak & D. Serrano, 15:15 – 18:30
S14 –
Sockets are restricted to the transmission of data In local applications, objects communicate via methods
Sockets leave the semantics of this data unconsidered
It would be desirable, for distributed applications, to have a
similar communication paradigm available
Protocols which provide the semantic interpretation of the
data must be developed on the application level
Such a communication paradigm would permit the remote call
The development of such protocols is often time-consuming of methods
and error-prone
Java provides the Remote Method Invocation mechanism: RMI
© S. Bouchenak Distributed systems & Middleware 3 © S. Bouchenak Distributed systems & Middleware 4
Remote Procedure Call (RPC) RPC implementation
RPC is a technology developed in the 80s to call procedures Different address spaces
on remotes computers (with the procedural programming
paradigm) In the local case:
Data used in a procedure call is simply passed as a reference (a
RPC allows the call of procedures located in another process pointer)
space on a remote computer (or on the same machine) This reference refers to a physical memory address
Such a reference has no correct meaning in a different address
space
Technical issues for implementing RPC
Different addess spaces In the distributed case:
The referenced data used in a remote call needs to be passed as
Heterogeneous machines a copy
© S. Bouchenak Distributed systems & Middleware 5 © S. Bouchenak Distributed systems & Middleware 6
© S. Bouchenak Distributed systems & Middleware 7 © S. Bouchenak Distributed systems & Middleware 8
Overview of RMI-based Overview of RMI-based
distributed applications distributed applications (2)
RMI applications comprise two separate programs, a server and a Distributed object applications follow these steps:
client Client locates remote objects on server
Various mechanisms to obtain references to remote objects
The server program: An application server can register its remote objects with RMI registry
(RMI's simple naming facility)
creates some remote objects,
An application server can return remote object references as part of
makes references to these objects accessible, other remote invocations
and waits for clients to invoke methods on these objects. Client communicates with remote objects on server
To the client programmer, remote communication looks similar to
The client program: regular Java method invocations
obtains a remote reference to one or more remote objects on a server, Details of remote communication are handled by RMI and transparent
to client and server programs
and then invokes methods on them.
Load class definitions for objects that are passed around
Because RMI enables objects to be passed back and forth, it
RMI provides the mechanism by which the server and the client provides mechanisms for loading an object's class definition
communicate and pass information back and forth.
© S. Bouchenak Distributed systems & Middleware 9 © S. Bouchenak Distributed systems & Middleware 10
Overview of RMI-based
distributed applications (3) Remote objects
Objects with methods that can be invoked across Java virtual
2
rmiregistry machines are called remote objects
© S. Bouchenak Distributed systems & Middleware 11 © S. Bouchenak Distributed systems & Middleware 12
Outline A simple example
Remote object – Interface definition
import java.rmi.*;
1. Motivations
public interface Hello
2. Overview of RMI-based distributed applications extends Remote {
© S. Bouchenak Distributed systems & Middleware 13 © S. Bouchenak Distributed systems & Middleware 14
try {
// A method provided by the private String message; // Create a Hello remote object
// remore object HelloImp h =
new HelloImp ("Hello world !");
public String sayHello() public Hello(String s) { Hello h_stub = (Hello)
throws RemoteException; message = s ; UnicastRemoteObject.exportObject(h, 0);
© S. Bouchenak Distributed systems & Middleware 15 © S. Bouchenak Distributed systems & Middleware 16
A simple example (4) Outline
Server Client
import java.rmi.*; import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.rmi.registry.*; 1. Motivations
public class HelloClient {
public class HelloServer { public static void main(String [] args) {
© S. Bouchenak Distributed systems & Middleware 21 © S. Bouchenak Distributed systems & Middleware 22
© S. Bouchenak Distributed systems & Middleware 23 © S. Bouchenak Distributed systems & Middleware 24
Making classes network Steps to build distributed
accessible applications with RMI
Certain class definitions are made network Using RMI to develop a distributed application
accessible involves the following general steps:
such as the definitions for the remote interfaces 1. Designing and implementing the components of the
and their associated types, distributed application
and the definitions for classes that need to be downloaded
to the clients or servers 2. Compiling sources
Class definitions are typically made network 3. Making classes network accessible
accessible through a web server
4. Starting the application
© S. Bouchenak Distributed systems & Middleware 25 © S. Bouchenak Distributed systems & Middleware 26
running the RMI remote object registry 2. Overview of RMI-based distributed applications
using the rmiregistry tool
1. rmiregistry 3. A simple example
the server
4. Methodology to build distributed applications using RMI
using the java tool
© S. Bouchenak Distributed systems & Middleware 27 © S. Bouchenak Distributed systems & Middleware 28
The architecture of RMI The architecture of RMI (2)
Server Client
import java.rmi.*; import java.rmi.*;
Transparency regarding distribution import java.rmi.server.*;
import java.rmi.registry.*;
import java.rmi.registry.*;
RMI offers full trasparency regarding distribution public class HelloServer { public class HelloClient {
public static void main(String [] args) {
public static void main(String [] args){
... try {
After a first initialization, a call can be used in exactly the same way as in if (args.lenght < 1) {
try {
the local case // Create a Hello remote object System.out.println("Usage:
java HelloClient <server host>"); return;
HelloImp h =
new HelloImp ("Hello world !"); }
Hello h_stub = (Hello) String host = arg[0];
UnicastRemoteObject.exportObject(h, 0);
// Get remote object reference
// Register the remote object in RMI Registry registry =
// registry with a given identifier LocateRegistry.getRegistry(host);
Registry registry= LocateRegistry.getRegistry(); Hello h = (Hello) registry.lookup("Hello1");
registry.bind("Hello1”, h_stub);
// Remote method invocation
System.out.println ("Server ready");
String res = h.sayHello();
} catch (Exception e) { System.out.println(res);
System.err.println("Error on server :" } catch (Exception e) {
+ e) ; System.err.println("Error on
e.printStackTrace();
client: " + e) ;
return;
} }
} }
} }
© S. Bouchenak Distributed systems & Middleware 29 © S. Bouchenak Distributed systems & Middleware 30
© S. Bouchenak Distributed systems & Middleware 31 © S. Bouchenak Distributed systems & Middleware 32
The architecture of RMI (4) The architecture of RMI (5)
The stub and skeleton layer in RMI Client Server The reference layer in RMI Client Server
© S. Bouchenak Distributed systems & Middleware 33 © S. Bouchenak Distributed systems & Middleware 34
provided class
Class java.rmi.server.UnicastRemoteObject
provided interface
Provides support for point-to-point active object references
(invocations, parameters, and results)
class to develop
interface to develop
Uses TCP streams
© S. Bouchenak Distributed systems & Middleware 37 © S. Bouchenak Distributed systems & Middleware 38
© S. Bouchenak Distributed systems & Middleware 39 © S. Bouchenak Distributed systems & Middleware 40
Outline A detailed example step by step
1. Motivations Main steps to create a distributed application with RMI:
Server side Client side
2. Overview of RMI-based distributed applications
Define the remote interface provided by the remote object
4. Methodology to build distributed applications using RMI Implement the server program Implement the client program
6. A detailed example step by step Start the server Start the client
© S. Bouchenak Distributed systems & Middleware 41 © S. Bouchenak Distributed systems & Middleware 42
A detailed example step by step (2) A detailed example step by step (3)
Define the remote interface provided by the remote object: Implement the remote object in a class:
Extends java.rmi.Remote Declare the remote interface being implemented
Defines the set of methods that can be called remotely Implement the set of methods that can be called remotely
Each method must declare java.rmi.RemoteException Implement any other local method that can not be invoked remotely
import java.rmi.RemoteException;
import java.rmi.Remote;
public class HelloImp implements Hello {
Import java.rmi.RemoteException;
private String message;
public Hello(String s) {
public interface Hello extends Remote {
message = s ;
// A method provided by the remore object }
public String sayHello() throws RemoteException;
public String sayHello () throws RemoteException {
} return message ;
}
}
© S. Bouchenak Distributed systems & Middleware 43 © S. Bouchenak Distributed systems & Middleware 44
A detailed example step by step (4) A detailed example step by step (5)
Implement the remote object in a class – Passing objects in RMI How arguments and return values are passed in remote method invocations
Arguments to remote methods or return values from remote methods can Remote objects are essentially passed by reference
be of any type A remote object reference is a stub
Primitive data types (e.g. int, float, etc.) It is a client-side proxy that implements the remote interface that the remote object
implements
Remote objects
Passing a remote object by reference means that any changes made to the state of the
Local objects object by remote method invocations are reflected in the original remote object
Objects passed to or returned from remote methods must be serializable
Local objects are passed by copy
They must implement the java.io.Serializable interface Using object serialization
Some object types do not meet any of these criteria; they cannot be By default, all fields are copied except fields that are marked static or transient
Default serialization behavior can be overridden on a class-by-class basis
passed to or returned from remote methods
A copy of the object is created in the receiving Java virtual machine
Most of these objects, such as threads or file descriptors, encapsulate Any changes to the object's state by the receiver are reflected only in the receiver's copy, not
information that makes sense only within a single address space in the sender's original instance
Any changes to the object's state by the sender are reflected only in the sender's original
Many of the core classes (e.g. classes in the packages java.lang and instance, not in the receiver's copy
java.util) implement the Serializable interface
© S. Bouchenak Distributed systems & Middleware 45 © S. Bouchenak Distributed systems & Middleware 46
A detailed example step by step (6) A detailed example step by step (7)
Implement the server program: Implement the server program – Create and install a Security Manager
Create and install a security manager
The first task of the server program is to create and install a security manager
Create and export remote objects
Register remote objects with the RMI registry This protects access to system resources from untrusted downloaded code running
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry; within the Java virtual machine
import java.rmi.registry.Registry;
public class HelloServer { A security manager determines whether downloaded code has access to the local
public static void main(String [] args){
try {
file system or can perform any other privileged operations
if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager());}
HelloImp h = new HelloImp ("Hello world !");
If an RMI program does not install a security manager, RMI will not download
Hello h_stub = (Hello) UnicastRemoteObject.exportObject(h, 0);
Registry registry= LocateRegistry.getRegistry(); classes (other than from the local class path) for objects received as arguments or
registry.bind("Hello1”, h_stub); return values of remote method invocations
System.out.println ("Server ready");
} catch (Exception e) {
System.err.println("Error on server :" + e) ; e.printStackTrace(); return; This restriction ensures that the operations performed by downloaded code are
}
subject to a security policy
}
}
© S. Bouchenak Distributed systems & Middleware 47 © S. Bouchenak Distributed systems & Middleware 48
A detailed example step by step (8) A detailed example step by step (9)
Implement the client program: Compile source files
Create and install a security manager This example separates
The remote interface
Get a remot object reference
The remote object implementation class
Perform remote method invocations on the remote object
The server program class
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; The client program class
public class HelloClient {
public static void main(String [] args) { Compile the remote interface and build a jar file that contains it
if (args.lenght < 1) { System.out.println("Usage: java HelloClient <server host>"); return; }
try {
javac –d classes –classpath .:classes src/Hello.java
if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager()); } jar cvf lib/Hello.jar classes/Hello.class
String host = arg[0];
Registry registry = LocateRegistry.getRegistry(host);
Hello h = (Hello) registry.lookup("Hello1"); Compile the remote object implementation class and build a jar file that
contains it
String res = h.sayHello(); System.out.println(res);
} catch (Exception e) { javac –d classes –classpath .:classes:lib/Hello.jar src/HelloImp.java
System.err.println("Error on client: " + e); e.printStackTrace(); return;
jar cvf lib/HelloImp.jar classes/HelloImp.class
}
}
}
© S. Bouchenak Distributed systems & Middleware 49 © S. Bouchenak Distributed systems & Middleware 50
A detailed example step by step (10) A detailed example step by step (11)
Compile and run server-side and client-side programs: A note about security
Server-side The server and client programs run with a security manager installed
Compile the server program When either program runs, a security policy file must be specified
javac –d classes –classpath .:classes:lib/Hello.jar:lib/HelloImp.jar
src/HelloServer.java So that the code is granted the security permissions it needs to run
Start RMI registry
rmiregistry & Example of a policy file (named server.policy) to use with the server
Start the server grant codeBase "file:/home/ann/src/" {
java –classpath .:classes:lib/Hello.jar:lib/HelloImp.jar HelloServer permission java.security.AllPermission;
};
Client-side
Compile the client program Example of a policy file (named client.policy) to use with the client
javac –d classes –classpath .:classes:lib/Hello.jar src/HelloClient.java grant codeBase "file:/home/john/src/" {
Start the client permission java.security.AllPermission;
java –classpath .:classes:lib/Hello.jar HelloClient };
© S. Bouchenak Distributed systems & Middleware 51 © S. Bouchenak Distributed systems & Middleware 52
Agenda References
Week Wednesday, 13:30 – 18:30
S6
Introduction to distributed systems and middleware (CM), S. Bouchenak, 13:30 – 15:00 This lecture is extensively based on:
Introduction to JDBC (CM), C. Labbé, 15:15 – 16:45
RMI-based distributed systems (CM), S. Bouchenak , 13:30 – 15:00
S7
RMI-based distributed systems (TD), S. Bouchenak & D. Serrano, 15:15 – 18:30
Sun Microsystems. Java Tutorial on RMI.
Servlet-based distributed systems (CM), S. Bouchenak , 13:30 – 15:00
S8
RMI-based distributed systems (TD), S. Bouchenak & D. Serrano, 15:15 – 18:30 http://java.sun.com/docs/books/tutorial/rmi/
S9 Interruption week
S10 Introduction to transactions (CM), C. Labbé, 13:30 – 15:00 M. Boger. Java in Distributed Systems: Concurrency, Distribution
Multi-tier distributed systems (CM), S. Bouchenak , 13:30 – 15:00 and Persistence. Wiley, 2001.
S11
Servlet-based distributed systems (TD), S. Bouchenak & D. Serrano, 15:15 – 18:30
Presentation of the project (CM), S. Bouchenak , 13:30 – 15:00
S12
Multi-tier distributed systems (TD), S. Bouchenak & D. Serrano, 15:15 – 18:30 This lecture is partly based on lectures given by Sacha Krakowiak,
S13 Support projet (TD), C. Labbé & D. Serrano, 15:15 – 18:30 http://sardes.inrialpes.fr/people/krakowia/
S14 –
© S. Bouchenak Distributed systems & Middleware 53 © S. Bouchenak Distributed systems & Middleware 54