SpringBoot整合ActiveMQ

SpringBoot整合ActiveMQ

IDEA 创建 SpringBoot 项目,因为 SpringBoot 已经内置了对 ActiveMQ 的支持,所以直接引入依赖 spring-boot-starter-activemq 就行。整体项目结构如下:

1、 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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>com.zhisheng</groupId>
    <artifactId>activemq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>activemq</name>
    <description>Demo project for Spring Boot ActiveMQ</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、 配置文件 application.properties

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

3、 发送消息类

package com.zhisheng.activemq.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class ActiveMQClient {

    @Autowired
    private JmsTemplate jmsTemplate;

    public void send(String message) {
        jmsTemplate.convertAndSend("zhisheng", message);
    }
}

同样,和 RabbitMQ 类似,不多说了。

4、 消息接收类

package com.zhisheng.activemq.server;

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

@Component
public class ActiveMQServer {
    @JmsListener(destination = "zhisheng")
    public void receive(String message) {
        System.out.println("收到的 message 是:" + message);
    }
}

5、 注意

这个队列是不需要我们提前定义好的,它和 RabbitMQ 不一样,它会在我们需要的时候动态的创建。

运行

package com.zhisheng.activemq;

import com.zhisheng.activemq.client.ActiveMQClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StopWatch;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class ActivemqApplication {

    @Autowired
    ActiveMQClient client;

    @PostConstruct
    public void init() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < 10000; i++) {
            client.send("发送消息----zhisheng-----");
        }
        stopWatch.stop();
        System.out.println("发送消息耗时: " + stopWatch.getTotalTimeMillis());
    }

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

发送一万条消息运行后需要的时间挺久的:73180 ms

比 RabbitMQ 发送 10000 条消息耗时 215 ms 不知道高出多少倍了,可见其性能并不高的。


作者:foreknow
链接:https://www.jianshu.com/p/df37e18b21ca
来源:简书