Message Driven Bean has been edited by Ashish Jain (Jul 02, 2008).

(View changes)


This application is a simple JMS application where in a user sends information to the administrator for updation.

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.

  • Sun JDK 5.0+ (J2SE 1.5)
  • Eclipse (Eclipse Classic package of Europa distribution), which is platform specific
  • Web Tools Platform (WTP) 2.0.1
  • Data Tools Platform (DTP) 1.5.1
  • Eclipse Modeling Framework (EMF) 2.3.1
  • Graphical Editing Framework (GEF) 3.3.1

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

The application development will take you through the following

Creating a Dynamic Web Project

  1. Launch Eclipse. Select File->New->Project.

  2. Select Web->Dynamic Web Project. Select Next.

  3. Name the project as WebMDB. Select Next.

  4. Next window displays the various project facets. Keep the default values and select Next.

  5. Keep the default values for the next window. Select Next->Finish.

  6. Right click on WebMDB project and create a new servlet.

  7. Name the servlet as UserServlet and package as webmdb. This is the producer in the application.

  8. Select Next and later Finish.

  9. Add the following code to
    package webmdb;
    import java.util.Enumeration;
    import java.util.Properties;
    import javax.annotation.Resource;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.MessageListener;
    import javax.jms.MessageProducer;
    import javax.jms.Queue;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import mdb.AdminMDB;
     public class UserServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    	 private ConnectionFactory connectionFactory;
    	 private Queue queue;
    	 static final long serialVersionUID = 1L;
         public UserServlet() {
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		PrintWriter out = response.getWriter();
    		if(connectionFactory == null) {
    			out.println("Connection Factory lookup has failed");
    		if(queue == null) {
    			out.println("Queue lookup has failed");
    	Connection connection;
    			connection = connectionFactory.createConnection();
    			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    			MessageProducer producer = session.createProducer(queue);
    			TextMessage message = session.createTextMessage();
    			Enumeration arr=request.getParameterNames();
    			String fields= (String)arr.nextElement();
    			String paramname[]=request.getParameterValues(fields);
    			String s=null;
    			int i;
    			for (i=0; i<paramname.length;i++)
    			s=fields+":" + paramname[i];
    		catch(Exception e)
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);

  10. Right Click on WebContent and create a jsp.

  11. Name the jsp as index.jsp. Select Next.

  12. Select Finish.

    Add the following code to index.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Sample Web Application accessing JMS Resources</title>
    <form action="UserServlet">
    <h1>Please enter the updated information</h1>
    <td><input type="text" name="username" size="30"></td>
    <td><input type="text" name="userid" size="30"></td>
    <td>Old Address:</td>
    <td><input type="text" name="oldaddress" size="30"></td>
    <td>New Address:</td>
    <td><input type="text" name="newaddress" size="30"></td>
    <input type="submit" value="Submit">

    This will create the producer and user interface required by the application.

Creating Connection Factory and Destination

In simple terms a Connection Factory is an object which is used by a client to connect to the Service provider. In our case we will be using Active MQ as the provider.
Destination is an object which is used by client to provide the target to messages produced and source of the messages to be consumed. In our case the target is going to be a queue.
We will use the default connection factory and queue which is already available with Apache Geronimo.

Creating a EJB Project

  1. Right click under project explorer and Select New-> Project-> EJB Project.

  2. Name the project as MessageDrivenBean and Select Next.

  3. On the next screen keep defaults and select Next.

  4. On the next screen uncheck Generate Deployment Descriptor and select Next.

  5. Next screen suggests to configure Geronimo Deployment Plan. For current tutorial we are keeping the default values.

    This creates the skeleton of the EJB project. Next step is to add a pojo to our project and annotate it.
  6. Right click on EJB project and select New->class.

  7. Name the class as AdminMDB and package as mdb. Select Finish.

  8. Add the following code to
    package mdb;
    //import java.sql.SQLException;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    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="AdminMDB", activationConfig = {
    	    @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
    	    @ActivationConfigProperty(propertyName="destination", propertyValue="SendReceiveQueue")
    public class AdminMDB implements MessageListener{
    	public AdminMDB()
    	public void initialize() {
    	public void cleanup()
    	 public void onMessage(Message message) {
    	        TextMessage textMessage = (TextMessage) message;
    	        try {
    	            System.out.println(textMessage.getText()+ "\n");
    	        } catch (JMSException e) {

Modifying Deployment plan

  1. In WebMDB project select WebContent/WEB-INF/geronimo-web.xml and modify the content with the one shown below.
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ns8:web-app xmlns="" xmlns:ns2="" xmlns:ns3="" xmlns:ns4="" xmlns:ns5="" xmlns:ns6="" xmlns:ns7="" xmlns:ns8="" xmlns:ns9="">
  2. In MessageDrivenBean project select META-INF/openejb-jar.xml and modify it as follows
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <openejb-jar xmlns="" xmlns:nam="" 
    xmlns:sec="" xmlns:sys="">
         		<nam:resource-link>ActiveMQ RA</nam:resource-link>

    Deploy and Run

  3. Export MessageDrivenBean.jar and WebMDB.war from eclipse as shown in the figure below

    Unable to render embedded object: File (run.png) not found.

    Unable to render embedded object: File (run2.png) not found.

  4. Launch Administrative console and deploy the EJB and Web Application using Deploy New.
  5. Once done launch the web application with http://localhost:8080/WebMDB/.\\

    Unable to render embedded object: File (run3.png) not found.
  6. Fill the form as suggested in the figure and Select Submit.

    Unable to render embedded object: File (run4.png) not found.

  7. Once done check out the command prompt. Message Driven Bean has triggered and it displays the message currently on the queue.

    Unable to render embedded object: File (run5.png) not found.

Powered by Atlassian Confluence (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request

Unsubscribe or edit your notifications preferences