JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Developing MDB with JBoss AS 7

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) {
  
        e.printStackTrace();
       }
 

   }

}

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 java.io.IOException;
import java.io.PrintWriter;

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


@WebServlet("/JMSClientServlet")
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);
 
       connection.start();

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

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

    }
     catch (Exception exc) {
       exc.printStackTrace();
     }
    finally {         


      if (connection != null)   {
        try {
           connection.close();
        } catch (JMSException e) {                    
          e.printStackTrace();
        }
    } 
  }
 }

   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 !<

Developing MDB with JBoss AS 7
You are here Home