I am using Glassfish JMS.

I am able to add messages to a queue. I can see the messages using the QueueBrowser object. However the MessageConsumer (nor the QueueReceiver) cannot receice any message and return null. Message expiration is set to 0 and I remember to open the connection.

Any ideas?

Here is the code:

      Session session = null;
      Connection conn = null;

         InitialContext jndi = new InitialContext();

            ConnectionFactory qFactory = (ConnectionFactory)jndi.
              conn = qFactory.createConnection();
         Queue queue = (Queue)jndi.lookup("myQueueName");
         session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

          QueueReceiver  mc = ((QueueSession)session).createReceiver(queue);

         Object  m = mc.receive(2000);
         //m is NULL!

         QueueBrowser browser = session.createBrowser(queue);

         for(Enumeration e = browser.getEnumeration(); e.hasMoreElements(); )
             //there are many messages here...
+1  A: 

That would be good to have the client code.

Similar thing happened to me when not properly committing/closing the connection on the sender side. The message would be visible when using the admin console, however, not available yet to the MDB.

Hope it helps.


Does this code run in the appserver? If it does, I'd obtain the required objects via annotations, and for a message receiver I'd use a MDB. If this is a piece of standalone code, I had a hell of a time getting a JNDI based client working, I reverted to using the "raw" Java API.