GlassFish Project - Simple Enterprise JavaBeansTM (EJB) 3.0 MDB Example 

New to GlassFish | Community Guidelines | Downloads | FAQ | How-Tos

Overview

This is a very simple EJB 3.0 Message Driven Bean that consumes JMS Messages.

Click here to get the ZIP file with the complete example.

Contact us at ejb@glassfish.dev.java.net with any comments or questions.


Message Driven Bean Class

Here's the bean implementation :

package ejb30;
import javax.ejb.MessageDriven;

@MessageDriven(mappedName="MDBQueue")
public class MDB implements MessageListener {

    public void onMessage(Message msg) {
        System.out.println("Got message!");
    }

}


@javax.ejb.MessageDriven is a component-defining annotation that designates this class as a bean class for a Message Driven Bean.  The message listener type(javax.jms.MessageListener) is derived from the implements clause.  

Deployment Descriptor

The good news is the deployment descriptor is no longer required!!!  The one java file above is sufficient to completely describe this message driven bean.  

Application Client

Here's a Java EE Application Client that send a JMS message to the message driven bean.   The Application Client uses dependency injection to get a Connection Factory and a Queue.  


package ejb30;
import javax.annotation.Resource;
import javax.jms.*;

public class MDBAppClient {
    @Resource(mappedName="MDBQueueConnectionFactory")
    private static QueueConnectionFactory queueCF;

    @Resource(mappedName="MDBQueue")
    private static Queue mdbQueue;

    public static void main(String args[]) {

        try {

            QueueConnection queueCon = queueCF.createQueueConnection();

            QueueSession queueSession = queueCon.createQueueSession
                (false, Session.AUTO_ACKNOWLEDGE);

            QueueSender queueSender = queueSession.createSender(null);

            TextMessage msg = queueSession.createTextMessage("hello");

            queueSender.send(mdbQueue, msg);

            System.out.println("Sent message to MDB");
           
            queueCon.close();

        } catch(Exception e) {
            e.printStackTrace();
        }

    }

}


Note that the Java EE 5 specification requires that annotations within Application Client classes be static since the entry point for the application is the static main() method.  

SUN App Server Specific Deployment Configuration

There is no need to define any SUN App Server-specific deployment descrpitors (e.g. sun-ejb-jar.xml, sun-application-client.xml) for this example.   The JNDI name of the Queue from which the message driven bean should consume is specified using the @MessageDriven mappedName() attribute.   Likewise,  the Application Client's @Resource dependencies are also resolved to JNDI names using the mappedName() attribute.  

Running the example


Use the following steps to build and execute the test.

  1. Set the glassfish.home property within build.xml.
  2. Set JAVA_HOME to point to a JDK 1.5 (or later) installation.
  3. ant build
  4. ant deploy
  5. ant run  

This will execute the Application Client and produce the following output  :

Sent message to MDB

Other Targets