JavaRMI && UnmarshallException
Bonjour,
Cela fait un bout de temps que je n'ai pas bossé le RMI et j'étais en train de réviser un peu en faisant un prog tout simple de gestion de compte bancaire.
Les fonctionalités offertes par ce service sont l'authentification, créditer / débiter son compte. Sauf que j'ai un UnmarshallException coté client.
J'ai voulu passer mes objets par valeur (implements Remote) et je crois que c'est là que ça coince. Est ce qq1 peut m'aider svp?
Voici le code de mon interface :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
package fr.grosdim.bankaccount.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
import fr.grosdim.bankaccount.bean.Compte;
public interface IBankAccount extends Remote {
/**
* @param code
* @return
* @throws RemoteException
*/
public Compte authentication(String code) throws RemoteException;
/**
* @param compte
* @param solde
* @throws RemoteException
*/
public void crediter(Compte compte, int solde) throws RemoteException;
/**
* @param compte
* @param solde
* @throws RemoteException
*/
public void debiter ( Compte compte, int solde) throws RemoteException;
/**
* @param code
* @return
* @throws RemoteException
*/
public Compte getCompte ( String code) throws RemoteException;
} |
Voici son implémentation
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
package fr.grosdim.bankaccount.server;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import fr.grosdim.bankaccount.bean.Compte;
public class BankAccount implements IBankAccount {
private Map<String, Compte> listCompte = new HashMap<String, Compte>();
public BankAccount() {
super();
putCompte("Bob", new Compte("Bob", 1500));
putCompte("alice", new Compte("Alice", 3500));
putCompte("trudy", new Compte("Trudy", 200));
putCompte("jack", new Compte("Jack", 10000));
putCompte("claire", new Compte("Claire", 20000));
}
@Override
public Compte authentication(String code) throws RemoteException {
// TODO Auto-generated method stub
if (listCompte.containsKey(code.toLowerCase()))
return getCompte(code);
return null;
}
@Override
public void crediter(Compte compte, int solde) throws RemoteException {
// TODO Auto-generated method stub
System.out.println( compte.getResponsable() );
int oldSolde = compte.getSolde();
int newSolde = oldSolde + solde;
compte.setSolde(newSolde);
}
@Override
public void debiter(Compte compte, int solde) throws RemoteException {
// TODO Auto-generated method stub
int oldSolde = compte.getSolde();
int newSolde = oldSolde - solde;
compte.setSolde(newSolde);
}
public void putCompte(String code, Compte c) {
listCompte.put(code, c);
}
@Override
public Compte getCompte(String code) throws RemoteException {
System.out.println(" code ");
return listCompte.get(code);
}
} |
Voici le code du serveur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
package fr.grosdim.bankaccount.server;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ServerBankAccount {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
IBankAccount bankAccount = new BankAccount();
try {
UnicastRemoteObject.exportObject(bankAccount,0);
Registry registry = LocateRegistry.createRegistry(15000);
//Registry registry = LocateRegistry.getRegistry("127.0.0.1");
registry.bind("bankAccount", bankAccount);
System.out.println( "Server BankAccount launched ....");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Voici le code du client Trudy :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
package fr.grosdim.bankaccount.client;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import fr.grosdim.bankaccount.bean.Compte;
import fr.grosdim.bankaccount.server.IBankAccount;
public class TrudyClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Registry registry;
try {
registry = LocateRegistry.getRegistry(15000);
IBankAccount bankAccount = (IBankAccount) registry
.lookup("bankAccount");
Compte compte = bankAccount.authentication("trudy");
if (compte == null) {
System.out.println(" User not found ");
}
bankAccount.crediter(compte, 10000);
Compte c = bankAccount.getCompte(compte.getResponsable().toLowerCase());
System.out.println(c.toString());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Code de la classe Compte
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
/**
*
*/
package fr.grosdim.bankaccount.bean;
import java.rmi.Remote;
/**
* @author dimitri
*
*/
public class Compte implements Remote {
/**
*
*/
private static final long serialVersionUID = 1L;
private String responsable;
private int solde;
public Compte() {
this("", 0);
}
public Compte(String responsable, int solde) {
super();
this.responsable = responsable;
this.solde = solde;
}
public String getResponsable() {
return responsable;
}
public void setResponsable(String responsable) {
this.responsable = responsable;
}
public int getSolde() {
return solde;
}
public void setSolde(int solde) {
this.solde = solde;
}
@Override
public String toString() {
return "Bonjour " + this.responsable + "\nVotre solde est de "
+ this.solde;
}
} |