詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)
寫在前面: 從2018年底開始學習SpringBoot,也用SpringBoot寫過一些項目。這里對學習Springboot的一些知識總結記錄一下。如果你也在學習SpringBoot,可以關注我,一起學習,一起進步。
ActiveMQ簡介1、ActiveMQ簡介
Apache ActiveMQ是Apache軟件基金會所研發的開放源代碼消息中間件;由于ActiveMQ是一個純Java程序,因此只需要操作系統支持Java虛擬機,ActiveMQ便可執行。
2、ActiveMQ下載
下載地址:http://activemq.apache.org/components/classic/download/
下載完成后解壓雙擊activemq.bat文件打開(不用安裝,直接使用),目錄和打開后效果如下:
運行后,瀏覽器訪問http://localhost:8161/地址進入一下界面。
點擊Manage ActiveMQ broker登錄到ActiveMQ管理頁面,默認賬號和密碼都是admin。管理頁面如下:
1、新建SpringBoot項目
新建Springboot項目,添加對應的依賴。項目完整的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、項目結構
3、相關配置信息
在application.properties類中添加ActiveMQ相關的配置信息
server.port=8080server.servlet.context-path=/mq#MQ服務器地址spring.activemq.broker-url=tcp://localhost:61616#用戶名spring.activemq.user=admin#密碼spring.activemq.password=admin#設置是Queue隊列還是Topic,false為Queue,true為Topic,默認false-Queuespring.jms.pub-sub-domain=false#spring.jms.pub-sub-domain=true#變量,定義隊列和topic的名稱myqueue: activemq-queuemytopic: activemq-topic
4、ActiveMQ配置類
ActiveMQ配置類ConfigBean,配置了Queue隊列和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; //隊列 @Bean public ActiveMQQueue queue(){ return new ActiveMQQueue(myQueue); } //topic @Bean public Topic topic(){ return new ActiveMQTopic(topicName); }}Queue隊列模式
隊列模式即點對點傳輸。點對點消息傳遞域的特點如下:
每個消息只能有一個消費者,類似于1對1的關系。好比個人快遞自己領自己的。
消息的生產者和消費者之間沒有時間上的相關性。無論消費者在生產者發送消息的時候是否處于運行狀態,消費者都可以提取消息。好比我們的發送短信,發送者發送后不見得接收者會即收即看。
消息被消費后隊列中不會再存儲,所以消費者不會消費到已經被消費掉的消息。
1、隊列生產者
QueueProducerController類為隊列生產者控制器,主要向消息隊列中發送消息。代碼如下:
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;/* * 隊列消息生產者 */@RestControllerpublic class QueueProducerController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; /* * 消息生產者 */ @RequestMapping('/sendmsg') public void sendmsg(String msg) { System.out.println('發送消息到隊列:' + msg); // 指定消息發送的目的地及內容 this.jmsMessagingTemplate.convertAndSend(this.queue, msg); }}
2、隊列消費者
QueueConsumerController類為隊列消費者控制器,具體代碼如下:
import org.springframework.beans.factory.annotation.Value;import org.springframework.jms.annotation.JmsListener;import org.springframework.web.bind.annotation.RestController;/* 1. 隊列queue消費者控制器 */@RestControllerpublic class QueueConsumerController { /* * 消費者接收消息 */ @JmsListener(destination='${myqueue}') public void readActiveQueue(String message) { System.out.println('接受到:' + message); }}
3、測試效果
運行項目在瀏覽器中訪問http://localhost:8080/mq/sendmsg?msg=123。向消息隊列中發送123??刂婆_輸出效果:
ActiveMQ控制臺顯示:
【注】隊列模式時,配置文件application.properties中spring.jms.pub-sub-domain屬性必須設置為false。
Topic模式topic模式基于發布/訂閱模式的傳輸。基于發布/訂閱模式的傳輸的特點如下:
生產者將消息發布到topic中,每個消息可以有多個消費者,屬于1:N的關系; 生產者和消費者之間有時間上的相關性。訂閱某一個主題的消費者只能消費自它訂閱之后發布的消息。 生產者生產時,topic不保存消息它是無狀態的不落地,假如無人訂閱就去生產,那就是一條廢消息。1、topic生產者
TopicProducerController類為topic生產者控制器,主要向消息隊列中發送消息。代碼如下:
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消息生產者*/@RestControllerpublic class TopicProducerController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Topic topic; /* * 消息生產者 */ @RequestMapping('/topicsendmsg') public void sendmsg(String msg) { System.out.println('發送消息到MQ:' + msg); // 指定消息發送的目的地及內容 this.jmsMessagingTemplate.convertAndSend(this.topic, msg); }}
2、topic消費者
TopicConsumerController類為topic消費者控制器,其中寫了兩個消費者方法,可以理解為有兩個用戶訂閱。具體代碼如下:
import org.springframework.jms.annotation.JmsListener;import org.springframework.web.bind.annotation.RestController;/* 1. topic消費者控制器 */@RestControllerpublic class TopicConsumerController { /* * 消費者接收消息 */ @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、測試效果
運行項目在瀏覽器中訪問http://localhost:8080/mq/topicsendmsg?msg=123。向消息隊列中發送123。控制臺輸出效果(有兩個消費者方法):
ActiveMQ控制臺顯示:
【注】Topic模式時,配置文件application.properties中spring.jms.pub-sub-domain屬性必須設置為true。
到此這篇關于詳解Springboot整合ActiveMQ(Queue和Topic兩種模式)的文章就介紹到這了,更多相關Springboot整合ActiveMQ內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: