5 Replies Latest reply on Sep 30, 2004 4:49 PM by AAAAA BBBBB

    Connecting from MDB

    Jerry Payne Newbie

      I have an app that is being deployed using Ant. It includes a servlet that is able to pass a message to the "queue/testQueue". The problem is that the bean does not read the messages. I have an ejb-jar.xml file created that defines the destination-type as javax.jms.Queue for the Message Driven Bean. I have created a jboss.xml file that maps the Message Driven Bean to the jndi name "queue/testQueue".

      My question is this: How does the Message Driven Bean know which connection factory to use?

      I deploy the bean out just fine. I am able to send messages to the queue, but my bean does read them.

      Thanks for any help.

        • 1. Re: Connecting from MDB
          Oguz Kologlu Newbie

          Jerry,

          Your mappings are probably not correct. By default jboss will create a queue for anymessage driven beans that are not attached to a queue.

          Check http://localhost:8080/jmx-console to see what queues you have.

          Post your config files if you don't have any luck
          HTH
          Oz

          • 2. Re: Connecting from MDB
            Jerry Payne Newbie

            Here is the ejb-jar.xml and the jboss.xml. I am getting a message that says I cannot find "queue/MyMessageBean". In the configuration I am trying to point the Message Bean to "queue/TestQueue".

            ejb-jar.xml

            <ejb-jar>
            <description>
            Message Driven Bean EJB
            </description>
            <enterprise-beans>
             <message-driven>
             <display-name>MyMessageBean</display-name>
             <ejb-name>MyMessageBean</ejb-name>
             <ejb-class>com.hott.ejb.MyMessageBean</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>
            </ejb-jar>
            


            jboss.xml

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


            • 3. Re: Connecting from MDB
              AAAAA BBBBB Newbie

              The MDB does not need to know the connection factory. You just need to link the MDB with the queue, which you are doing correctly in the jboss.xml

              Maybe the problem is in your servlet code where you are sending the messages.

              • 4. Re: Connecting from MDB
                Jerry Payne Newbie

                I have checked the servlet code and it is working. I wrote a class that uses QueueBrowser to check the messages. All of the messages I sent are on the Queue.

                Here is the code for the servlet:

                package com.hott.servlets;
                import javax.jms.*;
                import javax.naming.*;
                import javax.servlet.http.*;
                import javax.servlet.*;
                import java.io.*;
                import java.util.Properties;
                /**
                 * sends messages to a JMS queue that will be processed by a Message Driven Bean
                 */
                public class MyMessageServlet extends HttpServlet {
                
                 QueueConnection queueConnection = null;
                 QueueSession queueSession = null;
                 Queue queue = null;
                 QueueSender queueSender = null;
                 TextMessage message = null;
                
                 public void init() {
                 Properties env = new Properties();
                 env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
                 env.put("java.naming.provider.url","localhost:1099");
                 env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
                 try {
                 Context jndiContext = new InitialContext(env);
                 QueueConnectionFactory queueConnectionFactory =
                 (QueueConnectionFactory) jndiContext.lookup("ConnectionFactory");
                 queue = (Queue) jndiContext.lookup("queue/testQueue");
                 queueConnection = queueConnectionFactory.createQueueConnection();
                 queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                 queueSender = queueSession.createSender(queue);
                 message = queueSession.createTextMessage();
                 } catch (JMSException e) {
                 e.printStackTrace();
                 } catch (NamingException ne){
                 ne.printStackTrace();
                 }
                 }
                
                 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                 String text = req.getParameter("message");
                 resp.setContentType("text/html");
                 PrintWriter out = resp.getWriter();
                 out.println("<html><head><title>Send Results</title></head><body>");
                 try {
                 message.setText(text);
                 out.println("Sending message: <b>" + message.getText()+"</b><br>");
                 queueSender.send(message);
                 out.println("Your message has been sent successfully.");
                 } catch (JMSException e) {
                 out.println("Your message was not sent.");
                 } finally {
                 out.println("</body></html>");
                 }
                 }
                
                 public void destroy(){
                 if (queueConnection != null) {
                 try {
                 queueConnection.close();
                 } catch (JMSException e) {}
                 }
                 }
                }
                


                • 5. Re: Connecting from MDB
                  AAAAA BBBBB Newbie

                  jndiContext.lookup("ConnectionFactory");

                  I am not sure about this, but have you changed the JNDI name for the connection factory. The default name is "JmsXA"