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


AddThis Social Bookmark Button

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

Step Four: Write the deployment descriptors for the MDB

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.


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.)


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



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.


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.