Thursday, February 5, 2009

MDB in 3 minutes

This article introduces basic JMS Message Driven Bean (MDB) design that uses EJB 3.0 API (Java EE 5).

A MDB (EJB 3.0 API) class can be constructed with the following skeleton.

1. Implement the javax.jms.MessageListener interface.

2. Annotated with the MessageDriven annotation.

3. Include supported dependency injection annotation (optional).

The following is a "functional" MDB code example.

package emessaging.mdb;

import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(mappedName = "jms/myMDBQ")
public class HelloWorldMDBBean implements MessageListener {

public HelloWorldMDBBean() {

public void onMessage(Message message) {
System.out.println("*** received message: " + message);



When deployed in a container that supports Java EE 5, the above MDB receives messages on destination with the JNDI loop up name "jms/myMDBQ". This MDB uses the container-managed transaction (default). Transaction and message acknowledgment are automatically handled by the container for each message delivered to aMDB instance (onMesage() invocation).

In order to have the above code working properly, the following resources are also required to be configured for the target container (and/or the messaging server).

  • A JNDI Destination object must be created with look up name "jms/myMDBQ".
  • The JMS destination resource associated with "jms/myMDBQ" must be created on the messaging server.
  • A JNDI ConnectionFactory object may required be created and associated with the MDB.

The ConnectionFactory and Destination objects are heavily vendor dependent. After they are configured, the information may also need to be packaged along with the MDB code and then deployed to a target container (app server).

It is an inhumanity process to configure/package the administrated objects/MDB without using a (vendor) supported IDE/tool.

At the time of this writing, the IDE/tools have been improved over the years and are reasonably useful for all major vendors.

For example, the above example can be constructed/deployed/tested with netbeans IDE without dealing with the details of the notorious Java EE configuration syntax.

If you are not comfortable with this example, here is a link to get familiar with the basics.

In order to be comfortable with the MDBs, you would probably like to know what happened "under the hood". In the coming article, we will introduce the details of how it works.

No comments:

Post a Comment