[2.1] EJB Driven Message et TimedObject
bonjour,
j'essaye d'implémenter un petit exemple de MD EJB (en 2.1) trouvé dans ma bibliothèque ( -> J2EE 1.4 chap 11):
En gros j'ai un ejbSession qui instancie un timer qui m'envoie 5 secondes apres le démarrage (puis toutes les 10 secondes) un message. J'ai un MD EJB de l'autre coté qui est censé recevoir et m'afficher dans ma console le message envoyé.
mon ejb session :
Code:
1 2 3
| public interface TimeIt extends EJBObject {
public void startTimer() throws RemoteException;
} |
Code:
1 2 3
| public interface TimeItHome extends EJBHome {
public TimeIt create() throws CreateException, RemoteException;
} |
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 59 60 61
| public class TimeItBean implements SessionBean, TimedObject {
public void ejbCreate() throws CreateException {
System.out.println("ejbCreate");
}
private SessionContext ctx;
public void startTimer() {
System.out.println("start Timer");
TimerService timerService = ctx.getTimerService();
Timer timer = timerService.createTimer(5000, 10000, "timer");
}
public void ejbActivate() throws EJBException {
System.out.println("ejbActivate");
}
public void ejbPassivate() throws EJBException {
System.out.println("ejbPassivate");
}
public void ejbRemove() throws EJBException {
System.out.println("ejbRemove");
}
public void setSessionContext(SessionContext sessionContext) throws EJBException {
System.out.println("setContext");
ctx = sessionContext;
}
public void ejbTimeout(Timer timer) {
System.out.println("ejbTimeout");
QueueConnection queueConnection = null;
try {
InitialContext jndiContext = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("jms/QueueConnectionFactory");
Queue queue = (Queue) jndiContext.lookup("jms/LogWriterQueue");
queueConnection = queueConnectionFactory.createQueueConnection();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender = queueSession.createSender(queue);
TextMessage message = queueSession.createTextMessage();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd 'a' HH:mm:ss.SSS");
message.setText("entree log, date heure : " + sdf.format(new Date()));
queueSender.send(message);
}
catch (Exception e) {
System.out.println("Exception dans le msg : " + e.toString());
e.printStackTrace();
}
finally {
if (queueConnection != null) {
try {
queueConnection.close();
}
catch (Exception e) {
}
}
}
}
} |
et mon ejb Message Driven :
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
| public class MessageWriterBean implements MessageDrivenBean, MessageListener {
public void onMessage(Message message) {
System.out.println("onMessage");
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
System.out.println("Message reçu : " + msg.getText());
} else {
System.out.println("Message reçu de type : "
+ message.getClass().getName() + " ==> ignore !");
}
}
catch (Throwable te) {
te.printStackTrace();
}
}
public void ejbCreate() {}
public void ejbRemove() {}
public void setMessageDrivenContext(MessageDrivenContext mdContext) {}
} |
Or je n'arrive pas a faire mes fichiers de configurations pour mon MessageWriterBean:
ejb-xml.jar:
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
| <ejb-jar>
<enterprise-beans>
<session>
<ejb-name>TimeItBean</ejb-name>
<home>bean.timer.TimeItHome</home>
<remote>bean.timer.TimeIt</remote>
<ejb-class>bean.timer.TimeItBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
<message-driven>
<ejb-name>MessageWriterBeanEJB</ejb-name>
<ejb-class>bean.msg.MessageWriterBean</ejb-class>
<transaction-type>Container</transaction-type>
<resource-ref>
<res-ref-name>QueueConnectionFactory</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>PhysicalQueue</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</message-driven>
</enterprise-beans>
</ejb-jar> |
mon jboss.xml :
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
| <jboss>
<enterprise-beans>
<session>
<ejb-name>TimeItBean</ejb-name>
<jndi-name>ejb/timer.TimeIt</jndi-name>
</session>
<message-driven>
<ejb-name>MessageWriterBeanEJB</ejb-name>
<destination-jndi-name>jms/LogWriterQueue</destination-jndi-name>
<resource-ref>
<res-ref-name>QueueConnectionFactory</res-ref-name>
<jndi-name>jms/QueueConnectionFactory</jndi-name>
</resource-ref>
<resource-ref>
<res-ref-name>PhysicalQueue</res-ref-name>
<jndi-name>jms/LogWriterQueue</jndi-name>
</resource-ref>
</message-driven>
</enterprise-beans>
</jboss> |
et j'ai un problème de jndi quelque part... sauf que je ne sais pas ou ...
quelqu'un aurai une idée ?
:merci: d'avance !