亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

瀏覽:4日期:2023-05-24 10:36:56

寫在前面: 從2018年底開始學(xué)習(xí)SpringBoot,也用SpringBoot寫過一些項(xiàng)目。這里對學(xué)習(xí)Springboot的一些知識(shí)總結(jié)記錄一下。如果你也在學(xué)習(xí)SpringBoot,可以關(guān)注我,一起學(xué)習(xí),一起進(jìn)步。

ActiveMQ簡介

1、ActiveMQ簡介

Apache ActiveMQ是Apache軟件基金會(huì)所研發(fā)的開放源代碼消息中間件;由于ActiveMQ是一個(gè)純Java程序,因此只需要操作系統(tǒng)支持Java虛擬機(jī),ActiveMQ便可執(zhí)行。

2、ActiveMQ下載

下載地址:http://activemq.apache.org/components/classic/download/

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

下載完成后解壓雙擊activemq.bat文件打開(不用安裝,直接使用),目錄和打開后效果如下:

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

運(yùn)行后,瀏覽器訪問http://localhost:8161/地址進(jìn)入一下界面。

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

點(diǎn)擊Manage ActiveMQ broker登錄到ActiveMQ管理頁面,默認(rèn)賬號和密碼都是admin。管理頁面如下:

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

SpringBoot整合ActiveMQ

1、新建SpringBoot項(xiàng)目

新建Springboot項(xiàng)目,添加對應(yīng)的依賴。項(xiàng)目完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mcy</groupId> <artifactId>springboot-mq</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mq</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Activemq依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

2、項(xiàng)目結(jié)構(gòu)

3、相關(guān)配置信息

在application.properties類中添加ActiveMQ相關(guān)的配置信息

server.port=8080server.servlet.context-path=/mq#MQ服務(wù)器地址spring.activemq.broker-url=tcp://localhost:61616#用戶名spring.activemq.user=admin#密碼spring.activemq.password=admin#設(shè)置是Queue隊(duì)列還是Topic,false為Queue,true為Topic,默認(rèn)false-Queuespring.jms.pub-sub-domain=false#spring.jms.pub-sub-domain=true#變量,定義隊(duì)列和topic的名稱myqueue: activemq-queuemytopic: activemq-topic

4、ActiveMQ配置類

ActiveMQ配置類ConfigBean,配置了Queue隊(duì)列和topic兩種模式,代碼如下:

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.jms.annotation.EnableJms;import org.springframework.stereotype.Component;import javax.jms.Topic;/** 1. MQ配置類 */@Component@EnableJmspublic class ConfigBean { @Value('${myqueue}') private String myQueue; @Value('${mytopic}') private String topicName; //隊(duì)列 @Bean public ActiveMQQueue queue(){ return new ActiveMQQueue(myQueue); } //topic @Bean public Topic topic(){ return new ActiveMQTopic(topicName); }}Queue隊(duì)列模式

隊(duì)列模式即點(diǎn)對點(diǎn)傳輸。點(diǎn)對點(diǎn)消息傳遞域的特點(diǎn)如下:

每個(gè)消息只能有一個(gè)消費(fèi)者,類似于1對1的關(guān)系。好比個(gè)人快遞自己領(lǐng)自己的。

消息的生產(chǎn)者和消費(fèi)者之間沒有時(shí)間上的相關(guān)性。無論消費(fèi)者在生產(chǎn)者發(fā)送消息的時(shí)候是否處于運(yùn)行狀態(tài),消費(fèi)者都可以提取消息。好比我們的發(fā)送短信,發(fā)送者發(fā)送后不見得接收者會(huì)即收即看。

消息被消費(fèi)后隊(duì)列中不會(huì)再存儲(chǔ),所以消費(fèi)者不會(huì)消費(fèi)到已經(jīng)被消費(fèi)掉的消息。

1、隊(duì)列生產(chǎn)者

QueueProducerController類為隊(duì)列生產(chǎn)者控制器,主要向消息隊(duì)列中發(fā)送消息。代碼如下:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.jms.Queue;/* * 隊(duì)列消息生產(chǎn)者 */@RestControllerpublic class QueueProducerController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; /* * 消息生產(chǎn)者 */ @RequestMapping('/sendmsg') public void sendmsg(String msg) { System.out.println('發(fā)送消息到隊(duì)列:' + msg); // 指定消息發(fā)送的目的地及內(nèi)容 this.jmsMessagingTemplate.convertAndSend(this.queue, msg); }}

2、隊(duì)列消費(fèi)者

QueueConsumerController類為隊(duì)列消費(fèi)者控制器,具體代碼如下:

import org.springframework.beans.factory.annotation.Value;import org.springframework.jms.annotation.JmsListener;import org.springframework.web.bind.annotation.RestController;/* 1. 隊(duì)列queue消費(fèi)者控制器 */@RestControllerpublic class QueueConsumerController { /* * 消費(fèi)者接收消息 */ @JmsListener(destination='${myqueue}') public void readActiveQueue(String message) { System.out.println('接受到:' + message); }}

3、測試效果

運(yùn)行項(xiàng)目在瀏覽器中訪問http://localhost:8080/mq/sendmsg?msg=123。向消息隊(duì)列中發(fā)送123。控制臺(tái)輸出效果:

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

ActiveMQ控制臺(tái)顯示:

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

Number Of Pending Messages:消息隊(duì)列中待處理的消息 Number Of Consumers:消費(fèi)者的數(shù)量 Messages Enqueued:累計(jì)進(jìn)入過消息隊(duì)列的總量 Messages Dequeued:累計(jì)消費(fèi)過的消息總量

【注】隊(duì)列模式時(shí),配置文件application.properties中spring.jms.pub-sub-domain屬性必須設(shè)置為false。

Topic模式

topic模式基于發(fā)布/訂閱模式的傳輸。基于發(fā)布/訂閱模式的傳輸?shù)奶攸c(diǎn)如下:

生產(chǎn)者將消息發(fā)布到topic中,每個(gè)消息可以有多個(gè)消費(fèi)者,屬于1:N的關(guān)系; 生產(chǎn)者和消費(fèi)者之間有時(shí)間上的相關(guān)性。訂閱某一個(gè)主題的消費(fèi)者只能消費(fèi)自它訂閱之后發(fā)布的消息。 生產(chǎn)者生產(chǎn)時(shí),topic不保存消息它是無狀態(tài)的不落地,假如無人訂閱就去生產(chǎn),那就是一條廢消息。

1、topic生產(chǎn)者

TopicProducerController類為topic生產(chǎn)者控制器,主要向消息隊(duì)列中發(fā)送消息。代碼如下:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.jms.Queue;import javax.jms.Topic;/** topic消息生產(chǎn)者*/@RestControllerpublic class TopicProducerController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Topic topic; /* * 消息生產(chǎn)者 */ @RequestMapping('/topicsendmsg') public void sendmsg(String msg) { System.out.println('發(fā)送消息到MQ:' + msg); // 指定消息發(fā)送的目的地及內(nèi)容 this.jmsMessagingTemplate.convertAndSend(this.topic, msg); }}

2、topic消費(fèi)者

TopicConsumerController類為topic消費(fèi)者控制器,其中寫了兩個(gè)消費(fèi)者方法,可以理解為有兩個(gè)用戶訂閱。具體代碼如下:

import org.springframework.jms.annotation.JmsListener;import org.springframework.web.bind.annotation.RestController;/* 1. topic消費(fèi)者控制器 */@RestControllerpublic class TopicConsumerController { /* * 消費(fèi)者接收消息 */ @JmsListener(destination='${mytopic}') public void readActiveQueue(String message) { System.out.println('接受到:' + message); } @JmsListener(destination='${mytopic}') public void readActiveQueue1(String message) { System.out.println('接受到:' + message); }}

3、測試效果

運(yùn)行項(xiàng)目在瀏覽器中訪問http://localhost:8080/mq/topicsendmsg?msg=123。向消息隊(duì)列中發(fā)送123。控制臺(tái)輸出效果(有兩個(gè)消費(fèi)者方法):

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

ActiveMQ控制臺(tái)顯示:

詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)

Number Of Consumers:消費(fèi)者的數(shù)量 Messages Enqueued:累計(jì)進(jìn)入過消息隊(duì)列的總量 Messages Dequeued:累計(jì)消費(fèi)過的消息總量

【注】Topic模式時(shí),配置文件application.properties中spring.jms.pub-sub-domain屬性必須設(shè)置為true。

到此這篇關(guān)于詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)的文章就介紹到這了,更多相關(guān)Springboot整合ActiveMQ內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 在线观看精品视频网站www | 成 人免费视频l免费观看 | 富二代精品视频 | 一区二区日韩欧美 | 久久久这里只有精品加勒比 | 欧美成人午夜做爰视频在线观看 | 国产成年网站v片在线观看 国产成人aa在线视频 | 最新精品在线视频 | 国产人成精品 | 亚洲国产一区二区三区在线观看 | 免费女人18毛片a级毛片视频 | 日韩专区亚洲国产精品 | 玖玖在线免费视频 | 亚洲精品一区二三区在线观看 | 精品国产香蕉在线播出 | 亚洲精品一区最新 | 一级成人a免费视频 | 国产成人毛片视频不卡在线 | 一区二区三区精品国产 | 美女一级片 | 亚洲久草视频 | 久久99这里只有精品国产 | 国产亚洲精品国产 | 欧美三级日韩三级 | 国产福利一区二区三区 | 99久久成人 | 亚洲综合国产 | 日本亚欧乱色视频在线网站 | 操亚洲 | 亚欧国产 | 国产亚洲精品看片在线观看 | 97视频免费公开成人福利 | 久久精品国产一区二区三区 | 欧美资源在线观看 | 亚洲欧洲日产国码一级毛片 | 99热在线获取最新地址 | 亚洲视频国产视频 | 日本道综合一本久久久88 | 日本性色 | 国产精品久久久亚洲 | 91免费看国产 |