2 Replies Latest reply on Mar 29, 2003 4:14 PM by Adrian Brock

    JBossMq newbie: MDB not consuming message

    yanickduchesne Newbie


      I have a MDB that listens on a queue for incoming messages; the client successfuly sends the JMS messages (I see them in the /db directory of JBOSS); yet the MDB never has its onMessage() method called by the container. What's wrong? - see below for code and descriptors.

      Thanks for any help!!!



      Client
      ===============

      package com.newtrade.jboss.examples.jms;

      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;

      import java.util.Properties;

      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueConnection;
      import javax.jms.QueueSession;
      import javax.jms.QueueSender;
      import javax.jms.Queue;
      import javax.jms.TextMessage;

      /**
      * @author Yanick Duchesne
      * 26-Mar-2003
      */
      public class MDBClient {


      public static void main(String[] args) {

      try{

      Properties props = new Properties();
      props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      props.setProperty(InitialContext.PROVIDER_URL, "jnp://localhost:1099");
      props.setProperty(InitialContext.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

      InitialContext ctx = new InitialContext(props);

      QueueConnectionFactory fac = (QueueConnectionFactory)ctx.lookup("ConnectionFactory");
      QueueConnection conn = fac.createQueueConnection();
      Queue queue = (Queue)ctx.lookup("jms/jbossmig/messageQueue");
      QueueSession sess = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

      QueueSender sender = sess.createSender(queue);
      TextMessage txt = sess.createTextMessage("Foo Test");
      sender.send(txt);
      sender.close();
      sess.close();
      conn.close();

      System.out.println("Message sent");

      }catch(Throwable t){
      t.printStackTrace();
      }

      }

      }

      MDB
      ===============

      package com.newtrade.jboss.examples.jms;

      import com.newtrade.jboss.examples.datasource.DbSessionHome;
      import com.newtrade.jboss.examples.datasource.DbSession;

      import javax.ejb.EJBException;
      import javax.ejb.MessageDrivenBean;
      import javax.ejb.MessageDrivenContext;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.TextMessage;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      /**
      * @author Yanick Duchesne
      * 27-Mar-2003
      *
      * @ejb.bean
      * jndi-name="ejb/messageMDB"
      * name="ejb/messageMDB"
      * display-name="Message Driven Log Bean"
      * description="Logs messages"
      * acknowledge-mode="Auto-acknowledge"
      * destination-type="javax.jms.Queue"
      * transaction-type="Container"
      *
      * @jboss.destination-jndi-name
      * name="jms/jbossmig/messageQueue"
      *
      * @weblogic.pool
      * initial-beans-in-free-pool="2" max-beans-in-free-pool="5"
      */
      public class MessageMDBean implements MessageDrivenBean, MessageListener{

      private transient MessageDrivenContext _ctx;

      /**
      * Constructor for MessageMDB.
      */
      public MessageMDBean() {
      System.out.println("MDB created");
      }

      public void ejbCreate(){
      System.out.println("MDB created");
      }

      /**
      * @see javax.ejb.MessageDrivenBean#ejbRemove()
      */
      public void ejbRemove() throws EJBException {
      }

      /**
      * @see javax.ejb.MessageDrivenBean#setMessageDrivenContext(MessageDrivenContext)
      */
      public void setMessageDrivenContext(MessageDrivenContext ctx)
      throws EJBException {
      _ctx = ctx;
      }

      /**
      * @ejb.transaction type="Required"
      * @see javax.jms.MessageListener#onMessage(Message)
      */
      public void onMessage(Message msg) {
      try{

      System.out.println("received message");
      InitialContext ctx = new InitialContext();
      DbSessionHome dbh = (DbSessionHome)ctx.lookup(DbSessionHome.COMP_NAME);
      DbSession db = dbh.create();
      db.addMessage(((TextMessage)msg).getText());
      }catch(Exception e){
      e.printStackTrace();
      }

      }
      }

      ejb-jar.xml
      ====================

      ...

      <!-- Message Driven Beans -->
      <message-driven >
      <![CDATA[Logs messages]]>
      <display-name>Message Driven Log Bean</display-name>

      <ejb-name>ejb/messageMDB</ejb-name>

      <ejb-class>com.newtrade.jboss.examples.jms.MessageMDBean</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>
      ...

      jboss.xml
      ====================



      <enterprise-beans>

      ...


      <ejb-name>ejb/messageSession</ejb-name>
      <jndi-name>ejb/messageSession</jndi-name>
      <local-jndi-name>ejb/messageSessionLocal</local-jndi-name>


      <message-driven>
      <ejb-name>ejb/messageMDB</ejb-name>
      <destination-jndi-name>jms/jbossmig/messageQueue</destination-jndi-name>
      </message-driven>

      </enterprise-beans>

      <resource-managers>
      </resource-managers>