HornetQ Stand Alone – Basic JMS Messaging Example

HornetQ is an open source project to build a multi-protocol, embeddable, very high performance, clustered, asynchronous messaging system. HornetQ supports the JMS 1.1 API and also defines its own messaging API for maximum performance and flexibility. HornetQ class-beating high performance journal provides persistent messaging performance at rates normally seen for non-persistent messaging. HornetQ offers server replication and automatic client fail-over to eliminate lost or duplicated messages in case of server failure.

In previous post, we learned about configuring a stand alone hornetq server and basic configuration. Lets take the example forward.

In this post, we will learn the mechanism of sending JMS messages to a queue on hornetq server and then we will retrieve those messages.

Step 1) Create a maven project using below command and convert it to eclipse java project

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HornetQHelloWorld
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 cd HornetQHelloWorld

 mvn eclipse:eclipse

step 2) Update pom.xml file and update the project dependencies


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

Step 3) Place the basic hornetq configuration file in classpath.


<?xml version="1.0"?>
<configuration xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
		<connector name="netty-connector">
		<acceptor name="netty-acceptor">

Step 4) Configure the connector factory and place the configuration file in classpath.


<?xml version="1.0"?>
<configuration xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
	<!--the connection factory used by the example -->
	<connection-factory name="ConnectionFactory">
			<connector-ref connector-name="netty-connector" />
			<entry name="ConnectionFactory" />
	<queue name="exampleQueue">
		<entry name="exampleQueue" />

Step 5) Start the server and test the messaging code


package com.howtodoinjava;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.jms.HornetQJMSClient;
import org.hornetq.core.config.impl.FileConfiguration;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.integration.transports.netty.NettyConnectorFactory;
import org.hornetq.integration.transports.netty.TransportConstants;
import org.hornetq.jms.server.JMSServerManager;
import org.hornetq.jms.server.impl.JMSServerManagerImpl;

public class HornetQMessageQueueDemo {
	static void startServer()
		     FileConfiguration configuration = new FileConfiguration();
		     HornetQServer server = HornetQServers.newHornetQServer(configuration);
		     JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, "hornetq-jms.xml");
		     //if you want to use JNDI, simple inject a context here or don't call this method and make sure the JNDI parameters are set.
		     System.out.println("Server started !!");
		  catch (Throwable e)
		     System.out.println("Damn it !!");
	public static void main(String[] args) throws Exception
		//Start the server
		Connection connection = null;
			// Step 1. Directly instantiate the JMS Queue object.
			Queue queue = HornetQJMSClient.createQueue("exampleQueue");

			// Step 2. Instantiate the TransportConfiguration object which
			// contains the knowledge of what transport to use,
			// The server port etc.

			Map<String, Object> connectionParams = new HashMap<String, Object>();
			connectionParams.put(TransportConstants.PORT_PROP_NAME, 5445);

			TransportConfiguration transportConfiguration = new TransportConfiguration(
																NettyConnectorFactory.class.getName(), connectionParams);

			// Step 3 Directly instantiate the JMS ConnectionFactory object
			// using that TransportConfiguration
			ConnectionFactory cf = HornetQJMSClient.createConnectionFactory(transportConfiguration);

			// Step 4.Create a JMS Connection
			connection = cf.createConnection();

			// Step 5. Create a JMS Session
			Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

			// Step 6. Create a JMS Message Producer
			MessageProducer producer = session.createProducer(queue);

			// Step 7. Create a Text Message
			TextMessage message = session.createTextMessage("How to do in java dot com");

			System.out.println("Sent message: " + message.getText());

			// Step 8. Send the Message

			// Step 9. Create a JMS Message Consumer
			MessageConsumer messageConsumer = session.createConsumer(queue);

			// Step 10. Start the Connection

			// Step 11. Receive the message
			TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);

			System.out.println("Received message: " + messageReceived.getText());
			if (connection != null) {

Output in console:

22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: live server is starting..
22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Using NIO Journal
22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: Security risk! It has been detected that the cluster admin user and password have not been changed from the installation default. Please see the HornetQ user guide, cluster chapter, for instructions on how to do this.
22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Started Netty Acceptor version 3.1.5.GA-r1772

Server started !!

22 Mar, 2013 2:23:36 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: HornetQ Server version 2.0.0.GA (Hornet Queen, 113) started

Sent message: How to do in java dot com
Received message: How to do in java dot com

Download source code

Happy Learning !!

Leave a Reply

Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial