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:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

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://
spring.activemq.broker-url=tcp://127.0.0.1:61616
# Trust all packages, this configuration is to support sending object messages
spring.activemq.packages.trust-all=true
# ActiveMQ username
spring.activemq.user=admin
# ActiveMQ password
spring.activemq.password=admin
# queue and topic cannot be used at the same time. When using topic, uncomment the following line
#spring.jms.pub-sub-domain=true

(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)
@Configuration
public class JmsConfig {
 
    // Destination in Queue mode
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("amq.queue");
    }
 
    // Destination in Topic Mode
    @Bean
    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:

@RestController
public class ProducerController {
 
    // JMS message sending template
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
 
    @Autowired
    private Queue queue;
 
    @Autowired
    private Topic topic;
 
    // Send Queue message
    @GetMapping("/sendQueueMsg")
    public void sendQueueMsg(Book book) {
        this.jmsMessagingTemplate.convertAndSend(this.queue, book);
    }
 
    // Send Topic message
    @GetMapping("/sendTopicMsg")
    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:

@Data
@ToString
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:

@Component
public class Consumer {
 
    // Listen to and read queue messages
    @JmsListener(destination="amq.queue")
    public void readActiveQueue(Book book) {
        System.out.println("received queue information:" + book);
    }
 
    // Listen to and read topic messages
    @JmsListener(destination="amq.topic")
    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:

http://localhost:8080/sendQueueMsg?id=1&name=suohe

(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:

http://localhost:8080/sendTopicMsg?id=2&name=wanghua

(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