Embedded ActiveMQ and Spring JMS Integration

Spring and Embedded ActiveMQ

This tutorial will show you how you can build Spring based application with Apache ActiveMQ for point-to-point messaging domain. In point to point messaging domain you have only one message producer and only one message consumer. For more information on point-to-point messaging system please read tutorial https://roytuts.com/configure-jms-client-using-glassfish-3/

As the ActiveMQ will be started in embedded mode, so you won’t be able to see the message in the ActiveMQ admin console.

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


Java 12/19, Spring Boot 2.4.3/2.7.17, Maven 3.6.3/3.8.5

Embedded ActiveMQ is not yet supported in Spring Boot 3.

Project Setup

You can create maven based project in your favorite IDE or tool. The name of the project is spring-jms-embedded-apache-activemq.

The pom.xml file with required dependency is given below:

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

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">







Message Producer

In point to point messaging domain you need message produces that will produce message.

public class MessageProducer {

	private JmsTemplate jmsTemplate;

	public void sendMessageToDestination(final String message) {


Message Consumer

There is one message consumer that consumes messages produced by the message producer.

public class MessageConsumer 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");


App Configuration

Create a file application.properties under src/main/resources folder with the following key/value pairs. Here I have specified message broker URL and destination queue name. The queue is used in the point-to-point messaging domain whereas topic is used in the publish/subscribe domain.


You need to configure various Spring beans for connectionfactory, jms template, destination, etc. I am showing you embedded ActiveMQ, so I am creating the BrokerService bean for this.

public class JmsConfig {

	private String brokerUrl;

	private String queueName;

	private MessageConsumer messageConsumer;

	public BrokerService broker() throws Exception {
		BrokerService broker = new BrokerService();
		return broker;

	public ConnectionFactory connectionFactory() {
		return new CachingConnectionFactory(new ActiveMQConnectionFactory(brokerUrl));

	public Destination destination() {
		return new ActiveMQQueue(queueName);

	public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, Destination destination) {
		JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);

		return jmsTemplate;

	public MessageListenerContainer defaultMessageListenerContainer(ConnectionFactory connectionFactory,
			Destination destination) {
		DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();

		return defaultMessageListenerContainer;


Main Class

A class is having @SpringBootApplication and main method enough to deploy the application using Spring Boot framework.

public class SpringJmsApacheActiveMqApp implements CommandLineRunner {

	private MessageProducer messageProducer;

	public static void main(String[] args) {
		SpringApplication.run(SpringJmsApacheActiveMqApp.class, args);

	public void run(String... args) throws Exception {
		messageProducer.sendMessageToDestination("Send this message to destination");


Testing the Spring Embedded ActiveMQ Application

Now you can start your Spring boot application by running the main class. You will see the following output on the console:

Message has been consumed : Send this message to destination

You won’t be able to check the ActiveMQ console as the Apache ActiveMQ is running in embedded mode. In the console you will see that the connector started Connector tcp:// started.

That’s all about how to work with Spring and embedded Apache ActiveMQ for JMS.

Source Code


Leave a Reply

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