1 / 22 Remote Method Invocation Jean-Michel Richer jean-michel.richer@univ-angers.fr http://www.info.univ-angers.fr/pub/richer M2 Informatique 2010-2011
2 / 22 Plan Plan 1 Introduction 2 RMI en détails 3 Exemple 4 Application
3 / 22 Introduction Introduction Introduction
4 / 22 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)
5 / 22 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
6 / 22 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
7 / 22 RMI en détails RMI en détails RMI en détails
8 / 22 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
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
10 / 22 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)
11 / 22 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.
12 / 22 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)
13 / 22 Exemple Exemple Exemple
14 / 22 Exemple Exemple Exemple Appel d un objet distant qui réalise l addition de deux nombres réels.
15 / 22 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
16 / 22 Exemple Implantation Implantation étend UnicastRemoteObject implante l interface Service implantation de la méthode add partie implantation 1 import java.rmi.*; 2 import java.rmi.server.*; 3 4 public class ServiceImpl extends UnicastRemoteObject implements Service { 5 6 public ServiceImpl() throws RemoteException { 7 super(); 8 } 9 10 public float add(float a, float b) throws RemoteException { 11 return a+b; 12 } 13 } 14
17 / 22 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
18 / 22 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
Exemple Compilation et exécution Compilation javac *.java rmic ServiceImpl Exécution shell1> rmiregistry shell2> java ServiceServer shell3> java ServiceClient 19 / 22
20 / 22 Application Application Application
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
22 / 22 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