Enterprise Integration Zone is brought to you in partnership with:

Sreejith has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

Connecting a Spring Web App to a Standalone HornetQ JMS

07.02.2013
| 10947 views |
  • submit to reddit

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();
}

Published at DZone with permission of its author, Sreejith Sreekumar.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Johan Undra replied on Wed, 2013/06/12 - 10:57am

Hey

Thanks for the short article. Really helped me a lot. But I have one question: I got everything set up now and it actually seems to run good. Messages are being send, but I have an issues:

Sending works fine, receiving not. It just times out if I set a timeout frame. Is there anything special for receiving to acknowledge?


Thanks in advance

 

Piero Giacomelli replied on Fri, 2013/07/05 - 3:40am

I've used the same example in my HornetQ Book


www.packtpub.com/hornetq-messaging-developers-guide/book 


Piero Giacomelli

Veera Marni replied on Wed, 2013/09/18 - 12:52am

Sreejith,


Firstly, i want to connect to your linked in profile, can you add me [veera marni works at Bias corp]. Secondly, i tried your example with Jboss 7.1.1 embedded HornetQ  and i'm getting few issues . Please look at stackflow_question let me know if i am missing any obvious one. I really appreciate if you can take a look at it and respond. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.