3 Replies Latest reply on Aug 9, 2005 5:10 PM by kvemuri

    MDB not consuming messages

    kvemuri

      Alrighty I am stumped with the issue here...I have a normal java class as a MessageProducer which sends messages to a Queue and I have an MDB as the message consumer which is listening to the above Queue.

      The Producer successfully sends messages to this Queue..but the MDB isn't consuming them. The most strangest part is I have a System println set in the MDB's constructor and that is not being printed on the Jboss server console. The funny part is the console shows the MDB to be deployed but doesn't show the System println's either in the constructor or the ejbCreate methods. I am practically stumped by this. The messages can be seen on the testQueue via the jmx-console but the messages are not getting consumed. Any help here would be appreciated.

      I am using JBoss-4.0.2 and here's a piece of the Snippet of the MDB code and the associated xml files and yes i have set the jbossmq-destinations-service.xml too

      //MDB Code
      public class MessageConsumerBean implements MessageDrivenBean, MessageListener {

      /**
      *
      * Constructor
      *
      */
      public MessageConsumerBean() {
      System.out.println("The MessageConsumerBean is : " + hashCode());
      }



      /**
      *
      * @param ctx
      * @throws javax.ejb.EJBException
      *
      */
      public void setMessageDrivenContext(MessageDrivenContext ctx)
      throws EJBException {
      this.ctx = ctx;
      }

      //ejb create method
      public void ejbCreate() {
      System.out.println("The MDB MessageConsumerBean instantiated.....");
      setPTP();
      }


      private void setPTP() {


      try {
      connection = getConnection();
      } catch(SystemException sysEx) {
      logger.error("Exception occured while getting the QueueConnection", sysEx);
      }

      //create a queue session
      try {
      session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      } catch(JMSException jmsEx) {
      logger.error("JMSException occured while creating a queue receiver.", jmsEx);
      }

      //starts the JMS connection.
      try {
      connection.start();
      } catch(JMSException jmsEx) {
      logger.error("JMSException occured while starting a JMS Connection.", jmsEx);
      }


      }


      //onMessage
      public void onMessage(Message message) {
      System.out.println("Executing the onMessage method....." + message);
      //--- etc
      }

      //getting the connection
      //Returns a JMS QueueConnection object
      private QueueConnection getConnection() throws SystemException {
      Context ctx = null;

      try {
      ctx = new InitialContext();
      factory = (QueueConnectionFactory)ctx.lookup(CONNECTION_FACTORY_REF);
      connection = factory.createQueueConnection();
      System.out.println("The queue connection is.... " + connection);
      } catch(NamingException nameEx) {
      throw new SystemException("NamingException occured in getting a QueueConnection", nameEx);
      } catch(JMSException jmsEx) {
      throw new SystemException("JMSException occured in getting a QueueConnection", jmsEx);
      }

      return connection;
      }

      public void ejbRemove() {
      System.out.println("The MessageConsumerBean ejbRemove...");
      ctx = null;

      //closing the QueueSession
      if (session != null) {
      try {
      session.close();
      } catch(Exception ex) {
      logger.error("Exception while closing the session");
      }
      }

      //closing the QueueConnection
      if (connection != null) {
      try {
      connection.close();
      } catch(Exception ex) {
      logger.error("Exception while closing the connection");
      }
      }
      }
      }

      //ejb-xml

      <?xml version="1.0" encoding="UTF-8"?>

      <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">

      <display-name>JMS Message Driven Bean</display-name>

      <enterprise-beans>
      <message-driven>
      <ejb-name>JMSMessageConsumerEJB</ejb-name>
      <ejb-class>com.test.messaging.MessageConsumerBean</ejb-class>
      <transaction-type>Container</transaction-type>
      <message-selector>MessageFormat = 'Version 3.4'</message-selector>
      <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
      <message-driven-destination>
      <destination-type>javax.jms.Queue</destination-type>
      </message-driven-destination>
      <resource-ref>
      <res-ref-name>java:JmsXA</res-ref-name>
      <res-type>javax.jms.QueueConnectionFactory</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>
      </message-driven>
      </enterprise-beans>
      </ejb-jar>


      //jboss.xml
      <!DOCTYPE jboss PUBLIC
      "-//JBoss//DTD JBOSS 4.0//EN"
      "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">

      <enterprise-beans>
      <message-driven>
      <ejb-name>JMSMessageConsumerEJB</ejb-name>
      <destination-jndi-name>queue/java.jms.TestQueue</destination-jndi-name>
      <resource-ref>
      <res-ref-name>java:JmsXA</res-ref-name>
      <jndi-name>java:/ConnectionFactory</jndi-name>
      </resource-ref>
      </message-driven>
      </enterprise-beans>



      //entry in the jbossmq-destinations-service.xml

      ----

      <!-- Test JMS Queues -->

      <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager


      //Console output for ejb deployment is
      02:51:15,845 INFO [EjbModule] Deploying JMSMessageConsumerEJB
      02:51:21,002 INFO [EJBDeployer] Deployed: file:/C:/jboss-4.0.2/server/default/d
      eploy/test.ear/testmdb.jar

        • 1. Re: MDB not consuming messages
          kvemuri

          The entry in the jbossmq-destinations-service.xml didn't get pasted properly in my above post..hence repasting that entry only.

          <!-- Test JMS Queues -->

          <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=java.jms.TestQueue">
           <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
           </mbean>
          


          • 2. Re: MDB not consuming messages
            jaikiran

            Can you post the code, where you are sending the message. And is the part of sending the message done in a transaction(may be from some EJB)

            • 3. Re: MDB not consuming messages
              kvemuri

              Sure heres's the sender code

              public class JMSService {
              
              
               public static final String QUEUE_CONN_FACTORY_NAME = "java:/ConnectionFactory";
               public static final String EMAIL_QUEUE_NAME = "queue/java.jms.TestQueue";
              
               private QueueConnection connection = null;
               private QueueConnectionFactory factory = null;
               private Queue queue = null;
              
               private static final Logger logger = Logger.getLogger(JMSService.class);
              
               /**
               *
               * Constructor
               *
               * @param The jmsQueue JNDI name
               * that to which the message needs to be
               * sent
               *
               */
               public JMSService(String queueName) {
              
               //Get queue connection
               try {
               connection = getConnection();
               } catch(SystemException sysEx) {
               logger.error("Exception occured trying to get a queue connection.", sysEx);
               }
              
               //Get queue
               try {
               queue = getQueue(queueName);
               } catch(SystemException sysEx) {
               logger.error("Exception occured while getting a queue with name: " + queueName, sysEx);
               }
               }
              
              
               /**
               *
               * Sends a test message to the JMS queue defined
               * by the QUEUE_NAME. Collects the messages from
               * the Collection and creates a String message and
               * sends out the message as a text message.
               *
               * @param messages Collection of messages that
               * needs to be sent out to the queue.
               * @return none
               * @throws SystemException
               *
               */
               public void send(HashMap messages) throws SystemException {
               QueueSession session = null;
               QueueSender sender = null;
               MapMessage message = null;
               StringBuffer msgBuffer = null;
              
               if (messages == null || messages.size() <= 0) {
               throw new SystemException("The messages collection is null or 0");
               }
              
               if (connection == null) {
               throw new SystemException("The QueueConnection is null.");
               }
              
               if (queue == null) {
               throw new SystemException("The Queue is null.");
               }
              
               try {
               session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
               sender = session.createSender(queue);
               message = session.createMapMessage();
              
               if (queue.getQueueName().equalsIgnoreCase(EMAIL_QUEUE_NAME)) {
               createEmailMessage(messages, message);
               }
               System.out.println("Sending message to queue..." + sender.getQueue().getQueueName());
               sender.send(message);
               System.out.println("Message sent to queue successfully...");
               } catch(JMSException jmsEx) {
               throw new SystemException("JMSException occured while trying to send a message", jmsEx);
               } finally {
               if (sender != null) {
               try {
               sender.close();
               } catch(JMSException jmsEx) {
               throw new SystemException("JMSException occured while trying to close the sender", jmsEx);
               }
               }
               }
              
               }
              
               //Returns a JMS QueueConnection object
               private QueueConnection getConnection() throws SystemException {
               Context ctx = null;
              
               try {
               ctx = new InitialContext();
               factory = (QueueConnectionFactory)ctx.lookup(QUEUE_CONN_FACTORY_NAME);
               connection = factory.createQueueConnection();
               } catch(NamingException nameEx) {
               throw new SystemException("NamingException occured in getting a QueueConnection", nameEx);
               } catch(JMSException jmsEx) {
               throw new SystemException("JMSException occured in getting a QueueConnection", jmsEx);
               }
              
               return connection;
               }
              
               //Returns a JMS Queue
               private Queue getQueue(String queueName) throws SystemException {
               Context ctx = null;
              
               try {
               ctx = new InitialContext();
               queue = (Queue)ctx.lookup(queueName);
               } catch(NamingException sysEx) {
               throw new SystemException("NamingException occured in getting a Queue", sysEx);
               }
              
               return queue;
              
               }
              
               //Creates an email message
               private void createEmailMessage(HashMap messages, MapMessage message)
               throws SystemException {
              
               int length = messages.size();
              
               try {
              
               for(int i=0; i < length; i++) {
               message.setStringProperty("MessageFormat", "Version 3.4");
               message.setString(ConstantsIF.TO_EMAIL_ADDRESS, (String)messages.get(ConstantsIF.TO_EMAIL_ADDRESS));
               message.setString(ConstantsIF.EMAIL_SUBJECT, (String)messages.get(ConstantsIF.EMAIL_SUBJECT));
               message.setString(ConstantsIF.EMAIL_MESSAGE, (String)messages.get(ConstantsIF.EMAIL_MESSAGE));
              
              
               //if the person id is set in the messages then set the that to the message
               if (messages.get(ConstantsIF.EMAIL_PERSON_ID) != null) {
               message.setInt(ConstantsIF.EMAIL_PERSON_ID, ((Integer)messages.get(ConstantsIF.EMAIL_PERSON_ID)).intValue());
               }
               }
               } catch(JMSException jmsEx) {
               throw new SystemException("JMSException occured at the time of creating an email message", jmsEx);
               }
               }
              
              }
              


              thats the sender code.