Independent ActiveMQ handbook · archive of the 2004–2007 project site
Independent guide. Not the Apache ActiveMQ project site and not affiliated with the Apache Software Foundation. ActiveMQ and Apache are trademarks of the ASF. Official documentation: activemq.apache.org.

activemq.org › Handbook › ActiveMQ Spring Boot setup: starter, JmsTemplate and @JmsListener

Handbook

ActiveMQ Spring Boot setup: starter, JmsTemplate and @JmsListener

Checked against ActiveMQ Classic 6.4.0, 6.3.3 and 5.19.12 and Apache Artemis 2.57.0 on . Independent guide; sources are listed at the end. Official documentation: activemq.apache.org and artemis.apache.org.

To use ActiveMQ from Spring Boot, add spring-boot-starter-activemq, point spring.activemq.broker-url at your broker, then inject the auto-configured JmsTemplate to send and annotate a method with @JmsListener to receive. In Spring Boot 4.2.0, the current release, the starter brings in the ActiveMQ Classic 6.3.3 client; Apache Artemis (formerly ActiveMQ Artemis) has a separate starter, spring-boot-starter-artemis.

Everything below follows the Spring Boot and Spring Framework reference documentation on docs.spring.io, listed under Sources. Broker-side setup is in the official ActiveMQ documentation at activemq.apache.org and in Install ActiveMQ.

Which Spring Boot starter: ActiveMQ Classic or Artemis?

BrokerStarterPropertiesVersion managed by Spring Boot 4.2.0
ActiveMQ (Classic)spring-boot-starter-activemqspring.activemq.*6.3.3
Apache Artemisspring-boot-starter-artemisspring.artemis.*2.57.0

The Artemis starter is still called spring-boot-starter-artemis, and the Spring Boot 4.2 starter list describes it as the starter "for using Apache Artemis and JMS". This page covers the Classic starter. For the differences between the two brokers, see ActiveMQ Classic vs Artemis.

Does Spring Boot 3 support ActiveMQ?

Yes, with a gap at the start of the 3.x line:

  • Spring Boot 3.0 removed ActiveMQ client auto-configuration.
  • Spring Boot 3.1 restored it. Embedded broker support was not restored at that point, because ActiveMQ's broker did not yet support JMS 3.0.
  • Spring Boot 3.4 brought back the embedded broker. Unlike Spring Boot 2.7.x, the starter is client only: to run an embedded broker you add org.apache.activemq:activemq-broker yourself.
  • Spring Boot 4.2 auto-configures Spring's newer JmsClient, and JmsTemplate can still be injected as before.

Maven dependency

Spring Boot manages the versions of the dependencies it supports, so the starter needs no version when you use the Spring Boot parent or BOM. For Spring Boot 4.2.0, in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

For tests, Spring Boot 4.2 also lists spring-boot-starter-activemq-test. To run an embedded broker inside your application, add org.apache.activemq:activemq-broker as well.

Configure the broker connection

ActiveMQ settings live under spring.activemq.*. To connect to a standalone broker on the same machine, adapted from the Spring Boot reference example (which uses a different host and port):

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=secret

The ActiveMQ Spring support page uses the same tcp://localhost:61616 URL in its example; if your broker listens elsewhere, see ActiveMQ ports. Leave out the user and password lines if your broker does not require a login. Setting a broker URL also disables the embedded broker.

The properties you are most likely to need, from the Spring Boot 4.2 application properties appendix:

PropertyWhat it doesDefault
spring.activemq.broker-urlURL of the brokerauto-generated
spring.activemq.user, spring.activemq.passwordLogin for the brokernone
spring.activemq.embedded.enabledUse an embedded broker if activemq-broker is on the classpathtrue
spring.activemq.packages.trust-all, spring.activemq.packages.trustedTrust all packages, or only the listed onesnone
spring.activemq.pool.enabledCreate a JmsPoolConnectionFactory instead of a plain connection factoryfalse
spring.jms.pub-sub-domainMake topics the default destination typefalse
spring.jms.listener.min-concurrency, spring.jms.listener.max-concurrencyNumber of concurrent consumers per listenernone

Send a message with JmsTemplate

Spring Boot auto-configures a JmsTemplate that you can inject. For Spring Boot 4.2.0, following the injection pattern of the Spring Boot JMS reference and using JmsTemplate.convertAndSend(String, Object):

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class OrderSender {

    private final JmsTemplate jmsTemplate;

    public OrderSender(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void send(String text) {
        this.jmsTemplate.convertAndSend("orders", text);
    }
}

convertAndSend sends the object to the named destination after converting it to a JMS message with the configured MessageConverter. If you define a MessageConverter or DestinationResolver bean, Spring Boot attaches it to the auto-configured template.

On Spring Boot 4.2 you can use the auto-configured JmsClient instead. From the Spring Boot 4.2 JMS reference:

this.jmsClient.destination("myQueue").send("Hello");

Receive messages with @JmsListener

When JMS is on the classpath, any bean method annotated with @JmsListener becomes a listener endpoint, and Spring Boot creates a default listener container factory if you have not defined one. No @EnableJms is needed. For Spring Boot 4.2.0, based on the reference example:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class OrderListener {

    @JmsListener(destination = "orders")
    public void processMessage(String content) {
        // handle the order
    }
}

The default factory is transactional: when no JTA transaction manager is present, it enables transacted sessions. To listen on a topic rather than a queue, set spring.jms.pub-sub-domain=true, which makes topics the default destination type, or define your own factory.

Connection pooling: CachingConnectionFactory, pooled-jms and PooledConnectionFactory

Spring Boot gives you caching by default. Its auto-configuration wraps the ActiveMQ connection factory in a CachingConnectionFactory, tuned through spring.jms.cache.*; for example, spring.jms.cache.session-cache-size defaults to 1. Listener containers are configured against the unwrapped native factory, so each container manages its own connection.

For real pooling, the Spring Boot reference uses pooled-jms: add the org.messaginghub:pooled-jms dependency and enable the pool.

spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=50

Spring Boot then creates a JmsPoolConnectionFactory. The max-connections default is 1 and max-sessions-per-connection defaults to 500, so set them for your load.

ActiveMQ's own Spring support page takes a different route for XML-configured Spring applications: it recommends org.apache.activemq.pool.PooledConnectionFactory from the activemq-pool library, which pools connections, sessions and producers for use with JmsTemplate. Spring Boot's spring.activemq.pool.* properties configure pooled-jms, not that class. The 2006 archive page on Spring support shows the original XML approach.

Common problems

  • Messages go to an in-memory broker instead of your server. If activemq-broker is on the classpath and no broker URL is set, Spring Boot uses the vm transport and starts a broker inside your application. Set spring.activemq.broker-url, or spring.activemq.embedded.enabled=false.
  • Connection refused on 61616. The broker is not running, is listening on another port, or a firewall or container mapping is in the way. Check with the steps in ActiveMQ ports.
  • A topic listener never receives anything. The default destination type is a queue; set spring.jms.pub-sub-domain=true.
  • Destinations you never created appear on the broker. By default ActiveMQ creates a destination the first time it is used, so a typo in a destination name creates a new queue.

A plain-Java version of the same send and receive, without Spring, is in the ActiveMQ tutorial.

Frequently asked questions

What is the Spring Boot starter for ActiveMQ?

spring-boot-starter-activemq in the org.springframework.boot group. It provides the dependencies to connect to an ActiveMQ Classic broker plus Spring's JMS infrastructure; Spring Boot 4.2.0 manages the ActiveMQ 6.3.3 client version for you.

Does Spring Boot 3 support ActiveMQ?

Yes. Spring Boot 3.0 removed ActiveMQ client auto-configuration, 3.1 restored it, and 3.4 restored embedded broker support, which needs org.apache.activemq:activemq-broker added to your application.

What is the Spring Boot starter for Artemis?

spring-boot-starter-artemis, configured through spring.artemis.* properties. Spring Boot 4.2 describes it as the starter for using Apache Artemis and JMS and manages Artemis 2.57.0.

How do I use PooledConnectionFactory with Spring Boot?

Spring Boot uses a CachingConnectionFactory by default. For pooling it uses pooled-jms instead of ActiveMQ's PooledConnectionFactory: add org.messaginghub:pooled-jms and set spring.activemq.pool.enabled=true to get a JmsPoolConnectionFactory.

How do I send a message with JmsTemplate in Spring Boot?

Inject the auto-configured JmsTemplate and call convertAndSend with a destination name and the payload, for example convertAndSend("orders", text). Receive it with a method annotated @JmsListener(destination = "orders").

Related guides

From the 2006 archive

The original ActiveMQ documentation on these topics, kept as it was published here in 2004–2007. It describes ActiveMQ 4.x and older.

Sources

  1. Spring Boot reference: JMS docs.spring.io
  2. Spring Boot reference: Build Systems (starters) docs.spring.io
  3. Spring Boot appendix: Common Application Properties docs.spring.io
  4. Spring Boot 3.1 Release Notes github.com
  5. Spring Boot 3.4 Release Notes github.com
  6. Spring Boot 4.2 Release Notes github.com
  7. Spring Framework API: JmsTemplate docs.spring.io
  8. ActiveMQ Classic: Spring Support activemq.apache.org