Remote JMS Client and EJB3 MDB Queue Consumer in Eclipse (JBoss 7.1)
Contents
- 1 Environment Used
- 2 Project Description
- 3 Creating New EJB Project
- 4 Creating object message class
- 5 Creating Message Driven Bean Consumer
- 6 Configuring messaging services on JBoss AS 7
- 7 Deploying EJB project
- 8 Start/Restart the Server
- 9 Adding JAR file required for the client to run the client application
- 10 Creating JMS client
- 11 Folder Structure
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 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 Employee(int id, String name, String designation,
double salary)
{
this.id = id;
this.name = name;
this.designation = designation;
this.salary = salary;
}
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“
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.getId());
System.out.println(employee.getName());
System.out.println(employee.getDesignation());
System.out.println(employee.getSalary());
} else {
System.out.println("Not a 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 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
- Open standalone.xml and follow the steps below to configure messaging services.
Add extension element
Add
<extension module="org.jboss.as.messaging"/>
in the <extensions> element.
Add subsystem element
Add the following <subsystem> inside <profile> element.
<subsystem xmlns="urn:jboss:domain:messaging:1.1">
<hornetq-server>
<persistence-enabled>true</persistence-enabled>
<journal-file-size>102400</journal-file-size>
<journal-min-files>2</journal-min-files>
<connectors>
<netty-connector name="netty" socket-binding="messaging"/>
<netty-connector name="netty-throughput" socket-binding="messaging-throughput">
<param key="batch-delay" value="50"/>
</netty-connector>
<in-vm-connector name="in-vm" server-id="0"/>
</connectors>
<acceptors>
<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"/>
</netty-acceptor>
<in-vm-acceptor name="in-vm" server-id="0"/>
</acceptors>
<security-settings>
<security-setting match="#">
<permission type="send" roles="guest"/>
<permission type="consume" roles="guest"/>
<permission type="createNonDurableQueue" roles="guest"/>
<permission type="deleteNonDurableQueue" roles="guest"/>
</security-setting>
</security-settings>
<address-settings>
<address-setting match="#">
<dead-letter-address>jms.queue.DLQ</dead-letter-address>
<expiry-address>jms.queue.ExpiryQueue</expiry-address>
<redelivery-delay>0</redelivery-delay>
<max-size-bytes>10485760</max-size-bytes>
<address-full-policy>BLOCK</address-full-policy>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
</address-setting>
</address-settings>
<jms-connection-factories>
<connection-factory name="InVmConnectionFactory">
<connectors>
<connector-ref connector-name="in-vm"/>
</connectors>
<entries>
<entry name="java:/ConnectionFactory"/>
</entries>
</connection-factory>
<connection-factory name="RemoteConnectionFactory">
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="RemoteConnectionFactory"/>
<entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
</entries>
</connection-factory>
<pooled-connection-factory name="hornetq-ra">
<transaction mode="xa"/>
<connectors>
<connector-ref connector-name="in-vm"/>
</connectors>
<entries>
<entry name="java:/JmsXA"/>
</entries>
</pooled-connection-factory>
</jms-connection-factories>
<jms-destinations>
<jms-queue name="testQueue">
<entry name="queue/MyQueue"/>
</jms-queue>
<jms-topic name="testTopic">
<entry name="topic/MyTopic"/>
</jms-topic>
</jms-destinations>
</hornetq-server>
</subsystem>
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
In
<subsystem xmlns="urn:jboss:domain:security:1.1">
element,
inside
<login-module code="RealmUsersRoles" flag="required">
add this element
<module-option name="unauthenticatedIdentity" value="guest"/>
if not present.
Add guest user
Open application-roles.properties 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 add-user.sh 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;
import com.theopentutorials.mdb.to.Employee;
public class JMSApplicationClient {
public static void main(String[] args) {
TransportConfiguration transportConfiguration =
new TransportConfiguration(
NettyConnectorFactory.class.getName());
ConnectionFactory factory = (ConnectionFactory)
HornetQJMSClient.createConnectionFactoryWithoutHA(
JMSFactoryType.CF,
transportConfiguration);
//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(
false,
QueueSession.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
//1. Sending TextMessage to the Queue
TextMessage message = session.createTextMessage();
message.setText("Hello EJB3 MDB Queue!!!");
producer.send(message);
System.out.println("1. Sent TextMessage to the Queue");
//2. Sending ObjectMessage to the Queue
ObjectMessage objMsg = session.createObjectMessage();
Employee employee = new Employee();
employee.setId(2163);
employee.setName("Kumar");
employee.setDesignation("CTO");
employee.setSalary(100000);
objMsg.setObject(employee);
producer.send(objMsg);
System.out.println("2. Sent ObjectMessage to the Queue");
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
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










On first try it did not work. But with some googling (https://community.jboss.org/thread/197274), for JBoss 7.1.1 add (in standalone.xml)
under
loading...
I need to know how to do for JBoss 6.1, how you said using *-service.xml archive in META-INF foulder. Someone can give some tips about?
loading...
To develop EJB3 MDB in Eclipse for JBoss 6.1, refer this link.
*-service.xml file is created for JBoss AS 5.
loading...
Hi Nithya,Can we use standalone-full.xml instead of all these xml config?
loading...
Pingback: JavaPins
I am getting following error
19:05:41,541 WARN [org.hornetq.core.protocol.core.impl.RemotingConnectionImpl] (hornetq-failure-check-thread) Connection failure has been detected: Did not receive data from /127.0.0.1:4685. It is likely the client has exited or crashed without closing its connection, or the network between the server and client has failed. You also might have configured connection-ttl and client-failure-check-period incorrectly. Please check user manual for more information. The connection will now be closed.
19:05:41,541 WARN [org.hornetq.core.server.impl.ServerSessionImpl] (hornetq-failure-check-thread) Client connection failed, clearing up resources for session 84b012e2-d26f-11e1-a806-0019b922588a
19:05:41,551 WARN [org.hornetq.core.server.impl.ServerSessionImpl] (hornetq-failure-check-thread) Cleared up resources for session 84b012e2-d26f-11e1-a806-0019b922588a
please help on this
loading...
Check whether you have added extension element (<extension module=”org.jboss.as.messaging”/>) inside <extensions> element. Also make sure whether you have done the step 6 correctly and completely.
loading...
Hi,
Can you please provide an example for consuming messages from the queue?
I could publish to the queue using the MDB and I See messages process in the jboss console.
I tried the below code but I am getting null when i do receive.
Map connectionParams = new HashMap();
connectionParams.put(TransportConstants.PORT_PROP_NAME, 5455);
connectionParams.put(TransportConstants.HOST_PROP_NAME, "localhost");
TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams);
ConnectionFactory factory = (ConnectionFactory) HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);
// 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(false, QueueSession.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
// TextListener listener = new TextListener();
// consumer.setMessageListener(listener);
connection.start();
TextMessage message = (TextMessage) consumer.receive(2000);
System.out.println("Received message: " + message.getText());
loading...
Is there remote JNDI lookup for JMS connection factories and destinations without importing all the jboss/hornetmq dependent classes as imports in the application client. A simple remote jndi lookup should be sufficient using only javax.jms package to keep the client portable. All the application servers I have worked provide. Not sure why Jboss does not provide this functionality.
loading...
got my answer in jboss docs.
This keeps it clean from any jboss/hornetq specific api in the application client code. By initializing the following jndi props one can lookup any jndi resource remotely . make sure to exclude “java:jboss/exported/” portion of jndi name when looking up remotely
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, “org.jboss.naming.remote.client.InitialContextFactory”);
jndiProps.put(Context.PROVIDER_URL,”remote://localhost:4447″);
jndiProps.put(Context.SECURITY_PRINCIPAL, “jbossuser”);
jndiProps.put(Context.SECURITY_CREDENTIALS, “jbosspassword”);
assuming you create a user using add-user.sh in application-users
loading...
Glad you got it. When this article was written, JBoss 7.x did not support remote jndi except for EJB. It was added later.
loading...
Thanks for posting a very nice article. Could you please provide me the the answers for the following question :-
1. Is there any way to set max number of messages in a queue . how many maximum messages are in a queue at a single time.
loading...
Hi Im getting this error during deployment, I checked standalone.xml and it has this entries, in which I have read should fix the error.
Pls advise.
Warm Rgards,
19:55:36,968 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit.”FirstMdbProject.jar”.PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit.”FirstMdbProject.jar”.PARSE: Failed to process phase PARSE of deployment “FirstMdbProject.jar”
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0]
at java.lang.Thread.run(Thread.java:736) [vm.jar:1.6.0]
Caused by: org.jboss.msc.service.ServiceNotFoundException: Service service jboss.ejb.default-resource-adapter-name-service not found
at org.jboss.msc.service.ServiceContainerImpl.getRequiredService(ServiceContainerImpl.java:447) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.as.ejb3.deployment.processors.MessageDrivenComponentDescriptionFactory.getDefaultResourceAdapterName(MessageDrivenComponentDescriptionFactory.java:273)
at org.jboss.as.ejb3.deployment.processors.MessageDrivenComponentDescriptionFactory.processMessageBeans(MessageDrivenComponentDescriptionFactory.java:153)
at org.jboss.as.ejb3.deployment.processors.MessageDrivenComponentDescriptionFactory.processAnnotations(MessageDrivenComponentDescriptionFactory.java:81)
at org.jboss.as.ejb3.deployment.processors.AnnotatedEJBComponentDescriptionDeploymentUnitProcessor.processAnnotations(AnnotatedEJBComponentDescriptionDeploymentUnitProcessor.java:58)
at org.jboss.as.ejb3.deployment.processors.AbstractDeploymentUnitProcessor.deploy(AbstractDeploymentUnitProcessor.java:81)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
… 5 more
loading...
Add,
under,
in standalone/configuration/standalone.xml
loading...
This example is again producer only, even if we are creating consumer also that wont work until unless we are defining the maxSession in Listener class bcz we can see the 15 default consumers will be there that will consumes the messages in the queue whatever we are publishing.
I am seeing one working example here http://www.pretechsol.com/2012/10/how-to-create-mdb-consumer-in-eclipse.html
loading...
Well this article explains mdb consumer whereas what you posted is synchronous consumer where the receive blocks until a message is available in queue.
loading...
Hello!! this site is great!! and help me so much but i have a question.
i have tried to conect a java client using a main method and i get the next error :
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
i have done a servlet and this have run very good i dont have idea which is the problem if somebody can help me please.
this is my code is simple
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ClientUtility {
private static Context initialContext;
private static final String PKG_INTERFACES = “org.jboss.ejb.client.naming”;
public static Context getInitialContext() throws NamingException {
if (initialContext == null) {
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
initialContext = new InitialContext(properties);
}
return initialContext;
}
}
public class client {
final static String QUEUE_LOOKUP = “queue/test”;
final static String CONNECTION_FACTORY = “ConnectionFactory”;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
client cliente = new client();
Context context = ClientUtility.getInitialContext();
//Queue queue = (Queue) context.lookup(cliente.QUEUE_LOOKUP);
ConnectionFactory factory = (QueueConnectionFactory) context.lookup(cliente.CONNECTION_FACTORY);
} catch (Exception e) {
e.printStackTrace();
}
}
loading...
Hello!! this site is great!! and help me so much but i have a question.
i have tried to conect a java client using a main method and i get the next error :
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
i have done a servlet and this have run very good i dont have idea which is the problem if somebody can help me please.
loading...
Thank you for this Tuto
please i’m getting this error after running the client :
javax.jms.JMSSecurityException: Unable to validate user: null
at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:312)
at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSessionInternal(ClientSessionFactoryImpl.java:780)
at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSession(ClientSessionFactoryImpl.java:279)
at org.hornetq.jms.client.HornetQConnection.authorize(HornetQConnection.java:601)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:684)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:119)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:114)
at com.thopentutorials.client.JMSApplicationClient.main(JMSApplicationClient.java:35)
Caused by: HornetQException[errorCode=105 message=Unable to validate user: null]
… 8 more
help me please ..
loading...
Hi Nithya,
can u please do a post with eclipse and log4j?
thanks,
Shabu
loading...