Yaron Naveh
Yaron Naveh

Reputation: 24406

JMS client does not receive messages

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;

      try
      {
         InitialContext jndi = new InitialContext();

            ConnectionFactory qFactory = (ConnectionFactory)jndi.
                lookup("myConnectionFactory");
              conn = qFactory.createConnection();
              conn.start();
         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...
         }

Upvotes: 1

Views: 3044

Answers (3)

digital illusion
digital illusion

Reputation: 497

I witnessed the same behavior happening after the first session commit, meaning that before the messages where received correctly. In my case the issue was that I was re-creating the receiver while keeping the same session. As pointed out in this article:

Creating temporary destinations, consumers, producers and connections are all synchronous request-response operations with the broker and so should be avoided for processing each request as it results in lots of chat with the JMS broker.

The solution was as simple as reusing the same receiver.

Upvotes: 0

fvu
fvu

Reputation: 32953

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.

Upvotes: 0

vdr
vdr

Reputation: 988

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.

Upvotes: 1

Related Questions