Six steps to MDB nirvana in JBoss

If you don't need to do any special configuration and already know how to code a message driven bean (MDB), here are 6 easy step to get it up and working with JBoss. (The easiest way to get up and working with Message Driven Beans in JBoss is the check out the jbosstest from cvs and look into the mdb test.)

  1. Check out the latest JBoss from cvs and compile.

  2. Write the source code for a Message Driven Bean.

  3. Write the ejb-jar.xml descriptor.

    Here is an example for a bean listening on a queue with bean managed transactions :

        <?xml version="1.0"?>
        <!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>
              <ejb-name>QueueBean</ejb-name>
              <ejb-class>org.jboss.test.mdb.bean.QueueBean</ejb-class>
              <message-selector></message-selector>
              <transaction-type>Bean</transaction-type>
              <acknowledge-mode>Auto-acknowledge</acknowledge-mode>
              <message-driven-destination>
                <destination-type>javax.jms.Queue</destination-type>
                <subscription-durability>NonDurable</subscription-durability>
              </message-driven-destination>
            </message-driven>
          </enterprise-beans>
        </ejb-jar>
    

    And here are one for a durable topic with container managed transactions:

         <?xml version="1.0"?>
         <!DOCTYPE ejb-jar>
         <ejb-jar>
           <enterprise-beans>
             <message-driven>
               <ejb-name>DurableTopicBean</ejb-name>
               <ejb-class>org.jboss.test.mdb.bean.TopicBean</ejb-class>
               <message-selector></message-selector>
               <transaction-type>Container</transaction-type>
               <message-driven-destination>
                 <destination-type>javax.jms.Topic</destination-type>
                 <subscription-durability>Durable</subscription-durability>
               </message-driven-destination>
             </message-driven>
           </enterprise-beans>
           <assembly-descriptor>
             <container-transaction>
               <method>
                 <ejb-name>DurableTopicBean</ejb-name>
                 <method-name>*</method-name>
               </method>
                <trans-attribute>Required</trans-attribute>
               </container-transaction>
           </assembly-descriptor>
         </ejb-jar>
    
  4. Write the jboss.xml deployment descriptor (this MUST always be done with MDB in jboss), but for a MDB without special need the container configuration need not be filled in.

    The destination-jndi-name element points to the queue.

    Here are the one for the queue bean:

    
         <?xml version="1.0" encoding="Cp1252"?>
    
         <jboss>
              <enterprise-beans>
                <message-driven>
                  <ejb-name>QueueBean</ejb-name>
                  <configuration-name>Standard Message Driven Bean</configuration-name>
                  <destination-jndi-name>queue/testQueue</destination-jndi-name>
                </message-driven>
              </enterprise-beans>
         </jboss>
    

    And here for the durable topic:

         <?xml version="1.0" encoding="Cp1252"?>
    
         <jboss>
              <enterprise-beans>
                <message-driven>
                  <ejb-name>DurableTopicBean</ejb-name>
                  <configuration-name>Standard Message Driven Bean</configuration-name>
                  <destination-jndi-name>topic/testDurableTopic</destination-jndi-name>
                  <mdb-user>john</mdb-user>
                  <mdb-passwd>needle</mdb-passwd>
                  <mdb-client-id>DurableSubscriberExample</mdb-client-id>
                </message-driven>
              </enterprise-beans>
         </jboss>
    
  5. Edit jbossmq.xml in conf/default and ad the queue or topic

    Eg:

         <Queue>
           <Name>testQueue</Name>
         </Queue>
    

    For the queue, and

          <Topic>
            <Name>testDurableTopic</Name>
          </Topic>
    

    plus

         <User>
           <Name>john</Name>
           <Password>needle</Password>
           <Id>DurableSubscriberExample</Id>
           <DurableSubscription>
             <Name>DurableSubscriberExample</Name>
             <TopicName>testDurableTopic</TopicName>
           </DurableSubscription>
         </User>
    

    for the durable topic.

  6. Deploy the bean, for example by packing it in a jar and copy it into the jboss deploy directory.

Start sending messages to your bean