2 Replies Latest reply on Apr 29, 2009 1:56 PM by idhira

    Problems with MDBs in JBoss 5.0

    Archimedes Fagundes Junior Newbie

      Hi,

      I'm starting with JBoss and having some problems with Message Driven Beans. I have a Messenger class that put a message in a queue. It works normally.

      package com.jboss.ejb.teste.message;
      
      import javax.jms.Connection;
      import javax.jms.Destination;
      import javax.jms.MessageProducer;
      import javax.jms.ObjectMessage;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.Session;
      import javax.naming.InitialContext;
      
      public class Messenger {
      
       private QueueConnectionFactory connectionFactory;
       private Destination destination;
      
       public Messenger() {
       try {
       InitialContext ctx = new InitialContext();
      
       connectionFactory = (QueueConnectionFactory) ctx.lookup("java:/ConnectionFactory");
       destination = (Destination) ctx.lookup("queue/JBossTesteQueue");
       }
       catch (Exception exc) {
       exc.printStackTrace();
       }
       }
      
       public void sendMessage(String nome) {
       try {
       Connection connection = connectionFactory.createQueueConnection();
       Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
      
       MessageProducer producer = session.createProducer(destination);
      
       ObjectMessage message = session.createObjectMessage();
       message.setObject(nome);
      
       producer.send(message);
      
       session.close();
       connection.close();
      
       System.out.println("Mensagem enviada com sucesso!");
       }
       catch (Exception exc) {
       exc.printStackTrace();
       }
       }
      
      }


      And have this MDB, but it doesn't run. I've already tried everything i could but no success. No Exceptions, the deploy process is OK. It simply isn't called by the JBoss when i put a messege in the queue.

      package com.jboss.ejb.teste.message;
      
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      
      import javax.annotation.Resource;
      import javax.ejb.ActivationConfigProperty;
      import javax.ejb.MessageDriven;
      import javax.ejb.MessageDrivenContext;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.ObjectMessage;
      
      @MessageDriven(
       name = "MessageConsumer",
       activationConfig = {
       @ActivationConfigProperty(
       propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
       @ActivationConfigProperty(
       propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
       @ActivationConfigProperty(
       propertyName = "destination", propertyValue = "queue/JBossTesteQueue")
       }
       )
      public class MessageConsumer implements MessageListener {
      
       @Resource
       private MessageDrivenContext context;
      
       public void onMessage(Message message) {
       try {
       ObjectMessage objectMessage = (ObjectMessage) message;
      
       String time = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss").format(new Date());
       System.out.println("Mensagem recebida em " + time);
       System.out.println("Content: " + objectMessage.getObject());
      
      
       Connection conn = null;
       PreparedStatement ps = null;
      
       try {
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager.getConnection("jdbc:mysql://localhost/JBossEJBTeste", "root", "admin");
      
       ps = conn.prepareStatement("insert into teste_table (descricao) values ?");
       ps.setString(1, objectMessage.getObject() + " - " + time);
      
       ps.execute();
       }
       catch (Exception exc) {
       exc.printStackTrace();
       }
       finally {
       try {
       ps.close();
       conn.close();
       }
       catch (Exception exc) {}
       }
      
       }
       catch (JMSException exc) {
       exc.printStackTrace();
       context.setRollbackOnly();
       }
       }
      
      }


      Here is the ejb-jar.xml file:

      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
       <display-name>JBossEJBTeste_ejb</display-name>
       <enterprise-beans>
       <message-driven>
       <ejb-name>MessageConsumer</ejb-name>
       <ejb-class>com.jboss.ejb.teste.message.MessageConsumer</ejb-class>
       <transaction-type>Container</transaction-type>
       <message-destination-type>javax.jms.Queue</message-destination-type>
       </message-driven>
       </enterprise-beans>
      </ejb-jar>
      
      And here is the jboss.xml file:
      
      <?xml version="1.0" encoding="UTF-8"?>
      
      <jboss>
       <enterprise-beans>
       <message-driven>
       <ejb-name>MessageConsumer</ejb-name>
       <configuration-name>Standard Message Driven Bean</configuration-name>
       <destination-jndi-name>queue/JBossTesteQueue</destination-jndi-name>
       <activation-config>
       <activation-config-property>
       <activation-config-property-name>destinationType</activation-config-property-name>
       <activation-config-property-value>javax.jms.Queue</activation-config-property-value>
       </activation-config-property>
       </activation-config>
       </message-driven>
       </enterprise-beans>
      </jboss>
      
      
      Could someone help me?
      
      Perhaps, i'm sorry about my english, i'm brazilian... but i think it can be read =)