ExempleCh08_05_TesterMyRMIRetourTélécharger Programme de test du package myrmi. Le package est un .jar issu de la compilation de ExempleCh08_04_InterfaceService2. README.txt Pour compiler : compil.bat Pour executer : Lancer d'abord le serveur : runServer1.bat puis runServer2.bat (dans cet ordre) Puis lancer les clients : runClient1.bat et runClient2.bat compil.bat mkdir bin del /f /s /q bin\*.class cd src javac -d ../bin -classpath ".;../lib/myrmi.jar" Server1.java javac -d ../bin -classpath ".;../lib/myrmi.jar" Server2.java javac -d ../bin -classpath ".;../lib/myrmi.jar" Client1.java javac -d ../bin -classpath ".;../lib/myrmi.jar" Client2.java pause runClient1.bat cd bin java -classpath "." Client1 pause runClient2.bat cd bin java -classpath "." Client2 pause runServer1.bat cd bin java -classpath "." Server1 9100 9101 9102 pause runServer2.bat cd bin java -classpath "." Server2 9100 9103 pause src Client1.java import java.util.*; import fr.cnam.myrmi.MyNaming; public class Client1 { public static void main(String... args) throws Exception { System.out.println("Execution de Client1"); Service1ODInt serv1 = (Service1ODInt)MyNaming.lookup("rmi://localhost:9100/SERVER1-1"); Service1ODInt serv1bis = (Service1ODInt)MyNaming.lookup("rmi://localhost:9100/SERVER1-2"); for(int i=0;i<20;i++) { Resultat res1 = serv1.traitement(new Parametre(i)); Resultat res1bis = serv1bis.traitement(new Parametre(i)); System.out.println(String.format("SERVER1-1 : %3d SERVER1-2 : %3d",res1.getValeur(),res1bis.getValeur())); try{Thread.sleep(1000);}catch(Exception ex){}; } } } Client2.java import java.util.*; import fr.cnam.myrmi.MyNaming; public class Client2 { public static void main(String... args) throws Exception { System.out.println("Execution de Client2"); Service1ODInt serv1 = (Service1ODInt)MyNaming.lookup("rmi://localhost:9100/SERVER1-1"); Service2ODInt serv2 = (Service2ODInt)MyNaming.lookup("rmi://localhost:9100/SERVER2"); for(int i=0;i<20;i++) { Resultat res1 = serv1.traitement(new Parametre(i)); Resultat res2 = serv2.traitement2(new Parametre(i)); System.out.println(String.format("SERVER1-1 : %3d SERVER2 : %3d",res1.getValeur(),res2.getValeur())); try{Thread.sleep(1000);}catch(Exception ex){}; } } } Parametre.java import java.io.Serializable; public class Parametre implements Serializable { private int x; public Parametre(int x) { this.x=x; } public int getX(){return x;} } Resultat.java import java.io.Serializable; public class Resultat implements Serializable { private int valeur; public Resultat(int valeur) { this.valeur=valeur; } public int getValeur(){return valeur;} } Server1.java import java.util.*; import java.io.*; import java.net.*; import fr.cnam.myrmi.*; // =================================== public class Server1 { public static void main(String args[]) throws Exception { int portRegistry = Integer.parseInt(args[0]); int portURO1 = Integer.parseInt(args[1]); int portURO2 = Integer.parseInt(args[2]); try{ MyLocateRegistry.createRegistry(portRegistry); } catch(Exception ex){}; Service1OD sod1 = new Service1OD(portURO1); String uri1 = "rmi://localhost:"+portRegistry+"/SERVER1-1"; MyNaming.rebind(uri1,sod1); System.out.println("REBIND: "+uri1+" sur port "+portURO1); Service1OD sod2 = new Service1OD(portURO2); String uri2 = "rmi://localhost:"+portRegistry+"/SERVER1-2"; MyNaming.rebind(uri2,sod2); System.out.println("REBIND: "+uri2+" sur port "+portURO2); } } Server2.java import java.util.*; import java.io.*; import java.net.*; import fr.cnam.myrmi.*; // =================================== public class Server2 { public static void main(String args[]) throws Exception { int portRegistry = Integer.parseInt(args[0]); int portURO = Integer.parseInt(args[1]); Service2OD sod = new Service2OD(portURO); String uri = "rmi://localhost:"+portRegistry+"/SERVER2"; MyNaming.rebind(uri,sod); System.out.println("REBIND: "+uri+" sur port "+portURO); } } Service1OD.java import java.io.*; import fr.cnam.myrmi.*; public class Service1OD extends MyUnicastRemoteObject implements Service1ODInt { public Service1OD(int port) { super(port); } public Resultat traitement(Parametre p) throws MyRemoteException { System.out.println("Service1OD : traitement"); int x = p.getX(); Resultat res = new Resultat(x*x); return res; } } Service1ODInt.java import fr.cnam.myrmi.*; public interface Service1ODInt extends MyRemote { public Resultat traitement(Parametre p) throws MyRemoteException; } Service2OD.java import java.io.*; import fr.cnam.myrmi.*; public class Service2OD extends MyUnicastRemoteObject implements Service2ODInt { public Service2OD(int port) { super(port); } public Resultat traitement2(Parametre p) throws MyRemoteException { System.out.println("Service2OD : traitement"); int x = p.getX(); Resultat res = new Resultat(x+x); return res; } } Service2ODInt.java import fr.cnam.myrmi.*; public interface Service2ODInt extends MyRemote { public Resultat traitement2(Parametre p) throws MyRemoteException; }