This tutorial will show you how we can integrate Spring and Apache ActiveMQ using point-to-point messaging domain. For more information on point-to-point messaging system please read tutorial

Before you moving forward please read the tutorial for configuring ActiveMQ but do not create any Queue.

Now we will look into the following steps in order to implement point-to-point messaging system using Spring and ActiveMQ integration.

1. Create a class called MessageProducer that will produce message or send message to the destination – Queue.

package com.roytuts.spring.jms.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

public class MessageProducer {

	private JmsTemplate jmsTemplate;

	public void sendMessageToDefaultDestination(final String message) {


 2. Create a class called MessageDefaultConsumer that will receive message from the destination – Queue.

package com.roytuts.spring.jms.consumer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MessageDefaultConsumer implements MessageListener {

	public void onMessage(Message message) {
		if (message instanceof TextMessage) {
			try {
				String msg = ((TextMessage) message).getText();
				System.out.println("Message has been consumed : " + msg);
			} catch (JMSException ex) {
				throw new RuntimeException(ex);
		} else {
			throw new IllegalArgumentException(
					"Message must be of type TextMessage");


 3. Create an XML property file that will hold all key/value pairs for the application. Put this file under src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<bean id="jmsProperties"
		<property name="order" value="99999" />
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="properties">

				<!-- JMS -->



 4. Create an XML configuration file that will hold all JMS related configuration for the application. Put this file under src/main/resources/jms

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<!-- ActiveMQ connection factory -->
	<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg index="0" value="${JMS.BROKER.URL}" />

	<!-- ConnectionFactory Definition -->
	<bean id="connectionFactory"
		<constructor-arg ref="amqConnectionFactory" />

	<!-- Destination Queue -->
	<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0" value="${JMS.QUEUE.NAME}" />

	<!-- JmsTemplate Definition -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="defaultDestination" ref="destinationQueue" />

	<!-- Message Producer -->
	<bean id="messageProducer" class="com.roytuts.spring.jms.producer.MessageProducer" />

	<!-- Message Consumer from Default Destination -->
	<bean id="messageDefaultConsumer" class="com.roytuts.spring.jms.consumer.MessageDefaultConsumer" />

	<!-- Message Consumer Container for Default Destination -->
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destinationName" value="${JMS.QUEUE.NAME}" />
		<property name="messageListener" ref="messageDefaultConsumer" />


 5. Create an XML file that will load all other resources and configure support for annotation in the application. Put this file under src/main/resources/spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:context=""


	<import resource="classpath:apache-activemq-properties.xml" />
	<import resource="classpath:jms/apache-activemq-jms.xml" />


 6. Create a test class under src/test/java that will run the MessageProducer.

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

public class MessageProducerTest {

	private MessageProducer messageProducer;

	public void setUp() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
		messageProducer = (MessageProducer) applicationContext

	public void testSendMessageToDefaultDestination() {
				.sendMessageToDefaultDestination("Send this message to default destination.");


7. Run the MessageProducer.


Message has been consumed : Send this message to default destination.


8. Click on Queues in ActiveMQ Web Console. You will not see any message in the IN_QUEUE because as soon as you ran the MessageProducer the message has been consumed by the MessageDefaultConsumer and now you will see one message has been dequeued. We do not need to run the MessageDefaultConsumer because it is an asynchronous messaging system and MessageDefaultConsumer is already registered to the Spring’s DefaultMessageListenerContainer. So when a message arrives to the Queue, the message gets automatically consumed by the consumer’s onMessage() method.

ActiveMQ SPring JMS integration

Here is the pom.xml

<project xmlns="" xmlns:xsi=""




		<!-- activemq -->

		<!-- Spring -->

		<!-- junit -->



That’s all. Thank you for reading.


I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *