SpringBoot整合ActiveMQ的publish/subscribe发布订阅模式(二)

(注意:当使用了发布订阅模式那么P2P模式就不能用了,解决的方法:)

(1)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>

 

(2)application.properties文件配置

  

1
2
3
4
5
6
7
8
9
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
#支持发布订阅模型,默认只支持点对点,使用了spring.jms.pub-sub-domain=true那么点对点模式就失效了
spring.jms.pub-sub-domain=true

  

(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)发布者Publish类 

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
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 Publish {
     
    // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
 
 
        // 发送消息,destination是发送到的队列名称,message是待发送的消息 
        public void sendTopicMessage(Destination destination, String message){ 
            jmsMessagingTemplate.convertAndSend(destination, message); 
        
 
        // 发送消息,destination是发送到的队列名称,map类型的发送的消息 
            public void sendTopicMapMessage(Destination destination, Map<String ,String> map){ 
                jmsMessagingTemplate.convertAndSend(destination, map); 
            
     
     
 
}

  

(6)订阅者Subscribe类

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
package com.qingfeng.consumer;
 
import java.util.Map;
 
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
 * 订阅者
 */
@Component
public class Subscribe {
 
    // 使用JmsListener配置消费者监听的topic名称qingfeng-topic,其中text是接收到的消息 
        @JmsListener(destination = "qingfeng-topic"
        public void receiveTopic(String text) { 
            System.out.println("qingfeng-topic类型Subscribe收到的Publish的报文为:"+text); 
        
         
        @JmsListener(destination = "qingfeng-topic"
        public void receiveTopic2(String text) { 
            System.out.println("qingfeng-topic类型Subscribe收到的Publish的报文为:"+text); 
        
         
        @JmsListener(destination = "qingfeng-topic"
        public void receiveTopic3(String text) { 
            System.out.println("qingfeng-topic类型Subscribe收到的Publish的报文为:"+text); 
        
 
        // 使用JmsListener配置消费者监听的topic名称qingfeng-map-topic,其中map是接收到的消息
        @JmsListener(destination = "qingfeng-map-topic"
        public void receiveMapTopic(Map<String ,String> map) { 
            System.out.println("qingfeng-map-topic类型Subscribe收到的Publishmap类型的报文为:"+map); 
        
         
        @JmsListener(destination = "qingfeng-map-topic"
        public void receiveMapTopic2(Map<String ,String> map) { 
            System.out.println("qingfeng-map-topic类型Subscribe收到的Publishmap类型的报文为:"+map); 
        
 
     
}

  

  

(7)测试类TopicController类

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 javax.jms.Destination;
 
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
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.Publish;
 
@RestController
public class TopicController {
     
    @Autowired
    private Publish publish;
     
//  http://localhost:8088/springboot-ActiveMQ/topic/send?text=qingfeng
    @GetMapping("topic/send")
    public String  send(String text){
        //创建一个Topic名称为qingfeng-topic
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("qingfeng-topic");
        //调用生产者产生消息
        publish.sendTopicMessage(activeMQTopic, text);
        return "发送成功";
    }
     
// http://localhost:8088/springboot-ActiveMQ/topic/sendMap
    @GetMapping("topic/sendMap")
    public String  sendMap(){
        //创建一个Topic名称为qingfeng-map-topic
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("qingfeng-topic");
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "qingfeng");
        map.put("age", "18");
        map.put("QQ", "37942135");
        //调用生产者产生消息
        publish.sendTopicMapMessage(activeMQTopic, map);
        return "发送成功";
    }
 
}

  

8)启动项目AppStart类

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

 在控制台上输出

 

 

测试:http://localhost:8088/springboot-ActiveMQ/topic/sendMap

在控制台上输出

 

posted @   Amy清风  阅读(327)  评论(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代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示