7 Replies Latest reply on Oct 23, 2003 10:03 AM by vincentchun

    Receive / Subscrible message problem

    vincentchun Newbie

      I am using jboss-3.2.2RC4.

      I use Servlet to send message and receive message. After I send the message with Servlet, I can't receive the message with the receive message Servlet. If I call the receive message Servlet first, and then call the send message Servlet, the receive message Servlet can receive the message successfully.

      How can I send the message first, then receive the message with Servlet?

      I have try to use Queue, Topic and Durable Topic. The result are the same.

      Can anyone help me to solve this problem?

      Thanks a lot!

        • 1. Re: Receive / Subscrible message problem
          vincentchun Newbie

          How can I setup the JBossMQ in the following case?

          When I send message, the messages will be stored in DB (e.g. MySQL).
          When I receive messages, the messages will be received from DB.

          • 2. Re: Receive / Subscrible message problem
            Stephane Nicoll Master

            Well actually, this is the default behavior (you shouldn't care about the fact that the message is in DB or not. It might be in the cache with same results).

            Are you using the same connection for both sender and receiver. The fact that calling the receiver first do the trick sound to me as a bad init of your JMS sessions.

            Basically, the question is non sense because it's not really a special case, is it?

            Could you post the code you are using?

            Regards,

            Stephane

            • 3. Re: Receive / Subscrible message problem
              vincentchun Newbie

              Thanks Dark_Lord first.

              My source code as follow:
              (part of my code)

              My MDB:

              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());
              }catch (Throwable te) {
              System.err.println("MessageBean.onMessage:"+"Exception:"+te.toString());
              }
              }

              In my send message servlet:

              String username = "Vincent";
              QueueConnection conn = null;
              QueueSession session = null;
              TextMessage message = null;
              Queue queue = null;
              try {
              InitialContext ctx = new InitialContext();
              QueueConnectionFactory conFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
              conn = (QueueConnection) conFactory.createQueueConnection();
              queue = (Queue) ctx.lookup("queue/MessageQueue");
              session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
              message = session.createTextMessage("Vincent Testing Auth Queue successfully!!!");
              message.setStringProperty("user", username);
              QueueSender sender = session.createSender(queue);

              // sending the message & the message will stay at the destination for a day
              sender.send(message, DeliveryMode.PERSISTENT, 4, 86400000L);
              ctx.close();
              } catch (Exception e) {
              e.printStackTrace(System.err);
              } finally {
              try {
              if (session!= null)
              session.close();
              if (conn != null)
              conn.close();
              } catch(JMSException je){}
              }


              In my receive message servlet:

              String username = "Vincent";
              QueueConnection conn = null;
              QueueSession session = null;
              TextMessage message = null;
              try
              {
              Context ctx =new InitialContext();
              System.out.println("Looking up connection");
              QueueConnectionFactory conFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
              conn = (QueueConnection) conFactory.createQueueConnection();
              System.out.println("Looking up queue");
              Queue queue = (Queue) ctx.lookup("queue/MessageQueue");
              System.out.println("Creating session");
              session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
              System.out.println("Create receiver");
              QueueReceiver receiver = session.createReceiver(queue, "user = '" + username + "'");
              System.out.println("Connection Start");
              conn.start();
              Message myMes = receiver.receive();
              if(myMes != null)
              {
              if(myMes instanceof TextMessage)
              {
              message = (TextMessage)myMes;
              out.println(message.getText());
              }
              }
              } catch(JMSException je){}
              catch(NamingException ne){}
              finally
              {
              try {
              if (session!= null)
              session.close();
              if (conn != null)
              conn.close();
              } catch(JMSException je){}
              }

              In my ejb-jar.xml:

              <?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>
              <enterprise-beans>
              <message-driven>
              <display-name>MsgMDB</display-name>
              <ejb-name>MsgMDB</ejb-name>
              <ejb-class>fyp.gateway.mdb.MessageMDB</ejb-class>
              <transaction-type>Container</transaction-type>
              <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
              <message-driven-destination>
              <destination-type>javax.jms.Queue</destination-type>
              </message-driven-destination>
              </message-driven>
              </enterprise-beans>

              <assembly-descriptor>

              <container-transaction>

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

              <trans-attribute>Required</trans-attribute>
              </container-transaction>

              </assembly-descriptor>

              </ejb-jar>

              In my jboss.xml:

              <message-driven>
              <ejb-name>MsgMDB</ejb-name>
              <destination-jndi-name>queue/MessageQueue</destination-jndi-name>
              </message-driven>

              • 4. Re: Receive / Subscrible message problem
                Stephane Nicoll Master

                Well you have an MDB that is suppose to consume your message right?

                So you have actually two potential receivers here: the MDB first, your receiver servlet then.

                I don't see why you deploy such an architecture. If you post a message it will probably be fetched by the MDB. What are you trying to achieve. What's the purpose of this MDB?

                Regards,

                Stephane

                • 5. Re: Receive / Subscrible message problem
                  vincentchun Newbie

                  Thanks for Dark_Lord!

                  Now, I know I have two potential receivers (MDB and receiver servlet).
                  That is the cause of my problem!

                  My purpose is:
                  Use one serlvet to send message
                  and use another servlet to receive message
                  (use MDB in 'receive message servlet' to receive message)

                  Best Regards,
                  Vincent Chun

                  • 6. Re: Receive / Subscrible message problem
                    Stephane Nicoll Master

                    Your MDB cannot be used inside your servlet, this is a component on its own. Remove this MDB and it will work.

                    Regards,

                    Stephane

                    • 7. Re: Receive / Subscrible message problem
                      vincentchun Newbie

                      I remove that MDB and it is work.

                      Thank you very much!

                      Best Regards,
                      Vincent Chun