Remote JMS Client and EJB3 MDB Queue Consumer in Eclipse (JBoss 7.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) 7.1.0 Final

Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.0 Session bean 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 without using JNDI.
  • 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.
  • For simplicity, the message driven bean (message consumer) and the Java Application Client (message producer) are created on the same project.

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 7.1 Runtime has been selected with the EJB 3.0 Module version.
  • This project uses Java 1.6 version. The default Java version in Eclipse Indigo is 1.7 so to change the “Configuration” click on “Modify…” button to make changes.
  • Click Next -> Next -> and Finish.
  • You will see an EJB project in the Project Explorer view.

Creating object message class

  • Right click on ejbModule -> New -> Class
  • Enter the Java package name as
  • Enter the Class name as Employee
  • Click “Finish

Copy the following code:


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;
	public int getId() {
		return id;
	public void setId(int id) { = id;
	public String getName() {
		return name;
	public void setName(String 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;
	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

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;

@MessageDriven(activationConfig = {
		propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		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: ");
			} else {
				System.out.println("Not a valid message for this Queue MDB");

		} catch (JMSException e) {
  • 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 7

In the previous version of JBoss AS we had an option to configure the messaging destinations (Queue or Topic) in META-INF/*-service.xml with element. In JBoss AS 7, it is configured in main application server configuration file (standalone.xml) which is found in JBossAS_Home/standalone/configuration.

  • Open standalone.xml and follow the steps below to configure messaging services.

Add extension element


<extension module=""/>

in the <extensions> element.

Add subsystem element

Add the following <subsystem> inside <profile> element.

<subsystem xmlns="urn:jboss:domain:messaging:1.1">

                    <netty-connector name="netty" socket-binding="messaging"/>
                    <netty-connector name="netty-throughput" socket-binding="messaging-throughput">
                        <param key="batch-delay" value="50"/>
                    <in-vm-connector name="in-vm" server-id="0"/>

                    <netty-acceptor name="netty" socket-binding="messaging"/>
                    <netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
                        <param key="batch-delay" value="50"/>
                        <param key="direct-deliver" value="false"/>
                    <in-vm-acceptor name="in-vm" server-id="0"/>

                    <security-setting match="#">
                        <permission type="send" roles="guest"/>
                        <permission type="consume" roles="guest"/>
                        <permission type="createNonDurableQueue" roles="guest"/>
                        <permission type="deleteNonDurableQueue" roles="guest"/>

                    <address-setting match="#">

                    <connection-factory name="InVmConnectionFactory">
                            <connector-ref connector-name="in-vm"/>
                            <entry name="java:/ConnectionFactory"/>
                    <connection-factory name="RemoteConnectionFactory">
                            <connector-ref connector-name="netty"/>
                            <entry name="RemoteConnectionFactory"/>
                            <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
                    <pooled-connection-factory name="hornetq-ra">
                        <transaction mode="xa"/>
                            <connector-ref connector-name="in-vm"/>
                            <entry name="java:/JmsXA"/>

                    <jms-queue name="testQueue">
                        <entry name="queue/MyQueue"/>
                    <jms-topic name="testTopic">
                        <entry name="topic/MyTopic"/>

The entry name for jms-queue should match the destination activation config property in @MessageDriven.
jms-queue name should match the queue name in the remote sender.

Add messaging port

In <socket-binding-group> add these two elements

<socket-binding name="messaging" port="5445"/>
<socket-binding name="messaging-throughput" port="5455"/>

Add unauthenticated user module


<subsystem xmlns="urn:jboss:domain:security:1.1"> 


<login-module code="RealmUsersRoles" flag="required">

add this element

<module-option name="unauthenticatedIdentity" value="guest"/>

if not present.

Add guest user

Open file which is found in JBossAS_Home/standalone/configuration and uncomment the guest=guest line by removing the # at the beginning of the line to enable the guest user.

The above two steps are done to allow unauthenticated users to access JMS Queue/Topic remotely through role.

If you want to add a user for authenticated access to JMS Queue or Topic remotely, run add-user.bat or available in bin directory.

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 7.1 Runtime Server” and click Finish.
    • Right click on “JBoss 7.1 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 7.1 Runtime Server” from Servers view and click on Start if it has not yet been started.

If the project is deployed properly with global JNDI mapping then you will see the following message in the console.

Bound messaging object to jndi name java:/queue/MyQueue
Started message driven bean ‘QueueListenerMDB’ with ‘hornetq-ra’ resource adapter
Deployed “FirstMDBProject.jar”

Adding JAR file required for the client to run the client application

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

JAR name Where to find
jboss-client-7.1.0.Final.jar AS7_HOME/bin/client

Creating JMS client

  • The next step is to write a Java Application Client (main()) which produces the message and sends to the Queue.
  • Right click on ejbModule -> New -> Class
  • Enter the package name as com.thopentutorials.client
  • Enter the Class name as JMSApplicationClient
  • Check the main() method option
  • Click on Finish

package com.theopentutorials.client;

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 org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.jms.HornetQJMSClient;
import org.hornetq.api.jms.JMSFactoryType;
import org.hornetq.core.remoting.impl.netty.NettyConnectorFactory;


public class JMSApplicationClient {

	public static void main(String[] args) {
		TransportConfiguration transportConfiguration = 
		                new TransportConfiguration(
		ConnectionFactory factory = (ConnectionFactory)
		//The queue name should match the jms-queue name in standalone.xml
		Queue queue = HornetQJMSClient.createQueue("testQueue");
		Connection connection;
		try {
			connection = factory.createConnection();
			Session session = connection.createSession(
			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) {

Folder Structure

The figure below shows the final directory structure of this EJB3 MDB project “FirstMDBProject”.

Run the client

Use Ctrl + F11 to run the client.

Client console will display the following output

1. Sent TextMessage to the Queue
2. Sent ObjectMessage to the Queue

JBoss Runtime Server Console will display the following output.

[stdout] Queue: I received a TextMessage at Fri Feb 17 18:03:41 IST 2012
[stdout] Message is : Hello EJB3 MDB Queue!!!
[stdout] Queue: I received an ObjectMessage at Fri Feb 17 18:03:41 IST 2012
[stdout] Employee Details:
[stdout] 2163
[stdout] Kumar
[stdout] CTO
[stdout] 100000.0

Leave a Comment