Developing MDB with JBoss AS 7

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

The latest AS 7.0.1 release made finally available the last EJB component which was missing: Message Driven Beans. Let's see how to develop a simple MDB on AS 7 with this 5 minute tutorial!

In the current AS 7 release, the messaging domain is included into the *-preview.xml configuration files. In the future 7.1 release, the messaging configuration will be merged in the main configuration file.

However that's not a big issue: for example if you want to run JMS applications on AS 7 standalone, you should start your server using the standalone-preview.xml configuration file.

In practice this just requires using the --server-config when starting your server. For example:

standalone.bat --server-config=standalone-preview.xml

Ok, so we want to roll MDB. Here's a very simple one which does a simple job of printing the text message received:

package com.mdb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(name = "MessageMDBSample", activationConfig = {
 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
 @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/sampleQueue"),
 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

public class MDBSample implements MessageListener {

   public void onMessage(Message message) {

     TextMessage tm = (TextMessage) message;
       try {
         System.out.println("Received message "+tm.getText());
       } catch (JMSException e) {



This MDB consumes messages from the queue sampleQueue. In order to add this queue from the CLI just issue:

[standalone@localhost:9999 /] add-jms-queue --name=sampleQueue --entries=queue/sampleQueue

If you want to skip this step, you could just use the queue/test which is by default configured in the AS 7.

Ok. Now all you need is a JMS sender. To make things super fast just add a Servlet to your application:

package com.servlet;


import javax.jms.*;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

public class JMSClientServlet extends HttpServlet {

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     String destinationName = "queue/sampleQueue";
     PrintWriter out = response.getWriter();
     Context ic = null;
     ConnectionFactory cf = null;
     Connection connection =  null;

     try {         
       ic = new InitialContext();

       cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
       Queue queue = (Queue)ic.lookup(destinationName);

       connection = cf.createConnection();
       Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
       MessageProducer publisher = session.createProducer(queue);

       TextMessage message = session.createTextMessage("Hello AS 7 !");

       out.println("Message sento to the JMS Provider");

     catch (Exception exc) {
    finally {         

      if (connection != null)   {
        try {
        } catch (JMSException e) {                    

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doGet(request, response);


By invoking your Servlet one JMS message will be sent to the defined queue, resulting in:

16:03:22,853 INFO  [stdout] (Thread-3 (group:HornetQ-client-global-threads-903047498)) Received message Hello AS 7 !

Related articles available on

EJB 3.0 tutorial : Session Beans

Enterprise JavaBeans (EJB) technology is a J2EE technology for de

JBoss MDB 3.0

Message-driven beans (MDBs) are stateless, server-side, transacti


In a Bean Managed Transaction, the code in the session or message

How do you keep your EJB callbacks separated from biz. methods?

JBoss daily recipe

How to set EJB timeout period ?

JBoss recipe of the day

How to add a Selector with MDB 3.0 ?

Message selectors allow an MDB to be more selective about the mes