How to create EJB3 MDB Queue project in Eclipse (JBoss 6.1)

Environment Used

  • JDK 6 (Java SE 6)
  • JMS Sender/Client – Java Application Client (main())
  • JMS Consumer – EJB 3.0 Message Driven Bean (MDB)
  • Eclipse Indigo IDE for Java EE Developers (3.7.1)
  • JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
  • JBoss Application Server (AS) 6.1.0 Final

Setting up development environment:

Read this page for installing and setting up the environment for developing and deploying EJB 3.0 on JBoss application server.

Project Description

  • We are going to create a simple EJB 3 Message Driven Bean and a Java Application client (main()) which sends messages to the Queue destination.
  • This example explains how to develop, deploy and run EJB3 MDB as a message consumer in JBoss application server.
  • For testing this MDB listener, we write a Java Application Client (main()) as a message producer which sends a simple text message and an object message.
  • The message driven bean (message consumer) is created in a EJB project and the Java Application Client (message producer) is created in a separate Java project. To make this work, client needs required JAR file in its classpath.

Creating New EJB Project

  • Open Eclipse IDE and create a new EJB project which can be done in three ways,
    • Right click on Project Explorer -> New -> EJB Project
    • File menu -> New -> EJB Project
    • Click on the down arrow on New icon on toolbar -> EJB Project
  • Enter the project name as “FirstMDBProject” and make sure the JBoss 6.x Runtime has been selected with the EJB 3.0 Module version.

Creating object message class

  • Right click on ejbModule -> New -> Class
  • Enter the Java package name as com.theopentutorials.mdb.to
  • Enter the Class name as Employee
  • Click “Finish

Copy the following code:

package com.theopentutorials.mdb.to;
import java.io.Serializable;
public class Employee implements Serializable {
	private int id;
	private String name;
	private String designation;
	private double salary;
	
	public Employee() {	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + 
				", designation=" + designation + 
				", salary=" + salary + "]";
	}
}

Creating Message Driven Bean Consumer

  • Right click on ejbModule -> New -> Message-Driven Bean (EJB 3.x)
  • Enter the Java package name as com.theopentutorials.mdb
  • Enter the Class name as QueueListenerMDB
  • Select the Destination type as Queue
  • Click “Finish

Copy the following code:

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;
import com.theopentutorials.mdb.to.Employee;

@MessageDriven(activationConfig = {
		@ActivationConfigProperty(
				propertyName = "destinationType", 
				propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(
				propertyName = "destination", 
				propertyValue = "queue/MyQueue") })
public class QueueListenerMDB implements MessageListener {
	public QueueListenerMDB() {
	}

	public 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: ");
				System.out.println(employee);
			} else {
				System.out.println("Not valid message for this Queue MDB");
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
  • The activationConfig property of @MessageDriven is an array of ActivationConfigProperty and it should specify the destinationType (Queue or Topic) and destination (Queue/Topic’s JNDI name).
  • In the onMessage() we are receiving two types of message, TextMessage and ObjectMessage.

Configuring messaging services on JBoss AS 6

  • JBoss AS 6 uses HornetQ instead of JBoss Messaging.
  • To configure this we must create a “*-hornetq-jms.xml” file.
  • This file can be placed either inside
    • application’s META-INF folder (Specific for this application)
    • or JBossAS_Home/server/default/deploy (can be used by any application inside this server).
  • The *-hornetq-jms.xml files are picked up by JBoss AS and the queue/topics defined in that file will be deployed.
  • Create a new XML file in META-INF folder and name it as “mdb-hornetq-jms.xml” and copy the following:
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="urn:hornetq"
    xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd" >

    <queue name="MyQueue2" >
        <entry name="/queue/MyQueue" />
    </queue>

</configuration>

The queue’s entry name “queue/MyQueue” should match destination activation config property in @MessageDriven.

EJB Project Folder Structure

The complete folder structure of EJB project is shown below:

Deploying EJB project

  • Now we need to deploy the EJB project “FirstMDBProject” on server..
  • Deploying the project can be done in two ways,
    • Right click on the EJB project -> Run As -> Run On Server. Select the existing “JBoss 6.x Runtime Server” and click Finish.
    • Right click on “JBoss 6.x Runtime Server” available in Servers view -> Add and Remove… -> Select the EJB JAR file from the left pane and click Add-> and then Finish.

Start/Restart the Server

Right click on “JBoss 6.x Runtime Server” from Servers view and click on Start if it has not yet been started.

Creating Client Project

  • Create a new Java Project and name it as “FirstMDBClient”.

Creating JAR file

Since EJB and Client are in different project, to send the object message client needs to know the definition of Employee class. Let’s create a JAR file containing Employee.class and place it in Client’s classpath.

  • Right click on Employee.java file in FirstMDBProject -> Export.
  • Expand Java folder and select “JAR file” and click Next.
  • Click on Browse… and enter the file name and Finish.

Adding required JAR files

You can add the required JAR files in Build path (Right click on your client Project (FirstMDBClient)->Properties, select Java Build Path from left side pane and select Libraries from right side and click on Add External JARs)

Add the following JAR files.

JAR name Where to find
jbossall-client.jar JBossAS_Home/client
Employee.jar Created in the above step

Creating ClientUtility class

  • Java Application Client uses JNDI API to look up EJB components in a network.
  • This is a utility class which can be reused in all applications. Otherwise the code written in this class should be repeated in all clients.
  • Right click on src folder -> New -> Class
  • Enter the package name as com.theopentutorials.utility
  • Enter the Class name as ClientUtility
  • Click on Finish

Copy the following code:

package com.theopentutorials.utility;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ClientUtility {

	private static final String PROVIDER_URL = 
		"jnp://localhost:1099";

	private static final String JNP_INTERFACES = 
		"org.jboss.naming:org.jnp.interfaces";

	private static final String INITIAL_CONTEXT_FACTORY = 
		"org.jnp.interfaces.NamingContextFactory";

	private static Context initialContext;

	public static Context getInitialContextForClient()
			throws NamingException {
		if (initialContext == null) {
			Properties prop = new Properties();
			prop.put(Context.INITIAL_CONTEXT_FACTORY, 
					INITIAL_CONTEXT_FACTORY);
			prop.put(Context.URL_PKG_PREFIXES, 
					JNP_INTERFACES);
			prop.put(Context.PROVIDER_URL, PROVIDER_URL);
			initialContext = new InitialContext(prop);
		}
		return initialContext;
	}
}

Creating Client class

  • Now lets create a client class which sends messages to the queue destination.
  • Right click on src folder -> New -> Class
  • Enter the package name as com.theopentutorials.client
  • Enter the Class name as QueueSenderDemo
  • Click on Finish

Copy the following code

package com.theopentutorials.client;
  
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.NamingException;
import com.theopentutorials.mdb.to.Employee;
import com.theopentutorials.utility.ClientUtility;

public class QueueSenderDemo {
	private static final String QUEUE_LOOKUP = "queue/MyQueue";
	private static final String CONNECTION_FACTORY = "ConnectionFactory";

	public static void main(String[] args) {
		sendMessageToQueue();
		sendObjectMessageToQueue();
	}

	public static void sendMessageToQueue() {
		try {
			Context context = ClientUtility.getInitialContextForClient();
			QueueConnectionFactory factory = (QueueConnectionFactory) context
					.lookup(CONNECTION_FACTORY);
			QueueConnection connection = factory.createQueueConnection();
			QueueSession session = connection.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);
			Queue queue = (Queue) context.lookup(QUEUE_LOOKUP);

			QueueSender sender = session.createSender(queue);
			TextMessage message = session.createTextMessage();
			message.setText("Welcome to EJB3");
			sender.send(message);
			session.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

	public static void sendObjectMessageToQueue() {
		try {
			Context context = ClientUtility.getInitialContextForClient();
			QueueConnectionFactory factory = (QueueConnectionFactory) context
					.lookup(CONNECTION_FACTORY);
			QueueConnection connection = factory.createQueueConnection();
			QueueSession session = connection.createQueueSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);
			Queue queue = (Queue) context.lookup(QUEUE_LOOKUP);

			QueueSender sender = session.createSender(queue);
			ObjectMessage message = session.createObjectMessage();
			Employee emp = new Employee();
			emp.setDesignation("Developer");
			emp.setSalary(25000);
			emp.setName("ABC");
			emp.setId(1);
			message.setObject(emp);
			sender.send(message);
			session.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}

Client project folder structure

Run the client

Use Ctrl + F11 to run the client (QueueSenderDemo).
JBoss Runtime Server Console will display the following output.

[STDOUT] Queue: I received a TextMessage at Mon Jun 04 11:43:41 IST 2012
[STDOUT] Message is : Welcome to EJB3
[STDOUT] Queue: I received an ObjectMessage at Mon Jun 04 11:43:41 IST 2012
[STDOUT] Employee Details:
[STDOUT] Employee [id=1, name=ABC, designation=Developer, salary=25000.0]

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.