24. Springboot integrates ActiveMQ

Time:2021-1-10

Message queue middleware is an important component in distributed system, which mainly solves the problems of application coupling, asynchronous message, traffic cutting and so on. To achieve high performance, high availability, scalable and ultimately consistent architecture; it is an indispensable middleware for large-scale distributed systems. At present, ActiveMQ, rabbitmq, Kafka, rocketmq, metamq and so on are widely used. Spring boot provides support for JMS system; spring boot is convenient to integrate these message middleware.
There are two types of asynchronous messages in practical applications
JMS: the representative work is ActiveMQ, but its performance is not high, because it is implemented by Java program.
AMQP: a message component implemented directly by protocol. Its popular representative is rabbitmq, and its high-performance representative is Kafka.

1. New project, corresponding pom.xml The document is as follows

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-cloud</groupId>
    <artifactId>sc-activeMQ</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-activeMQ</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.7.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2. New springboot startup class ActiveMqApplication.java

package sc.activemq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ActiveMqApplication {

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

}

3. New profile application.yml

server:
  port: 9080

spring:
  appliction:
   name: sc-activemq
  activemq:
    broker-url: tcp://localhost:61616
    in-memory: true  
    user: admin
    password: admin
    pool:
      enabled: true
      max-connections: 50
      expiry-timeout: 10000
      idle-timeout: 30000
  jms: 
    Pub sub domain: false # by default, ActiveMQ provides queue mode. To use topic mode, you need to configure pub sub domain as true

Note: by default, ActiveMQ provides the queue mode. If you want to use the topic mode, you need to configure it spring.jms.pub -Sub domain is true

4. New consumer producers

package sc.activemq.service.impl;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import sc.activemq.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Override
    public void sendMessage(Destination destination, String message) {
        jmsMessagingTemplate.convertAndSend(destination, message);
    }

}

5. New message consumer

Queue mode:

package sc.activemq.service.consumer;

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

@Component
public class ConsumerQueue {

    //Use jmslistener to configure the queue that consumers listen to, where text is the received message
    @JmsListener(destination = "jms-queue")
    public void receiveQueue(String text) {
        System.out.println (consumerqueue received: + text);
    }
}

Subscription mode:

package sc.activemq.service.consumer;

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

@Component
public class ConsumerTopic {

    //Use jmslistener to configure the queue that consumers listen to, where text is the received message
    @JmsListener(destination = "jms-topic")
    public void receiveQueue(String text) {
        System.out.println (consumertopic received: + text);
    }
}

6. New test class

package sc.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import sc.activemq.service.ProductService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqTest {

    @Autowired
    private ActiveMQQueue queue;

    @Autowired
    private ActiveMQTopic topic;

    @Autowired
    private ProductService productService;

    @Test
    public void testJms() {
        String msgqueue = send gold;
        for(int i=0; i<5; i++){
            productService.sendMessage(this.queue, msgQueue+i);
        }
        String msgtopic = send silver;
        for(int i=0; i<5; i++){
            productService.sendMessage(this.topic, msgTopic+i);
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
    }

}

7. Test
First log in to ActiveMQ management platform: http://localhost :8161/
Queue mode:
(1) Configuration spring.jms.pub -Sub domain is false
24. Springboot integrates ActiveMQ
(2) Note the following code for the test class
24. Springboot integrates ActiveMQ
(3) Run test class
24. Springboot integrates ActiveMQ

Subscription mode:
(1) Configuration spring.jms.pub -Sub domain is true
24. Springboot integrates ActiveMQ
(2) Note the following code for the test class
24. Springboot integrates ActiveMQ
(3) Run test class
24. Springboot integrates ActiveMQ