remote method invocation - univ-angers.frricher/ens/m2cdsii/crs_rmi.pdf · remote method invocation...
TRANSCRIPT
Remote Method Invocation
Remote Method Invocation
Jean-Michel [email protected]
http://www.info.univ-angers.fr/pub/richer
M2 Informatique 2010-2011
1 / 22
Remote Method Invocation
Introduction
Principe
Le principe
• applications distribuees (ex. Agence de Voyage)
• appel de services distants (interaction avec desobjets/services situes sur une autre machine)
• obtention de donnees distantes (communication donneesresultat)
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
Proprietes de RMI
• echange d’information entre deux JVM (Java VirtualMachine)
• oriente objet
• serialisation des objets Java
• chargement dynamique des objets/services
6 / 22
Remote Method Invocation
RMI en details
RMI
RMI-JRMP (Java Remote Method Protocol)
• version d’origine
• integree au langage
• simple d’utilisation
RMI-IIOP (Internet InterORB(Object Request Broker) Protocol)
• version recente
• comptatible CORBA
• plus difficile a mettre en oeuvre
8 / 22
Remote Method Invocation
RMI en details
Differences JRMP-IIOP
Differences
• implantation d’un objet distant :• JRMP : UnicastRemoteObject• IIOP : PortableRemoteObject
• ramasse miettes (garbace collector) :• JRMP : implicite (DGC)• IIOP : a realiser (unreferenced())
9 / 22
Remote Method Invocation
RMI en details
RMI
Service distant
Les fonctionnalites d’un service distant sont definies par uneinterface
• sur le serveur : l’interface est implantee
• sur le client : l’interface sert de proxy (serveur mandataire)
10 / 22
Remote Method Invocation
RMI en details
le Skeleton ou partie Serveur
Le Skeleton (Squelette)
• objet distant qui implemente les methodes visibles
• reception des donnees (unmarshall)
• execution de la methode
• envoi du resultat (marshall)
Note
Avec Java 2, le squelette est devenu obsolete.
11 / 22
Remote Method Invocation
RMI en details
le Stub ou partie Client
Le Stub (Souche)
• representant local de l’objet distant qui implemente lesmethodes visibles
• envoi des parametres (marshall)
• recuperation des donnees (unmarshall)
12 / 22
Remote Method Invocation
Exemple
Exemple
Exemple
Appel d’un objet distant qui realise l’addition de deux nombresreels.
14 / 22
Remote Method Invocation
Exemple
Interface
Interface
definition de l’interface de communication : methode add
• etend Remote
• chaque methode est susceptible de generer une exception
partie interface
1 import java.rmi.*;23 public interface Service extends Remote {4 public float add(float a, float b) throws RemoteException ;5 }67
15 / 22
Remote Method Invocation
Exemple
Implantation
Implantation
• etend UnicastRemoteObject
• implante l’interface Service
• implantation de la methode add
partie implantation
1 import java.rmi.*;2 import java.rmi.server.*;34 public class ServiceImpl extends UnicastRemoteObject implements Service {56 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
16 / 22
Remote Method Invocation
Exemple
Serveur
Implantation
enregistrement du service Naming.rebind()
partie serveur
1 import java.rmi.*;23 public class ServiceServer {45 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 }1314 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.*;23 public class ServiceClient {45 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 execution
Compilation
javac *.javarmic ServiceImpl
Executionshell1> rmiregistryshell2> java ServiceServershell3> java ServiceClient
19 / 22
Remote Method Invocation
Application
Application
Partie Serveur
Creer un objet distant qui sera charge de fournir la liste despersonnes stockees dans une base de donnees et dont le nomou prenom correspond a des valeurs fournies en parametres.
Partie Client
Interrogation du service distant et affichage des personnessusceptibles de repondre aux criteres fournis.
21 / 22