14 Replies Latest reply on Aug 27, 2003 8:17 AM by Markus Härnvi

    Simple JMS Queue Question

    sanjewad Newbie

      Hi
      I am using Jboss 3.2 and I have deployed a Basic JMS example.
      I have noticed both my QueueConnection Factory and Queue lokk up work fine.
      But When I run the Cleint I don't get a response from MDB. Can anybody say why?
      ---------------------------
      Here is the complete code.

      Client code
      -------------------
      QueueConnection connection = null;
      QueueSession session = null;
      try
      {
      Context ctx =new InitialContext();

      log.info("Looking up connection");
      QueueConnectionFactory conFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
      connection = (QueueConnection) conFactory.createQueueConnection();

      log.info("Looking up queue");
      Queue queue = (Queue) ctx.lookup("queue/testQueue");

      log.info("Creating session");
      session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      log.info("Creating message");
      TextMessage message = session.createTextMessage(msg);
      log.info("Creating sender");
      QueueSender sender = session.createSender(queue);
      log.info("Sending message");
      sender.send(queue, message);
      log.info("Closing sender");
      // connection.start();
      /* sender.close();

      connection.start();
      QueueReceiver receiver = session.createReceiver(queue);
      System.out.print("Received Message is :" + receiver.receive());

      log.info("Done");
      connection.close();
      */
      }
      catch(JMSException je){}
      catch(NamingException ne){}
      finally
      {
      try {if (session!= null)
      session.close();
      if (connection != null)
      connection.close();
      }
      catch(JMSException je){}

      }

      Message Driven Bean Code
      -------------------------------------
      public class TestMDBBean implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
      MessageDrivenContext ctx;

      public void onMessage(Message message)
      {


      TextMessage msg =null;

      try {
      if (message instanceof TextMessage){
      msg =(TextMessage)message;
      System.out.println("MESSAGE BEAN:Message "+"received:"+msg.getText());
      }else
      {
      System.out.println("Message of wrong type:"+ message.getClass().getName());
      }
      }catch (JMSException e){
      System.err.println("MessageBean.onMessage:"+"JMSException:"+e.toString());
      ctx.setRollbackOnly();
      }catch (Throwable te){
      System.err.println("MessageBean.onMessage:"+"Exception:"+te.toString());
      }





      /*

      System.out.println("This is MDB:"+message);
      try{

      InitialContext ctx = new InitialContext();
      LocalCartHome cartHome = (LocalCartHome)ctx.lookup("Cart");
      LocalCart cart = cartHome.create();
      cart.setMessage(message.toString());
      }catch (Exception e) {

      throw new EJBException(e);
      }
      */
      }

      public void ejbCreate()
      {
      }

      public void ejbRemove()
      {
      }

      public void setMessageDrivenContext(MessageDrivenContext ctx)
      {
      this.ctx = ctx;
      // this.onMessage(msg);
      }

      }

      ----------------------------------
      ejb-jar.xml code
      -------------
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
      <ejb-jar>
      <display-name>EJBModule_TestMDB</display-name>
      <enterprise-beans>
      <message-driven>
      <display-name>TestMDB</display-name>
      <ejb-name>TestMDB</ejb-name>
      <ejb-class>first.TestMDBBean</ejb-class>
      <transaction-type>Container</transaction-type>
      <message-driven-destination>
      <destination-type>javax.jms.Queue</destination-type>
      </message-driven-destination>
      </message-driven>
      </enterprise-beans>
      <assembly-descriptor>
      <container-transaction>
      This value was set as a default by Sun ONE Studio.

      <ejb-name>TestMDB</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>NotSupported</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>
      ------------------------------

      jboss.xml
      ---------------------
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN" "http://www.jboss.org/j2ee/dtd/jboss.dtd">

      <enterprise-beans>
      <message-driven>
      <ejb-name>TestMDB</ejb-name>
      <destination-jndi-name>queue/testQueue</destination-jndi-name>
      </message-driven>
      </enterprise-beans>

      -----------------------------
      Output from the Application Server
      --------------------------
      12:01:41,531 INFO [STDOUT] Looking up connection
      12:01:41,609 INFO [STDOUT] Looking up queue
      12:01:41,625 INFO [STDOUT] Creating session
      12:01:41,625 INFO [STDOUT] Creating message
      12:01:41,625 INFO [STDOUT] Creating sender
      12:01:41,625 INFO [STDOUT] Sending message
      12:01:41,640 INFO [STDOUT] Closing sender

      ------------------------------

      This is only the client output.
      How do I know it is delivered to my MDB because I don't get any response from Message Driven Bean.
      Am I making a silly mistake?






        • 1. Re: Simple JMS Queue Question
          Adrian Brock Master

          You should set JMSReplyTo on your message and
          get the MDB to send the reply to that queue.

          Regards,
          Adrian

          • 2. Re: Simple JMS Queue Question
            sanjewad Newbie

            Hi Adrian
            I am getting an output something like
            ----------------------------------------------------
            12:50:07,062 INFO [STDOUT] Closing sender
            12:50:07,093 INFO [STDOUT] i am here org.jboss.mq.SpyTextMessage {
            Header {
            jmsDestination : QUEUE.testQueue
            jmsDeliveryMode : 2
            jmsExpiration : 0
            jmsPriority : 4
            jmsMessageID : ID:12-10613622070311
            jmsTimeStamp : 1061362207031
            jmsCorrelationID: null
            jmsReplyTo : QUEUE.testQueue
            jmsType : null
            jmsRedelivered : false
            jmsPropertiesReadWrite:false
            msgReadOnly : true
            producerClientId: ID:12
            }
            Body {
            text :Hey I am working now
            }
            }
            ---------------------------------------
            Still I can't see any output from the Message Driven Been as I have set some Output onMessage Method.

            Can you figure this out please?

            Thanks

            • 3. Re: Simple JMS Queue Question
              Adrian Brock Master

              Can you try this example, then tell me what it is the
              difference between this and your application.

              http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t=

              • 4. Re: Simple JMS Queue Question
                Raja Master

                Is it because your connection.start() is done after the send is issued?

                • 5. Re: Simple JMS Queue Question
                  Adrian Brock Master

                  No start() only affects delivery.

                  Regards,
                  Adrian

                  • 6. Re: Simple JMS Queue Question
                    sanjewad Newbie

                    17:11:49,828 INFO [STDOUT] org.jboss.mq.SpyTextMessage {
                    Header {
                    jmsDestination : QUEUE.testQueue
                    jmsDeliveryMode : 1
                    jmsExpiration : 0
                    jmsPriority : 4
                    jmsMessageID : ID:3-10618099097811
                    jmsTimeStamp : 1061809909781
                    jmsCorrelationID: null
                    jmsReplyTo : null
                    jmsType : null
                    jmsRedelivered : false
                    jmsPropertiesReadWrite:false
                    msgReadOnly : true
                    producerClientId: ID:3
                    }
                    Body {
                    text :hello
                    }
                    }

                    This is what I get When I run your TestMDB.
                    But how do I know the message is passed to MDB.
                    Simply if you put a System out in MDB It is not getting printed.
                    Am I doing something wrong?

                    • 7. Re: Simple JMS Queue Question
                      Adrian Brock Master

                      Did you look for differences.

                      Since my version works, there must be something you've
                      overlooked in your code.

                      Try changing testMDB to do what you want to do.

                      Regards,
                      Adrian

                      • 8. Re: Simple JMS Queue Question
                        sanjewad Newbie

                        Hi Adrian
                        I am getting the same output as above. My question is How do I know whether I am getting OnMessage MDB output or How do I invoke onMessage MDB System Output?

                        Thanks

                        • 9. Re: Simple JMS Queue Question
                          sanjewad Newbie

                          I am getting the same output as above. My question is How do I know whether I am getting OnMessage MDB output or How do I invoke onMessage MDB System Output?

                          Simply I can't see any output from my Message Driven Bean. How Do I know MDB OnMessage method receive this message. I have put a System out on this and I can't see my output getting printed. What shall I do for this?

                          Thanks

                          • 10. Re: Simple JMS Queue Question
                            Adrian Brock Master

                            I've lost track of what you are asking?

                            You cannot see any output from your MDB?
                            But you can when you use my testMDB,
                            mine just does a System.out.println(message);

                            Have you added your code into my MDB?

                            Or are you asking something else?

                            Regards,
                            Adrin

                            • 11. Re: Simple JMS Queue Question
                              sanjewad Newbie

                              I have altered your testMDB adding

                              public void onMessage(Message message)
                              {
                              System.out.println("This is the message" + message);
                              }

                              to onMessage method.

                              Still I cannot see System out from MDB saying

                              "This is the message Hello"

                              Instead
                              I see only a kind of output like
                              -----------------------
                              13:52:48,765 INFO [STDOUT] This is the messageorg.jboss.mq.SpyTextMessage {
                              Header {
                              jmsDestination : QUEUE.testQueue
                              jmsDeliveryMode : 1
                              jmsExpiration : 0
                              jmsPriority : 4
                              jmsMessageID : ID:9-10619707687501
                              jmsTimeStamp : 1061970768750
                              jmsCorrelationID: null
                              jmsReplyTo : null
                              jmsType : null
                              jmsRedelivered : false
                              jmsPropertiesReadWrite:false
                              msgReadOnly : true
                              producerClientId: ID:9
                              }
                              Body {
                              text :hello
                              }
                              }

                              ---------------------------

                              This is using your testMDB.
                              How can I see the MDB message

                              "This is the message Hello" using your MDB.

                              • 12. Re: Simple JMS Queue Question
                                Adrian Brock Master

                                What does your MDB code look like now?

                                Regards,
                                Adrian

                                • 13. Re: Simple JMS Queue Question
                                  sanjewad Newbie

                                  I have done nothing but changing the System out as shown below. Rest of the code remains same including the client. But I can't see the amended System out from MDB. This is your TestMDB.

                                  /*
                                  * JBoss, the OpenSource J2EE webOS
                                  *
                                  * Distributable under LGPL license.
                                  * See terms of license at gnu.org.
                                  */
                                  package test.mdb;

                                  import javax.ejb.*;
                                  import javax.jms.*;
                                  import javax.naming.InitialContext;

                                  public class TestMDBBean
                                  implements MessageDrivenBean, MessageListener
                                  {
                                  MessageDrivenContext ctx;

                                  public void onMessage(Message message)
                                  {
                                  System.out.println("This is the message" + message);
                                  }

                                  public void ejbCreate()
                                  {
                                  }

                                  public void ejbRemove()
                                  {
                                  }

                                  public void setMessageDrivenContext(MessageDrivenContext ctx)
                                  {
                                  this.ctx = ctx;
                                  }
                                  }

                                  • 14. Re: Simple JMS Queue Question
                                    Markus Härnvi Newbie


                                    Here you are printing the TextMessage object:

                                    System.out.println("This is the message" + message);

                                    And here you get the output you asked for:

                                    13:52:48,765 INFO [STDOUT] This is the messageorg.jboss.mq.SpyTextMessage {
                                    Header {
                                    jmsDestination : QUEUE.testQueue
                                    jmsDeliveryMode : 1
                                    jmsExpiration : 0
                                    jmsPriority : 4
                                    jmsMessageID : ID:9-10619707687501
                                    jmsTimeStamp : 1061970768750
                                    jmsCorrelationID: null
                                    jmsReplyTo : null
                                    jmsType : null
                                    jmsRedelivered : false
                                    jmsPropertiesReadWrite:false
                                    msgReadOnly : true
                                    producerClientId: ID:9
                                    }
                                    Body {
                                    text :hello
                                    }

                                    This is what you get from TextMessage in a String context. It's normal.

                                    If you want only the text content of the message you have to call message.getText().