Integrating ActiveMQ with Spring Boot for Asynchronous Messaging
This guide explains how to use Spring Boot's ActiveMQ starter to configure, code, and test both queue (point‑to‑point) and topic (publish‑subscribe) messaging, covering JMS fundamentals, project setup, bean definitions, listener annotations, and dual‑mode container factories.
Background
In many business scenarios (e.g., user withdrawal, accounting, SMS notification, bank transfer) the downstream operations are time‑consuming. To improve concurrency and response speed these steps are usually off‑loaded to an asynchronous message queue.
JMS Specification
JMS (Java Message Service) is a Java API that abstracts messaging middleware. It defines two messaging models:
Point‑to‑point (queue) – a message is consumed by a single consumer.
Publish‑subscribe (topic) – the same message can be delivered to multiple subscribers.
ActiveMQ Overview
ActiveMQ is an open‑source implementation of the JMS specification. It provides high availability, strong performance, scalability and security, and supports both queue and topic modes with many extra features.
Spring Boot Integration
Spring Boot offers the starter spring-boot-starter-activemq for automatic configuration. Optionally, activemq-pool can be added for connection pooling.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>If pooling is required, add:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>Configuration in application.properties:
# In‑memory broker (convenient for tests)
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
# Standalone broker (production)
#spring.activemq.broker-url=tcp://127.0.0.1:61616
#spring.activemq.user=admin
#spring.activemq.password=adminQueue (Point‑to‑Point) Example
Define a Queue bean, then use JmsMessagingTemplate to send messages. A consumer is created with @JmsListener.
@Configuration
public class MyMqConfig {
@Bean
public Queue queue() {
return new ActiveMQQueue("sms.queue");
}
}
@Component
public class Producer {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource
private Queue queue;
public void sendMsg(String msg) {
System.out.println("Sending message: " + msg);
jmsMessagingTemplate.convertAndSend(queue, msg);
}
}
@Component
public class Consumer {
@JmsListener(destination = "sms.queue")
public void receiveMsg(String text) {
System.out.println("Received message: " + text);
}
}JUnit test to verify the flow:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqTests {
@Autowired
private Producer producer;
@Test
public void sendSimpleQueueMessage() {
producer.sendMsg("Withdrawal 200.00元");
}
}The test prints both the send and receive logs, confirming successful messaging. In in‑memory mode an informational javax.jms.JMSException: peer (vm://localhost#1) stopped. may appear but does not affect functionality.
Topic (Publish‑Subscribe) Example
Enable publish‑subscribe mode: spring.jms.pub-sub-domain=true Define a Topic bean:
@Bean
public Topic topic() {
return new ActiveMQTopic("sms.topic");
}Producer sends to the topic, and two consumer methods demonstrate multiple subscribers:
@Component
public class Producer {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource
private Topic topic;
public void sendTopic(String msg) {
System.out.println("Sending Topic message: " + msg);
jmsMessagingTemplate.convertAndSend(topic, msg);
}
}
@Component
public class Consumer {
@JmsListener(destination = "sms.topic")
public void receiveTopic1(String text) {
System.out.println("receiveTopic1 got: " + text);
}
@JmsListener(destination = "sms.topic")
public void receiveTopic2(String text) {
System.out.println("receiveTopic2 got: " + text);
}
}JUnit test:
@Test
public void sendSimpleTopicMessage() {
producer.sendTopic("Withdrawal 200.00元");
}Console output shows the send log followed by both subscriber logs, confirming broadcast delivery.
Supporting Both Queue and Topic Simultaneously
When an application needs both modes, define two JmsListenerContainerFactory beans – one with setPubSubDomain(false) for queues and another with setPubSubDomain(true) for topics. Listeners then specify the appropriate containerFactory attribute.
@Bean("queueListenerFactory")
public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory cf) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(cf);
factory.setPubSubDomain(false);
return factory;
}
@Bean("topicListenerFactory")
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory cf) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(cf);
factory.setPubSubDomain(true);
return factory;
}Listeners reference the factories:
@JmsListener(destination = "sms.queue", containerFactory = "queueListenerFactory")
public void receiveMsg(String text) { ... }
@JmsListener(destination = "sms.topic", containerFactory = "topicListenerFactory")
public void receiveTopic1(String text) { ... }Additional Practical Notes
ActiveMQ default TCP port is 61616.
Enable topic mode with spring.jms.pub-sub-domain=true.
Queues persist messages when no consumer is present; topics deliver only to currently connected subscribers.
When sending Java objects, they must implement Serializable and be received as ObjectMessage.
The containerFactory attribute on @JmsListener allows mixing queue and topic listeners in the same application.
Queue name ( sms.queue) and topic name ( sms.topic) can be externalized to configuration properties.
Reference Repository
https://github.com/secbr/springboot-learn/tree/master/springboot-activemq
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
