6 Replies Latest reply on Sep 1, 2002 9:21 AM by juha

    invoking MDB from a servlet

    chumps

      Can somebody help?

      I'm trying to have as servlet send a message to a MDB. This seems like a simple thing, but I can't get it to work.

      Here is the MDB code:

      public void onMessage(Message message) {
      try {
      System.out.println("treating message");
      TextMessage textMsg = (TextMessage) message;
      String collectionUri = textMsg.getText();
      //Directory dmsCollection = (Directory) dms.getDmsObject(collectionUri);
      //handleCollection(dmsCollection);
      //handleLegislations(dmsCollection);
      System.out.println("sending response");
      sendResponse(message,"ok");
      System.out.println("response sent");
      }
      catch (Exception e) {
      throw new EJBException(e);
      }
      }

      here is the ejb-jar.xml

      <ejb-jar>
      <display-name>CanliiDBJAR</display-name>
      <enterprise-beans>
      <message-driven>
      <display-name>DmsSynchronizationProcessEJB</display-name>
      <ejb-name>DmsSynchronizationProcessEJB</ejb-name>
      <ejb-class>org.canlii.ejb.DmsSynchronizationProcessEJB</ejb-class>
      <transaction-type>Container</transaction-type>
      <message-driven-destination>
      <destination-type>javax.jms.Topic</destination-type>
      <subscription-durability>NonDurable</subscription-durability>
      </message-driven-destination>
      <security-identity>

      <run-as>

      <role-name>guest</role-name>
      </run-as>
      </security-identity>
      </message-driven>
      </enterprise-beans>
      </ejb-jar>

      the jboss.xml

      <enterprise-beans>
      <message-driven>
      <ejb-name>DmsSynchronizationProcessEJB</ejb-name>
      <destination-jndi-name>topic/dmsTopic</destination-jndi-name>
      </message-driven>
      </enterprise-beans>


      when I deploy it I notice the following warning:
      WARNING element resource-env-ref not handled in WebApplicationContext[/dmstest, DmsSyncWAR]

      When I run the following code the bean isn't invoked:
      Context jndiContext = new InitialContext();
      TopicConnectionFactory topicFactory = (TopicConnectionFactory)
      jndiContext.lookup("java:comp/env/jms/TopicFactory");
      Topic dmsTopic = (Topic)
      jndiContext.lookup("java:comp/env/jms/Topic");
      TopicConnection connection = topicFactory.createTopicConnection();
      TopicSession session = connection.createTopicSession(true,0);
      TopicPublisher publisher = session.createPublisher(dmsTopic);
      TextMessage msg = session.createTextMessage("/collections/test");
      QueueConnectionFactory queueFactory = (QueueConnectionFactory)
      jndiContext.lookup("java:comp/env/jms/QueueFactory");
      QueueConnection queueConnection = queueFactory.createQueueConnection();
      QueueSession queueSession = queueConnection.createQueueSession(true,0);
      Queue replyQueue = queueSession.createTemporaryQueue();
      msg.setJMSReplyTo(replyQueue);
      QueueReceiver receiver = queueSession.createReceiver(replyQueue);
      System.out.println("sending message");
      publisher.publish(msg);
      queueConnection.start();
      System.out.println("receiving message");
      TextMessage response = (TextMessage) receiver.receive();
      System.out.println("got message");

      here is the web.xml file:
      <resource-env-ref>
      <resource-env-ref-name>jms/Topic</resource-env-ref-name>
      <resource-env-ref-type>javax.jms.Topic</resource-env-ref-type>
      </resource-env-ref>
      <resource-ref>
      <res-ref-name>jms/TopicFactory</res-ref-name>
      <res-type>javax.jms.TopicConnectionFactory</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>
      <resource-ref>
      <res-ref-name>jms/QueueFactory</res-ref-name>
      <res-type>javax.jms.QueueConnectionFactory</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>

      and the jboss-web.xml

      <jboss-web>
      <resource-env-ref>
      <resource-env-ref-name>jms/Topic</resource-env-ref-name>
      <jndi-name>topic/dmsTopic</jndi-name>
      </resource-env-ref>
      <resource-ref>
      <res-ref-name>jms/TopicFactory</res-ref-name>
      <jndi-name>ConnectionFactory</jndi-name>
      </resource-ref>
      <resource-ref>
      <res-ref-name>jms/QueueFactory</res-ref-name>
      <jndi-name>ConnectionFactory</jndi-name>
      </resource-ref>
      </jboss-web>

      Am I doing something wrong? Thanks,

        • 1. Re: invoking MDB from a servlet
          chumps

          actually I get the warning when I deploy the servlet, not the MDB as my message suggests

          • 2. Re: invoking MDB from a servlet
            chumps

            I've finallly manage to get this working by replacing the following line
            TopicSession session = connection.createTopicSession(true,0);

            by
            TopicSession session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
            in a client code. One thing still puzzles me though, the JMS tutorial advise against this when it comes EJBs but I wasn't able to get my bean working properly with container managed transactions. Anyone knows if this is an issue in JBoss?

            thanks,

            • 3. Re: invoking MDB from a servlet

              Yes, this is a bug in JBossMQ... :(

              -- Juha

              • 4. Re: invoking MDB from a servlet

                ... or in the MDB integration, not sure which, maybe the latter

                -- Juha

                • 5. Re: invoking MDB from a servlet

                  Which version are you using?

                  WARNING element resource-env-ref not handled in WebApplicationContext[/dmstest, DmsSyncWAR]

                  This looks like a jetty message.

                  Support for resource-env-ref was added to embedded
                  jboss web applications in 2.4.4

                  Regards,
                  Adrian

                  • 6. Re: invoking MDB from a servlet

                    It doesnt work with bean to bean either, for example in our training labs we have a session bean sending a JMS message to MDB, you either have to set the JMS session as non-transacted, or if you need to use transactions explicitly commit() the JMS session (which should not be allowed to be called in the first place with CMT).

                    2.4.7 doesnt work, Im pretty sure 3.0.1RC1 didnt work either.

                    -- Juha