Over a million developers have joined DZone.

Connecting a Spring Web App to a Standalone HornetQ JMS

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Problem at hand: Setting up a HornetQ as a standalone instance and running a JMS queue on it. Then getting a Spring webapplication connect to the queue to send and receive messages. 

Though the above problem seems quite easy to resolve, it does take some time to figure out and you can get easily lost among the tons of relevent and irrelevent materials available. I am listing below the steps to resolve the problem.

1. Setup HornetQ. This is the simplest part. Just download the package (I used 2.3.0.CR2).

Add the following configuration to include the queue you need in <HornetQ>\config\stand-alone\non-clustered\hornetq-jms.xml. 

	<queue name="DemoQueue">
		<entry name="/queue/DemoQueue"/>

2. Make the following changes in your spring configuration xml.

	<bean id="hornetConnectionFactory" class="org.hornetq.jms.client.HornetQJMSConnectionFactory">
		<constructor-arg name="ha" value="false" />
			<bean class="org.hornetq.api.core.TransportConfiguration">
					value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
					<map key-type="java.lang.String" value-type="java.lang.Object">
						<!-- HornetQ standalone instance details -->
						<entry key="host" value=""></entry>
						<entry key="port" value="5445"></entry>

    <!-- ConnectionFactory Definition -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="hornetConnectionFactory" />

	<!-- Definition of the JMS queue -->
	<bean id="defaultDestination" class="org.hornetq.jms.client.HornetQQueue">
        <constructor-arg index="0" value="DemoQueue"/>

	<bean id="producerTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="defaultDestination" />

    <bean id="messageSender" class="com.samples.producer.JMSProducer">
        <property name="jmsTemplate" ref="producerTemplate" />

3. Add relevent HornetQ jars to the webapplication lib folder.

4. JMS Message producer code as follows. I am using the same class to send and receive messages.

public class JMSProducer {
	private JmsTemplate jmsTemplate;
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;

	public JmsTemplate getJmsTemplate() {
		return this.jmsTemplate;
	public void sendMessages() throws JMSException {

            jmsTemplate.send(new MessageCreator() {

                public Message createMessage(Session session) throws JMSException {

                    TextMessage message = session.createTextMessage("test message"); 

                    message.setStringProperty("text", "Hello World");

                    return message;



	public void receiveMessages() throws JMSException {

        System.out.println("Getting message from queue "+jmsTemplate.receive().getStringProperty("text"));


5. Just autowire this class in the webpplication class that you wanted to send and receive the message from.

JMSProducer jMSProducer;


try {
} catch (JMSException e) {

try {
} catch (JMSException e) {

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}