Message Driven Bean avec JBoss
Bonjour
j'ai crée un MDB pour gérer une notification pour mon client lorsqu'il effectu une tâche critique;
voilà le code que je suis entrain de tester :
lorsque je lance le consomateur j'ai ce message d'erreur :
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
| Exception in thread "main" javax.naming.NameNotFoundException: topic not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
at org.jnp.server.NamingServer.lookup(NamingServer.java:396)
at sun.reflect.GeneratedMethodAccessor835.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:688)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.soutem.app.MailConfirmationConsommateur.<init>(MailConfirmationConsommateur.java:42)
at com.soutem.app.MailConfirmationConsommateur.main(MailConfirmationConsommateur.java:23) |
est ce qu'il ya une façon pour vérifier si ce topic est bien deployer , sinon est ce qu'il y-a une config coté 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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
@MessageDriven(mappedName = "topic/MailConfirmationMdbTopic",
activationConfig = {
// @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
// @ActivationConfigProperty(propertyName="subscriptionDurability", propertyValue="Durable"),
//@ActivationConfigProperty(propertyName="subscriptionName", propertyValue="topicmdb"),
//@ActivationConfigProperty(propertyName="clientId", propertyValue="mdbtopic-test"),
//
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/MailConfirmationMdbTopic")
})
public class MailConfirmationMdbBean implements MessageListener {
private static final Logger log = Logger.getLogger(MailConfirmationMdbBean.class);
// @Resource MessageDrivenContext mdc;
public MailConfirmationMdbBean(){
log.info("Initialisation de l'envoi du mail depuis MailConfirmationMdbBean");
}
public void onMessage(Message message) { //Message de javax.jms.Message
// Pour la classe de test MailConfirmationProducteur
if (message instanceof TextMessage) {
TextMessage mail = (TextMessage) message;
// L'envoi d'un mail de confirmation au client est ici simulé par l'affichage d'un message au niveau des logs.
try {
String leMail = mail.getText();
log.info(" Envoi du mail : " + leMail);
log.info(" --------------------------------------------------- ");
sendMsg("ahmed.drira@gmail.com@gmail.com", "Confirmation de commande.", leMail);
log.info(" --------------------------------------------------- ");
log.info(" Mail envoyé.");
}
catch (JMSException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (message instanceof ObjectMessage) {
ObjectMessage lemessage = (ObjectMessage) message;
}
}
protected void sendMsg(String email, String subject, String body) throws MessagingException, NamingException, UnsupportedEncodingException {
Properties props = new Properties();
InitialContext ictx = new InitialContext(props);
javax.mail.Session mailSession = (javax.mail.Session) ictx.lookup("java:/Mail");
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(email, false));
message.setText(body);
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
try {
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
log.info("Message envoyé");
}
finally {
transport.close();
}
}
@PreDestroy
public void remove() {
log.info("Suppression de MailConfirmationMdbBean.");
}
} |
le déploiement n'indique aucune erreur
pour tester j'ai ces 2 bout de code , un cosomateur et un producteur :
les voilà :
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
|
public class MailConfirmationConsommateur implements MessageListener {
public static void main(String[] args) throws Exception {
new MailConfirmationConsommateur();
}
public MailConfirmationConsommateur() throws Exception {
Properties proprietes = new Properties();
proprietes.load(new FileInputStream("jndi.properties"));
InitialContext ctx = new InitialContext(proprietes);
// 1: recherche d'une connection factory
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
// 2: cr�ation d'une connection JMS
Connection conn = factory.createConnection();
// 3: cr�ation d'une session
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 4. Recherche d'une destination
Topic topic = (Topic) ctx.lookup("topic/MailConfirmationMdbTopic");
// 5: cr�ation d'un consommateur de message
MessageConsumer consommateur = session.createConsumer(topic);
consommateur.setMessageListener(this);
System.out.println("Client JMS MailConfirmationConsommateur � l'�coute de messages.");
conn.start(); //
}
public void onMessage(Message msg) {
if (msg instanceof TextMessage) {
TextMessage tm = (TextMessage) msg;
// L'envoi d'un mail de confirmation au client est ici simul�
// par l'affichage d'un message au niveau des logs.
try {
String mail = tm.getText();
System.out.println("Le client JMS MailConfirmationConsommateur a re�u le message : " + mail);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
@PreDestroy
public void remove() {
System.out.println("Suppression du client JMS MailConfirmationConsommateur.");
}
} |
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
|
public class MailConfirmationProducteur {
private static final Logger log = Logger.getLogger(MailConfirmationProducteur.class);
public static void main(String[] args) throws Exception {
Properties proprietes = new Properties();
proprietes.load(new FileInputStream("jndi.properties"));
InitialContext ctx = new InitialContext(proprietes);
// 1: recherche d'une connection factory
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
// 2: cr�ation d'une connection JMS
Connection conn = factory.createConnection();
// 3: cr�ation d'une session
Session session = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);
// 4: Recherche d'une destination
Topic topic = (Topic) ctx.lookup("topic/MailConfirmationMdbTopic");
// 5: cr�ation d'un producteur de message
MessageProducer producteur = session.createProducer(topic);
// 6: publication d'un message
TextMessage msg = session.createTextMessage();
msg.setText("Mail de confirmation pour le client.");
producteur.send(msg);
producteur.close();
log.info("Message envoy�.");
}
} |