DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • The SPACE Framework for Developer Productivity
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Understanding Dependencies...Visually!
  • Observability Architecture: Financial Payments Introduction

Trending

  • The SPACE Framework for Developer Productivity
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Understanding Dependencies...Visually!
  • Observability Architecture: Financial Payments Introduction
  1. DZone
  2. Coding
  3. Frameworks
  4. Connecting a Spring Web App to a Standalone HornetQ JMS

Connecting a Spring Web App to a Standalone HornetQ JMS

Sreejith Sreekumar user avatar by
Sreejith Sreekumar
·
Jul. 03, 13 · Interview
Like (0)
Save
Tweet
Share
20.23K Views

Join the DZone community and get the full member experience.

Join For Free

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

HornetQ Spring Framework app

Opinions expressed by DZone contributors are their own.

Trending

  • The SPACE Framework for Developer Productivity
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Understanding Dependencies...Visually!
  • Observability Architecture: Financial Payments Introduction

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: