SpringBoot整合activemq的案例代碼
ActiveMQ是什么
ActiveMQ是消息隊(duì)列技術(shù),為解決高并發(fā)問題而生
ActiveMQ生產(chǎn)者消費(fèi)者模型(生產(chǎn)者和消費(fèi)者可以跨平臺、跨系統(tǒng))
ActiveMQ支持如下兩種消息傳輸方式
- 點(diǎn)對點(diǎn)模式,生產(chǎn)者生產(chǎn)了一個(gè)消息,只能由一個(gè)消費(fèi)者進(jìn)行消費(fèi)
- 發(fā)布/訂閱模式,生產(chǎn)者生產(chǎn)了一個(gè)消息,可以由多個(gè)消費(fèi)者進(jìn)行消費(fèi)
1.安裝activemq(linux)
1.下載壓縮包,地址鏈接: https://activemq.apache.org/components/classic/download/.
2.利用xshell上傳壓縮包到linux上,并且解壓
3.進(jìn)入到bin目錄啟動,通過./activemq start命令啟動activemq
4.在宿主主機(jī)范文虛擬機(jī)ip地址加上端口號8161,會出現(xiàn)登錄彈框叫我們輸入用戶名和密碼,用戶名和密碼都是admin。如下界面即代表active安裝成功
5.要注意的坑!
1)linux主機(jī)和宿主主機(jī)的防火墻都要關(guān)閉
2)linux主機(jī)和宿主主機(jī)一定要互ping得通
3)如果linux主機(jī)和宿主主機(jī)互ping得通,但是卻訪問不了activemq的端口,那就參考這篇文章,把conf目錄下的jetty.xml文件中的地址為linux主機(jī)的ip地址,就可以訪問了。文章鏈接: http://www.dbjr.com.cn/LINUXjishu/810589.html.
4)activemq還依賴java環(huán)境,所以linux上也要安裝java環(huán)境
2.SpringBoot整合activemq案例
2.1 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.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yl</groupId> <artifactId>jms</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jms</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 application.properties
# activemq的配置 # 地址 spring.activemq.broker-url=tcp://192.168.244.135:61616 # 信任所有的包 spring.activemq.packages.trust-all=true # 用戶名 spring.activemq.user=admin # 密碼 spring.activemq.password=admin
2.3 消息實(shí)體
package com.yl.jms.model; import java.io.Serializable; import java.util.Date; public class Message implements Serializable { private String content; private Date date; public String getContent() { return content; } public void setContent(String content) { this.content = content; public Date getDate() { return date; public void setDate(Date date) { this.date = date; @Override public String toString() { return "Message{" + "content='" + content + '\'' + ", date=" + date + '}'; }
2.4 主程序
package com.yl.jms; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import javax.jms.Queue; @SpringBootApplication public class JmsApplication { public static void main(String[] args) { SpringApplication.run(JmsApplication.class, args); } @Bean Queue queue() { return new ActiveMQQueue("yl-queue"); }
2.5 定義消息的發(fā)送和接收方法
package com.yl.jms.config; import com.yl.jms.model.Message; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; import javax.jms.Queue; @Component public class JmsComponent { //消息發(fā)送模板 @Autowired JmsMessagingTemplate jmsMessagingTemplate; Queue queue; //發(fā)送消息 public void send(Message message) { jmsMessagingTemplate.convertAndSend(queue,message); } //接收消息 @JmsListener(destination = "yl-queue") public void receive(Message message) { System.out.println("message:" + message); }
2.6 測試
到此這篇關(guān)于SpringBoot整合activemq的文章就介紹到這了,更多相關(guān)SpringBoot整合activemq內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
短網(wǎng)址的原理與生成方法(Java實(shí)現(xiàn))
這篇文章主要給大家介紹了關(guān)于短網(wǎng)址的原理與生成方法,利用的是Java實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10IOC?容器啟動和Bean實(shí)例化兩個(gè)階段詳解
這篇文章主要為大家介紹了IOC?容器啟動和Bean實(shí)例化兩個(gè)階段詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Maven 版本管理與 flatten-maven-plugin 插件的使用解析
這篇文章主要介紹了Maven 版本管理與 flatten-maven-plugin 插件的使用解析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07JDBC獲取數(shù)據(jù)庫連接的5種方式實(shí)例
JDBC是一種用于執(zhí)行SQL語句的JavaAPI,為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成,提供了諸如查詢和更新數(shù)據(jù)庫中數(shù)據(jù)的方法,這篇文章主要給大家介紹了關(guān)于JDBC獲取數(shù)據(jù)庫連接的5種方式,需要的朋友可以參考下2022-06-06FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
這篇文章主要介紹了FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解,需要的朋友可以參考下2017-08-08