Platinum Partner
java,enterprise-integration,jms,tool,tips and tricks,tools & methods,spring integration,hornetq

Connecting a Spring Web App to a Standalone HornetQ JMS

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"/>
	</queue>


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" />
		<constructor-arg>
			<bean class="org.hornetq.api.core.TransportConfiguration">
				<constructor-arg
					value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
				<constructor-arg>
					<map key-type="java.lang.String" value-type="java.lang.Object">
						<!-- HornetQ standalone instance details -->
						<entry key="host" value="192.168.22.573"></entry>
						<entry key="port" value="5445"></entry>
					</map>
				</constructor-arg>
			</bean>
		</constructor-arg>
	</bean>

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

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

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

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

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 {
	
	@Autowired
	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.

@Autowired
JMSProducer jMSProducer;

..........

try {
	jMSProducer.sendMessages();
} catch (JMSException e) {
	e.printStackTrace();
}
..........


try {
	jMSProducer.receiveMessages();
} catch (JMSException e) {
	e.printStackTrace();
}

{{ tag }}, {{tag}},

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

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}