Exemple Java RMI Hello World
RMI signifieRemote Method Invocation et c'est l'équivalent orienté objet de RPC (Remote Procedure Calls). RMI was designed to make the interaction between applications using the object-oriented model and run on different machines seem like that of stand-alone programs.
Le code ci-dessous vous donnera les bases de Java RMI avec un exemple très simple de modèle de communication serveur-client.
1. L'interface à distance
La première chose que nous devons concevoir est l'interfaceRemote
que le serveur et le client implémenteront. LesInterface
doivent toujours être publics et étendre lesRemote
. Toutes les méthodes décrites dans l'interfaceRemote
doivent listerRemoteException
dans leur clause throws.
NotreRMIInterface
n'a qu'une seule méthode; il reçoit un paramètre String et renvoie String.
RMIInterface.java
package com.techfou.rmiinterface; import java.rmi.Remote; import java.rmi.RemoteException; public interface RMIInterface extends Remote { public String helloTo(String name) throws RemoteException; }
2. Le serveur
Notre serveur étendUnicastRemoteObject
et implémente lesInterface
que nous avons créés auparavant. Dans la méthode principale, nous lions le serveur sur localhost avec le nom «MyServer».
ServerOperation.java
package com.techfou.rmiserver; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import com.example.rmiinterface.RMIInterface; public class ServerOperation extends UnicastRemoteObject implements RMIInterface{ private static final long serialVersionUID = 1L; protected ServerOperation() throws RemoteException { super(); } @Override public String helloTo(String name) throws RemoteException{ System.err.println(name + " is trying to contact!"); return "Server says hello to " + name; } public static void main(String[] args){ try { Naming.rebind("//localhost/MyServer", new ServerOperation()); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
3. Le client
Enfin, nous créons le client. Pour «trouver» le Serveur, le Client utilise un ObjetRMIInterface
qui «cherche» une référence pour l'objet distant associé au nom que nous passons en paramètre. Avec cet objetRMIInterface
, nous pouvons maintenant parler au serveur et recevoir des réponses.
Fichier: ClientOperation.java
package com.example.rmiclient; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import javax.swing.JOptionPane; import com.example.rmiinterface.RMIInterface; public class ClientOperation { private static RMIInterface look_up; public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { look_up = (RMIInterface) Naming.lookup("//localhost/MyServer"); String txt = JOptionPane.showInputDialog("What is your name?"); String response = look_up.helloTo(txt); JOptionPane.showMessageDialog(null, response); } }
4. Comment l'exécuter
4.1 Create the three java files with your favorite IDE or Download the code below. Accédez à votre dossier source comme indiqué ci-dessous.
4.2 First thing we need to do is compile our sources. ExécutezcompileEverything.bat
si vous avez téléchargé le code ci-dessous ou ouvrez une fenêtre de commande dans votre répertoire et exécutez:
Terminal
javac src/com/example/rmiinterface/RMIInterface.java src/com/example/rmiserver/ServerOperation.java src/com/example/rmiclient/ClientOperation.java
4.3 Confirm that your sources were compiled by accessing their respective directories:
4.4 Next we need to start the rmiregistry. Encore une fois, exécutez lesstartServer.bat
ou ouvrez une fenêtre de commande et exécutez:
Terminal
cd src start rmiregistry java com.example.rmiserver.ServerOperation
4.5 If RmiRegistry started successfully, there will be another window that looks like this:
4.6 Now we are ready to run our Client:
Ouvrez une nouvelle fenêtre d'invite de commande (ou exécutez lesrunClient.bat
à partir des fichiers téléchargés) et exécutez ceci:
Terminal
cd src java com.example.rmiclient.ClientOperation
LeClientOperation
s'exécute et nous invite à entrer:
Nous tapons un nom dans le champ de saisie (ex. «Marilena») et cliquez sur «OK»
4.7 On Server side we can see this:
4.8 And on the Client side this:
4.9 The client closes after the exchange is over, while the server stays on and we can communicate again if we run the client file again. En effet, nous exécutons à nouveau le client et:
Serveur:
Client :
Télécharger le code source
Téléchargez-le -SimpleRmiExample.zip (4 Ko)