SpringBoot整合ActiveMq實現Queue和Topic兩種模式(看不懂你來打我)


一、前言

最近小編在學習消息隊列,然后選中了ActiveMq,來進行學習.於是探索了好久,來整理一下自己的學習心得!大家一起學習,希望對你有用.我把一些我自己的理解寫在注釋里了注意看!!

二、ActiveMq的下載和使用

  • 下載

大家直接下載解壓就可以使用了--->
鏈接:https://pan.baidu.com/s/1W0MZtQAya0mOEKMWqJK1iA
提取碼:29mz

  • 使用
    在這里插入圖片描述

三、依賴准備

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

四、yml文件配置

spring:
 activemq:
  broker-url: tcp://127.0.0.1:61616
  user: admin
  password: admin
  jms:
   pub-sub-domain: true # 默認為false:queue   true:topic
queue: queue_mq # 點對點消費名字
topic: topic_mq # 訂閱式消費名字

五、配置Bean

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
@EnableJms
public class ActiveMqConfig {

    @Value("${queue}")//對應yml文件中定義的queue
    private String queue;

    @Value("${topic}")//對應yml文件中定義的topic
    private String topic;
    /**
     * 創建點對點的隊列  一個消息只能被一個消費者消費  --- 一對一
     * @return
     */
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(queue);
    }
    /**
     * 創建訂閱式的隊列  一個消息可以被多個消費者消費 --- 一對多
     * @return
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic(topic);
    }
}

六、創建生產者(Queue+Topic)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

@RestController
public class ProducerController {

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 點對點的消息隊列的生產者
     * @param string
     */
    @GetMapping("/queue")
    public void sendMsgQueue(@RequestParam String string){
        System.out.println("消息已經發送,准備被消費,消息為 ---> "+string);
        jmsMessagingTemplate.convertAndSend(queue,string);
    }

    /**
     * 一對多的消息隊列的生產者
     * @param string
     */
    @GetMapping("/topic")
    public void sendMsgTopic(@RequestParam String string){
        System.out.println("消息已經發送,准備被消費,消息為 ---> "+string);
        jmsMessagingTemplate.convertAndSend(topic,string);
    }

}

七、創建消費者(Topic模式下)

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

@Component
public class TopicConsumer {
    
    /**
     * 監聽消息,名字為生產者發送的名字,要一致,不然監聽不到.
     * 因為是訂閱者模式,可以有多個消費者,我們這里舉兩個來進行測試
     * @param string
     */
    @JmsListener(destination = "${topic}")
    public void consumerTopicOne(String string){

        System.out.println("我是消費者一號:消費消息成功,信息為---> "+string);

    }

    @JmsListener(destination = "${topic}")
    public void consumerTopicTwo(String string){

        System.out.println("我是消費者二號:消費消息成功,信息為---> "+string);

    }
}

八、測試結果(Topic模式下)

在這里插入圖片描述

九、ActiveMq網頁版查看是否成功(Topic模式下)

網站地址 http://127.0.0.1:8161/admin/ 賬號密碼都是admin

在這里插入圖片描述
在這里插入圖片描述

十、創建消費者(Queue模式下)

首先把yml文件中的配置修改為Queue:pub-sub-domain: false

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

@Component
public class QueueConsumer {

    /**
     * 監聽消息,名字為生產者發送的名字,要一致,不然監聽不到.
     * 因為是隊列模式,只能消費者
     * @param string
     */
    @JmsListener(destination = "${queue}")
    public void consumerQueue(String string){

        System.out.println("消費消息成功,信息為---> "+string);
    }

}

十一、測試結果(Queue模式下)

在這里插入圖片描述

十二、ActiveMq網頁版查看是否成功(Queue模式下)

在這里插入圖片描述

十三、總結

這樣我們就搭建好了,並且測試沒有問題,有問題留言哦.比較合適剛剛學習的童鞋們,期待您的關注,一起學習,一起提高哦!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM