ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

EJB 2 Message-Driven Beans
Pages: 1, 2, 3

Step Four: Write the deployment descriptors for the MDB
(ejb-jar.xml)

At deployment time, we tell the container the information about our MDB. You use standard EJB deployment descriptors to setup MDBs. Therefore, we need a META-INF directory to house our descriptor files.



META-INF\ejb-jar.xml

In the ejb-jar file, we place the fully qualified class name of the MDB, the destination type of the bean, and security information etc. The file follows; notice the class name and the JMS destination type. (The tag <destination-type> was changed by the specication authors from <jms-destination-type>. You may need to use <jms-destination-type> if you are using BEA WebLogic 6.0sp1.)

<ejb-jar>
 <enterprise-beans>
  <message-driven>
   <ejb-name>EmailMDB</ejb-name>
   <ejb-class>com.customware.ejb.EmailMDB</ejb-class>
   <transaction-type>Container</transaction-type>
   <message-driven-destination>
    <destination-type>javax.jms.Queue</destination-type>
   </message-driven-destination>
   <security-identity>
    <run-as-specified-identity>
     <role-name>system</role-name>
    </run-as-specified-identity>
   </security-identity>
  </message-driven>
 </enterprise-beans>
</ejb-jar>

Where do we tell it the NAME of the queue? This goes in the vendor specific file. For example, if you were deploying this on BEA WebLogic 6.0 you would have the weblogic-ejb-jar.xml file that looks like


META-INF\weblogic-ejb-jar.xml

<weblogic-ejb-jar>
  <weblogic-enterprise-bean>
    <ejb-name>EmailMDB</ejb-name>
    <message-driven-descriptor>
      <pool>
        <max-beans-in-free-pool>200</max-beans-in-free-pool>
        <initial-beans-in-free-pool>5</initial-beans-in-free-pool>
      </pool>
      <destination-jndi-name>EmailQueue</destination-jndi-name>
    </message-driven-descriptor>
    <jndi-name>jms/EmailMDB</jndi-name>
  </weblogic-enterprise-bean>
</weblogic-ejb-jar>

The <ejb-name> links you to the bean definition in ejb-jar.xml. Then we can setup pool information. In this example, we are going to have 5 MDBs instantiated at deployment with the ability to increase to 200. This will allow us to have 200 concurrent messages hitting the queue. The <destination-jndi-name> is where we tell the container to look at the EmailQueue. Since we do not have this in code, if the JMS environment changed, we could just change this XML file, and redeploy the bean.

Step Five: Package up the code

Now we have gotten the code, and the deployment descriptors, we need to package it to deploy on the EJB server. The directory structure should look like

Directory structure diagram.

Within the client, ejb, and util directories should be the compiled classes; e.g. ../client/EmailClient.class, ../ejb/EmailMDB.class, and ../util/EmailHelper.class.

Now we archive the code and the deployment descriptors via:

../code% jar cvf emailmdb.jar com META-INF

Sample Files

Download the sample files discussed in this article here.

Now we have the Email MDB in a jar file that we deploy to an EJB server. To test, after deploying the bean, run the client, and you should see the EJB server sending out an email. You may need to make sure that the JavaMail API mail.jar is at the beginning of the CLASSPATH for your EJB server.

Conclusion

We have created a Message Driven Bean, showing how simple it is to tie into JMS as a consumer. Message Driven Beans are a nice addition to the EJB component architecture, offering a way for developers to create consumers that are pooled, transactional, and use the container's infrastructure.

Dion Almaer is a Principal Technologist for The Middleware Company, and Chief Architect of TheServerSide.Com J2EE Community.


Return to ONJava.com.