cours Remote Method Invocation

publicité
Remote Method Invocation
Remote Method Invocation
Jean-Michel Richer
[email protected]
http://www.info.univ-angers.fr/pub/richer
M2 Informatique 2010-2011
1 / 22
Remote Method Invocation
Plan
Plan
1
Introduction
2
RMI en détails
3
Exemple
4
Application
2 / 22
Remote Method Invocation
Introduction
Introduction
Introduction
3 / 22
Remote Method Invocation
Introduction
Principe
Le principe
• applications distribuées (ex. Agence de Voyage)
• appel de services distants (interaction avec des
objets/services situés sur une autre machine)
• obtention de données distantes (communication données
résultat)
4 / 22
Remote Method Invocation
Introduction
Technologies
Les technologies
Il existe plusieurs protocoles/frameworks/technologies :
• RPC (Remote Procedure Call)
• RMI (Remote Method Invocation) SUN
• CORBA (Common Object Request Broker Architecture)OMG (Object Management Group)
• DCOM - Microsoft
5 / 22
Remote Method Invocation
Introduction
RMI - SUN
Propriétés de RMI
• échange d’information entre deux JVM (Java Virtual
Machine)
• orienté objet
• sérialisation des objets Java
• chargement dynamique des objets/services
6 / 22
Remote Method Invocation
RMI en détails
RMI en détails
RMI en détails
7 / 22
Remote Method Invocation
RMI en détails
RMI
RMI-JRMP (Java Remote Method Protocol)
• version d’origine
• intégrée au langage
• simple d’utilisation
RMI-IIOP (Internet InterORB(Object Request Broker) Protocol)
• version récente
• comptatible CORBA
• plus difficile à mettre en oeuvre
8 / 22
Remote Method Invocation
RMI en détails
Différences JRMP-IIOP
Différences
• implantation d’un objet distant :
• JRMP : UnicastRemoteObject
• IIOP : PortableRemoteObject
• ramasse miettes (garbace collector) :
• JRMP : implicite (DGC)
• IIOP : à réaliser (unreferenced())
9 / 22
Remote Method Invocation
RMI en détails
RMI
Service distant
Les fonctionnalités d’un service distant sont définies par une
interface
• sur le serveur : l’interface est implantée
• sur le client : l’interface sert de proxy (serveur mandataire)
10 / 22
Remote Method Invocation
RMI en détails
le Skeleton où partie Serveur
Le Skeleton (Squelette)
• objet distant qui implémente les méthodes visibles
• réception des données (unmarshall)
• exécution de la méthode
• envoi du résultat (marshall)
Note
Avec Java 2, le squelette est devenu obsolète.
11 / 22
Remote Method Invocation
RMI en détails
le Stub où partie Client
Le Stub (Souche)
• représentant local de l’objet distant qui implémente les
méthodes visibles
• envoi des paramètres (marshall)
• récupération des données (unmarshall)
12 / 22
Remote Method Invocation
Exemple
Exemple
Exemple
13 / 22
Remote Method Invocation
Exemple
Exemple
Exemple
Appel d’un objet distant qui réalise l’addition de deux nombres
réels.
14 / 22
Remote Method Invocation
Exemple
Interface
Interface
définition de l’interface de communication : méthode add
• étend Remote
• chaque méthode est susceptible de générer une exception
partie interface
1 import java.rmi.*;
2
3 public interface Service extends Remote {
4 public float add(float a, float b) throws RemoteException;
5 }
6
7
15 / 22
Remote Method Invocation
Exemple
Implantation
Implantation
• étend UnicastRemoteObject
• implante l’interface Service
• implantation de la méthode add
partie implantation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.rmi.*;
import java.rmi.server.*;
public class ServiceImpl extends UnicastRemoteObject implements Service {
public ServiceImpl() throws RemoteException {
super();
}
public float add(float a, float b) throws RemoteException {
return a+b;
}
}
16 / 22
Remote Method Invocation
Exemple
Serveur
Implantation
enregistrement du service Naming.rebind()
partie serveur
1 import java.rmi.*;
2
3 public class ServiceServer {
4
5 public ServiceServer() {
6
try {
7
ServiceImpl s=new ServiceImpl();
8
Naming.rebind(”rmi://localhost:1099/Service”, s);
9
} catch(Exception e) {
10
System.out.println(e.getMessage());
11
}
12 }
13
14 public static void main(String args[]) {
15
new ServiceServer();
16 }
17 }
18
17 / 22
Remote Method Invocation
Exemple
Client
Implantation
appel du service Naming.lookup()
partie client
1 import java.rmi.*;
2
3 public class ServiceClient {
4
5 public static void main(String args[]) {
6
try {
7
Service s=(Service) Naming.lookup(”rmi://localhost:1099/Service”);
8
System.out.println( s.add(4.2f, 3.7f) );
9
} catch(Exception e) {
10
System.out.println(e.getMessage());
11
e.printStackTrace();
12
}
13 }
14 }
15
18 / 22
Remote Method Invocation
Exemple
Compilation et exécution
Compilation
javac *.java
rmic ServiceImpl
Exécution
shell1> rmiregistry
shell2> java ServiceServer
shell3> java ServiceClient
19 / 22
Remote Method Invocation
Application
Application
Application
20 / 22
Remote Method Invocation
Application
Application
Partie Serveur
Créer un objet distant qui sera chargé de fournir la liste des
personnes stockées dans une base de données et dont le nom
ou prénom correspond à des valeurs fournies en paramètres.
Partie Client
Interrogation du service distant et affichage des personnes
susceptibles de répondre aux critères fournis.
21 / 22
Remote Method Invocation
Application
Bibliographie
• Développement Web avec J2EE, O’ Reilly, Eric Sarrion,
Paris, 2005, ISBN 2-35402-140-2
• Au coeur de Java 2 - Fonctions avancées, Campus Press,
Hortsmann et Cornell, 2002, ISBN 2-7440-1332-3
22 / 22
Téléchargement