Stateful Bean appelle une méthode d'un Stateless Bean
Bonjour,
je souhaiterais qu'une méthode de mon Stateless Bean soit appelée par le Stateful Bean.
Voici les différents fichiers du Stateful bean:
SentenceHome.java
Code:
1 2 3 4 5 6 7 8 9 10 11
| package beans;
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
public interface SentenceHome extends EJBHome {
// Méthode permettant de créer un bean Sentence
public Sentence create()
throws CreateException, RemoteException;
} |
Sentence.java
Code:
1 2 3 4 5 6 7 8 9 10 11
| package beans;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Sentence extends EJBObject {
// Méthodes publiques du bean Sentence
public void createSentence(String clientString)
throws RemoteException;
public String getSentence() throws RemoteException;
} |
SentenceBean.java
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
| package beans;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class SentenceBean implements SessionBean {
// Phrase complète
private String _Sentence = "";
// Méthodes publiques du bean Sentence. Ces méthodes
// doivent également être déclarées dans l'interface.
// Concaténation de la phrase
public void createSentence(String clientString) {
// On ajoute le mot
_Sentence = _Sentence + " " + clientString;
}
// Retourner la phrase
public String getSentence() {
return _Sentence;
}
//Méthodes EJB standards
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void ejbCreate() {}
public void setSessionContext(SessionContext context) {}
} |
Voici les différents fichiers du Stateless bean:
CountCharactersHome.java
Code:
1 2 3 4 5 6 7 8 9 10 11
| package beans;
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
public interface CountCharactersHome extends EJBHome {
// Méthode permettant de créer un bean CountCharacter
public CountCharacters create()
throws CreateException, RemoteException;
} |
CountCharacters.java
Code:
1 2 3 4 5 6 7 8 9 10
| package beans;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface CountCharacters extends EJBObject {
// L'unique méthode publique du bean CountCharacters
public int countChar(String clientString)
throws RemoteException;
} |
CountCharactersBean.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package beans;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class CountCharactersBean implements SessionBean {
// Implémentation des méthodes publiques. Ces méthodes
// doivent également être présentes dans l'interface bean.
public int countChar(String clientString) {
int length = clientString.length();
return length;
}
// Méthodes EJB standards
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void ejbCreate() {}
public void setSessionContext(SessionContext context) {}
} |
Le code du client:
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
| package client;
import beans.Sentence;
import beans.SentenceHome;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class SentenceClient {
public static void main(String[] args) {
try {
// Obtention d'un contexte
InitialContext jndiContext = new InitialContext();
// Obtention d'une référence à l'entrée JNDI Sentence
Object ref = jndiContext.lookup("ejb/beans.Sentence");
// Obtention d'une référence à l'interface home du bean
SentenceHome home = (SentenceHome) PortableRemoteObject.narrow(ref, SentenceHome.class);
// Création d'un objet Sentence à partir de l'interface home
Sentence sentence = home.create();
//Traitement de la phrase
for (int i = 0; i < args.length; i++) {
sentence.createSentence(args[i]);
String returnedSentence = sentence.getSentence();
System.out.println("Phrase apres le mot numero " + (i + 1) + ": " + returnedSentence);
}
} catch(Exception e) {
e.printStackTrace();
}
}
} |
Je voudrais savoir comment dans le code client je peux appeler une méthode de mon bean Stateless, ici CountCharacters, à partir du Bean Sentence (Bean Stateful). J'avoue que je ne vois pas bien ce qu'il faut faire, si quelqu'un a une idée je suis preneur.
Merci d'avance.
mumu27!