4 Replies Latest reply on Mar 23, 2007 3:30 PM by vitor_b

    Queue - I can send but cannot receive

    vitor_b

      Hello

      JBoss: 4.0.5 GA

      I'm still facing some problems working with JMS.
      The last problem is that i cannot receive message from queue. I can send it, but cannot receive it later. All from mothod inside simle stateless session bean. This method works in a transaction (Container, Required).

      First getting stuff

      InitialContext ictx = new InitialContext();
       ConnectionFactory cf = (ConnectionFactory) ictx.lookup("java:JmsXA");
       Connection con = cf.createConnection();
       Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
      
       Queue queue = (Queue ) ictx.lookup("queue/testQueue");
      
       logger.info(queue.getQueueName());


      Now we can perform some operations on queue:
      First we can send message:

      MessageProducer sender = session.createProducer(queue);
      
       Order order = new Order();
       order.setOrderId("orderId");
      
       Message message = session.createObjectMessage(order);
      
       sender.send(message);
       logger.info("Message has been sent");


      Sending messages works fine.
      Now i would like to receive one message:

      MessageConsumer consumer = session.createConsumer(queue);
       Message m = consumer.receiveNoWait();
      
       if (m == null){
       logger.error("m is null");
       }else{
       logger.info("got message, yupi");
       }


      But I cannot get message from queue "testQueue". Everytime single time i get null. Queue depth is huge now after so many tries :(

      I deployed MDB, and that MDB was able to consume messages from this queue. I have no idea what i'm doing wrong. Any sugestions?

      Thank you for all your replies in advance.

      vitor_b

        • 1. Re: Queue - I can send but cannot receive
          weston.price

          To do a receive you will need to call start() on the connection. Otherwise, messages will not be delivered. The reason the MDB works is because this is done automagically under the hood for you.

          • 2. Re: Queue - I can send but cannot receive
            vitor_b

            Thank you for this helpful reply.

            I added method start() at the begining and stop() method at the end. But that was not a good move. I received Exception:


            javax.jms.IllegalStateException: This method is not applicable inside the application server. See the J2EE spec, e.g. J2EE1.4 Section 6.6


            When i removed method stop() everything works fine.
            consumer.close();
             session.close();
             //con.stop();
             con.close();
             ictx.close();


            Thanks again. I would have lost much more time if i had not been told what is wrong.

            Best regards.

            vitor_b

            • 3. Re: Queue - I can send but cannot receive
              genman

              What happens with Connection.start() ?

              • 4. Re: Queue - I can send but cannot receive
                vitor_b

                With connection.start() everything works fine.

                Connection con = cf.createConnection();
                 con.start(); //new line
                 Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);


                I had some problems when i added connection.stop() method to my code.
                So i removed stop() method call, and again everything works corectly.

                Best regards
                vitor_b