0 Replies Latest reply on Sep 6, 2010 10:48 AM by M Arnold

    Only receive message when EAR is redeployed

    M Arnold Newbie

      Hello,

       

      I have an EJB application running in JBoss AS 5.1.  I'm trying to receive a JMS message in a stateless session EJB, and even though I can see messages on the queue using a QueueBrowser, I cannot receive messages using a QueueReceiver (or MessageConsumer).  The receive() method returns null.  Interestingly, when I redeploy my EAR, it does receive a single message, but only once, and any subsequent calls to the stateless session EJB do not receive any messages.

       

      Can anybody tell me if I am doing something outside of the JMS or EJB specs, or if this unexpected behaviour by JBoss?  Thanks.

       

      This is the code:

          

          @Resource(mappedName=GlobalConstants.JMS_QUEUE_FACTORY_JNDI)

          QueueConnectionFactory connFactory;

         

          @Resource(mappedName=GlobalConstants.EVENT_QUEUE_JNDI)

          Queue eventQueue;

         

          @Resource(mappedName=GlobalConstants.EVENT_ERROR_QUEUE_JNDI)

          private Queue errorQueue;   

         

          private QueueConnection eventQueueConnection;

          private QueueConnection errorEventQueueConnection;

         

          @PostConstruct

          public void initConnections() {

              try {

                  eventQueueConnection = connFactory.createQueueConnection();

                  errorEventQueueConnection = connFactory.createQueueConnection();

              } catch (JMSException e) {

                  throw new RuntimeException("Could not initialise JMS connection", e);

              }

          }

         

          @PreDestroy

          public void closeConnections() {

              try {

                  if (eventQueueConnection != null) {

                      eventQueueConnection.close();

                  }

              } catch (JMSException ignore) {

              } finally {

                  try {

                      if (errorEventQueueConnection != null) {

                          errorEventQueueConnection.close();

                      }

                  } catch (JMSException ignore) {

                  }

              }       

          }

       

      [snip]

       

          @TransactionAttribute(TransactionAttributeType.REQUIRED)

          @RolesAllowed(GlobalConstants.INTERNAL_ROLE_NAME)

          public boolean someMethod(String customerID) {

             

              QueueSession session = null;

              QueueReceiver receiver = null;

              MessageConsumer mc = null;

              QueueBrowser qb = null;

             

              try { 

                 

                  log.debug("Entering someMethod");

                  session = eventQueueConnection.createQueueSession(true, 0);

                 

                  qb = session.createBrowser(eventQueue);

                  Enumeration<Message> e = qb.getEnumeration();

                  while (e.hasMoreElements()) {

                      Message message = e.nextElement();

                      log.info("MESSAGE org = " + message.getStringProperty(GlobalConstants.JMS_ORGANISATION));

                  }           

                 

                  //receiver = session.createReceiver(eventQueue);

                  mc = session.createConsumer(eventQueue);

                  eventQueueConnection.start();

                  log.info("Receiving message from queue");

                  //Message message = receiver.receiveNoWait();

                  Message message = mc.receiveNoWait();

                 

                  if (message == null) {

                      log.info("No message waiting");

                      return false;

                  }

                  log.info("Process message");

                            

      [snip]

       

      I get this my log (there are six message in the queue, but gets null back from QueueReceiver?):

       

      Entering someMethod
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST

      Receiving message from queue
      No message waiting

       

      Then I redeploy my EAR and get (still six message, gets one from the QueueReceiver, as expected):

       

      Entering someMethod
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST

      Receiving message from queue
      Process message

       

      Second call gets this (now there are five messages, but gets null back from QueueReceiver, and will until it's deployed again):

       

      Entering someMethod
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST
      MESSAGE org = TEST

      Receiving message from queue
      No message waiting