MDB Example

29 April 2012 By Nithya Vasudevan 19,621 views 2 Comments
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

EJB3 Message Driven Bean

In this example, we are going to create an MDB which consumes the message sent to the Queue Destination and a JMS Application Client which sends the message to the Queue using JMS API.
The client sends two types of messages – TextMessage and ObjectMessage. For ObjectMessage we create a Transfer Object ‘Employee’ with some basic fields as follows.

This example uses JBoss 7.1 as application server.

Transfer Object Class

We create a simple ‘Employee’ class and use it to create an object which will be sent to the destination by the client as an ObjectMessage.

public class Employee implements Serializable{
private int id;
	private String name;
	private String designation;
	private double salary;
	public Employee() {	}
	public Employee(int id, String name, String designation, 
				double salary) { = id; = name;
		this.designation = designation;
		this.salary = salary;
	//Getters and Setters
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", designation="
				+ designation + ", salary=" + salary + "]";

JMS Application Client – Message Producer

We create a Java application which uses JMS API to send messages to the queue. This application client sends a text message and an employee object message and hence is the message producer.
The client needs to get the JMS Administered Objects – ConnectionFactory and the Destination objects by using JNDI lookup.

First, we need to get the Context object to lookup the JBoss’s JNDI namespace. We can use the following context properties to get the context object.

Properties env = new Properties();
env.put(Context.PROVIDER_URL, "remote://localhost:4447");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "password");
Context remoteContext = new InitialContext(env);

JBoss JNDI is protected by ApplicationRealm. So you need to provide username and password or create a new one by using the add user script available in ‘bin’ folder of JBoss.

Using the context object we can lookup the JMS administered objects and retrieve the connection factory and destination objects. Since the client is outside the JBoss container (normal java application), we need to use RemoteConnectionFactory.

ConnectionFactory factory = 
Queue queue = (Queue) remoteContext.lookup("jms/queue/MyQueue");

Next, we can use the connection factory to create a connection to the JMS provider which in turn can be used to create a session.
Using the session object, we can create the message and message producer which sends the message to the queue using the send() method.
The complete code is as follows.

package com.theopentutorials.client;

import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSApplicationClient1 {
	public static void main(String[] args) {
		Connection connection;
		try {
			final Properties env = new Properties();
			env.put(Context.PROVIDER_URL, "remote://localhost:4447");
			env.put(Context.SECURITY_PRINCIPAL, "user1");
			env.put(Context.SECURITY_CREDENTIALS, "pass1");
			Context remoteContext = new InitialContext(env);
			ConnectionFactory factory = 						
			Queue queue = (Queue) remoteContext.
			connection = factory.createConnection();
			Session session = connection.createSession(false, 
			MessageProducer producer = session.createProducer(queue);
			//1. Sending TextMessage to the Queue 
			TextMessage message = session.createTextMessage();
			message.setText("Hello EJB3 MDB Queue!!!");
			System.out.println("1. Sent TextMessage to the Queue");
			//2. Sending ObjectMessage to the Queue
			ObjectMessage objMsg = session.createObjectMessage();
			Employee employee = new Employee();
			System.out.println("2. Sent ObjectMessage to the Queue");
		} catch (JMSException e) {
		} catch (NamingException e) {

Message-Driven Bean – Message Consumer

Lastly, we need to create an MDB which will receive the message from the destination. While creating the MDB, we specify the destination type and destination JNDI in @ActivationConfigProperty annonation of @MessageDriven.

The MDB should implement the javax.jms.MessageListener interface and provide the onMessage() method implementation. Inside that method we retrieve the message and cast it to the appropriate message type (TextMessage or ObjectMessage). For TextMessage we can use the getText() method to get the message and for ObjectMessage, getObject() method returns the object sent to the destination by the producer.
The code is as follows.

package com.theopentutorials.mdb;
import java.util.Date;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
	activationConfig = 
		@ActivationConfigProperty(propertyName = "destinationType", 
					propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "destination", 
					propertyValue = "queue/MyQueue") 
public class QueueListenerMDB implements MessageListener {
	public QueueListenerMDB() {
	public final void onMessage(Message message){
		try {
			if (message instanceof TextMessage) {
				System.out.println("Queue: I received a TextMessage at " + 
								new Date());
				TextMessage msg = (TextMessage) message;
				System.out.println("Message is : " + msg.getText());
			} else if (message instanceof ObjectMessage) {
				System.out.println("Queue: I received an ObjectMessage " +
							" at " + new Date());
				ObjectMessage msg = (ObjectMessage) message;
				Employee employee = (Employee) msg.getObject();
				System.out.println("Employee Details: ");
			} else {
				System.out.println("Not valid message for this Queue MDB");
		} catch (JMSException e) {

Deploy the MDB and start the JBoss server. Run the application client which sends the message and verify that the MDB consumes it.

Tags: , , , , , , , ,

  • newbie2Java

    When running above example, getting below error. Please help.

    17:58:47,512 ERROR [org.jboss.remoting.remote.connection] (Remoting “leus00g8dhxw1ho” read-1) JBREM000200: Remote connection failed: An existing connection was forcibly closed by the remote host
    17:58:47,518 INFO [] (Remoting “leus00g8dhxw1ho” task-2) JBAS011806: Channel end notification received, closing channel Channel ID 54f88b40 (inbound) of Remoting connection 5f698b39 to null

    • Giorgio

      Your client seems to doesen’t have necessary credential for to connect the jboss server. You have to create an account on jboss server. You can do that by running a .bat call adduser and providing user and password…then you can you these in your jndi properties. Also be sure you remoting server is running, you can see that with server log. Hope this helps.
      By Giorgio.