SpringBoot整合ActiveMQ的p2p模式(一)

1.安装ActiveMQ

官网下载地址:http://activemq.apache.org/download.html

(1)在Windows上在解压后有个bin下有win64,win64目录下有个activemq.bat,双击activemq.bat启动

 

 

 

(2)访问http://127.0.0.1:8161/

 

 

 (3)输入用户名:admin   密码:admin

(4)看到ActiveMQ这就启动成功了

 

p2p的过程则理解起来更加简单。它好比是两个人打电话,这两个人是独享这一条通信链路的。一方发送消息,另外一方接收,就这么简单。在实际应用中因为有多个用户对使用p2p的链路。

 

2.SpringBoot整合ActiveMQ

(1)这是我的项目结构

 

 

 (2)pom.xml完整的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<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.qingfeng</groupId>
    <artifactId>springboot-ActiveMQ</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.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>
 
            <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
 
    </dependencies>
 
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

 

(3)application.properties文件配置

1
2
3
4
5
6
7
server.port=8088
server.servlet.context-path=/springboot-ActiveMQ/
#不能用127.0.0.1,不然会报错
#spring.activemq.broker-url=tcp://127.0.0.1:61616 
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true 
spring.activemq.pool.enabled=false

  

(4)SpringBoot启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.qingfeng;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class AppStart {
 
    public static void main(String[] args) {
        SpringApplication.run(AppStart.class, args);
    }
     
}

  

(5)生产者Producer类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.qingfeng.producer;
 
import java.util.Map;
 
import javax.jms.Destination;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
 
/**
 *生产者
 */
@Service
public class Producer {
 
    // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
 
 
    // 发送消息,destination是发送到的队列名称,message是待发送的消息 
    public void sendMessage(Destination destination, String message){ 
        jmsMessagingTemplate.convertAndSend(destination, message); 
    
 
    // 发送消息,destination是发送到的队列名称,map类型的发送的消息 
        public void sendMapMessage(Destination destination, Map<String ,String> map){ 
            jmsMessagingTemplate.convertAndSend(destination, map); 
        
     
}

  

(6)消费者Consumer类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.qingfeng.consumer;
 
import java.util.Map;
 
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
 *消费者
 */
@Component
public class Consumer {
 
    // 使用JmsListener配置消费者监听的队列名称qingfeng.queue,其中text是接收到的消息 
    @JmsListener(destination = "qingfeng-queue"
    public void receiveQueue(String text) { 
        System.out.println("Consumer收到的生产者的报文为:"+text); 
    
 
    // 使用JmsListener配置消费者监听的队列名称qingfeng-map-queue,其中map是接收到的消息
    @JmsListener(destination = "qingfeng-map-queue"
    public void receiveMapQueue(Map<String ,String> map) { 
        System.out.println("Consumer收到的生产者map类型的报文为:"+map); 
    
     
}

  

(7)测试类TestController

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.qingfeng.controller;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.jms.Destination;
 
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.qingfeng.producer.Producer;
 
@RestController
public class TestController {
 
    @Autowired
    private Producer producer;
 
    //   http://localhost:8088/springboot-ActiveMQ/send?text=qingfeng
    @GetMapping("send")
    public String  send(String text){
        //创建一个队列名称为qingfeng-queue
        Destination destination = new ActiveMQQueue("qingfeng-queue");
        //调用生产者产生消息
        producer.sendMessage(destination, text);
        return "发送成功";
    }
     
//  http://localhost:8088/springboot-ActiveMQ/sendMap
    @GetMapping("sendMap")
    public String  sendMap(){
        //创建一个队列名称为qingfeng-map-queue
        Destination destination = new ActiveMQQueue("qingfeng-map-queue");
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "qingfeng");
        map.put("age", "18");
        map.put("QQ", "37942135");
        //调用生产者产生消息
        producer.sendMapMessage(destination, map);
        return "发送成功";
    }
     
}

 

  

(8)启动项目AppStart类

测试:http://localhost:8088/springboot-ActiveMQ/send?text=qingfeng

 

 

 (9)在控制台上输出

 

 (10)测试:http://localhost:8088/springboot-ActiveMQ/sendMap

 

 (10)在控制台上输出

 

 

(11)可以看到activemq就多了两个qingfeng-queue队列和qingfeng-map-queue队列了

 

 

SpringBoot整合activemq就完成了。

 

posted @   Amy清风  阅读(499)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示