Spring Boot integrates JMS (ActiveMQ)

Spring Boot integrates JMS (ActiveMQ)

1. Introduction to JMS

JMS (Java Message Service) is the Java message service. It unifies the standards at the JAVA API level so that multiple clients can interact through JMS. Most message middleware providers provide support for JMS. The relationship between JMS and ActiveMQ is like the relationship between JDBC and JDBC drivers. JMS includes two message models: point-to-point and publisher/subscriber, while JMS only supports the Java platform.

2. Spring Boot integrates JMS

Since JMS is a set of standards, the integration of JMS by Spring Boot must be an implementation of integrating JMS. In this case, ActiveMQ is used as an example to see how Spring Boot integrates.

2.1 Add dependencies

First edit the project's pom.xml file and add ActiveMQ dependencies:


2.2 Configuring ActiveMQ

(1) First configure ActiveMQ connection information in application.properties:

# The broker address, the default port is 61616, which can only be written as tcp://
# Trust all packages, this configuration is to support sending object messages
# ActiveMQ username
# ActiveMQ password
# queue and topic cannot be used at the same time. When using topic, uncomment the following line

(2) Then provide two message queue beans (corresponding to Queue and Topic modes respectively) in the project configuration class. These Bean instances are provided by ActiveMQ: The main difference between Queue mode and Topic mode is whether it can be consumed repeatedly:

  • Queue is a point-to-point mode: even if there are multiple consumers, only one consumer can consume a message, and other consumers cannot consume the message after consumption. When the consumer does not exist, the message will be kept until there is consumption.
  • Topic is a publish-subscribe model: multiple message consumers (subscribing to the topic) are allowed to consume the same message. If the subscriber does not exist, the message will not be saved after it is sent, and it will be lost directly (not landed)
public class JmsConfig {
    // Destination in Queue mode
    public Queue queue(){
        return new ActiveMQQueue("amq.queue");
    // Destination in Topic Mode
    public Topic topic(){
        return new ActiveMQTopic("amq.topic");

2.3 Create a producer

(1) Here we use the JMS message sending template JmsMessagingTemplate provided by Spring for message sending, which sends an object message:

public class ProducerController {
    // JMS message sending template
    private JmsMessagingTemplate jmsMessagingTemplate;
    private Queue queue;
    private Topic topic;
    // Send Queue message
    public void sendQueueMsg(Book book) {
        this.jmsMessagingTemplate.convertAndSend(this.queue, book);
    // Send Topic message
    public void sendTopicMsg(Book book) {
        this.jmsMessagingTemplate.convertAndSend(this.topic, book);

(2) The message object Book sent here is an ordinary JavaBean, and the specific code is as follows:

public class Book implements Serializable {
    private Integer id;
    private String name;

2.4 Create a consumer

Add the @JmsListener annotation to the method to indicate that the method is a message consumer, and the destination parameter indicates the message destination subscribed by the message consumer:

public class Consumer {
    // Listen to and read queue messages
    public void readActiveQueue(Book book) {
        System.out.println("received queue information:" + book);
    // Listen to and read topic messages
    public void readActiveTopic(Book book) {
        System.out.println("received topic information:" + book);

3. Run the test

3.1 Queue mode

(1) Start the project and use a browser to access the following address:


(2) You can see the console output the following information:

3.2 Topic mode

(1) When using Topic mode, we need to release the configuration of spring.jms.pub-sub-domain=true first:

(2) Start the project and use a browser to access the following address:


(3) You can see the console output the following information:

Tags: Java Spring Boot

Posted by jasonok6 on Wed, 18 May 2022 22:23:11 +0300