SpringBoot整合activemq的案例代碼
ActiveMQ是什么
ActiveMQ是消息隊列技術(shù),為解決高并發(fā)問題而生
ActiveMQ生產(chǎn)者消費者模型(生產(chǎn)者和消費者可以跨平臺、跨系統(tǒng))
ActiveMQ支持如下兩種消息傳輸方式
- 點對點模式,生產(chǎn)者生產(chǎn)了一個消息,只能由一個消費者進行消費
- 發(fā)布/訂閱模式,生產(chǎn)者生產(chǎn)了一個消息,可以由多個消費者進行消費
1.安裝activemq(linux)
1.下載壓縮包,地址鏈接: https://activemq.apache.org/components/classic/download/.


2.利用xshell上傳壓縮包到linux上,并且解壓

3.進入到bin目錄啟動,通過./activemq start命令啟動activemq

4.在宿主主機范文虛擬機ip地址加上端口號8161,會出現(xiàn)登錄彈框叫我們輸入用戶名和密碼,用戶名和密碼都是admin。如下界面即代表active安裝成功

5.要注意的坑!
1)linux主機和宿主主機的防火墻都要關(guān)閉
2)linux主機和宿主主機一定要互ping得通
3)如果linux主機和宿主主機互ping得通,但是卻訪問不了activemq的端口,那就參考這篇文章,把conf目錄下的jetty.xml文件中的地址為linux主機的ip地址,就可以訪問了。文章鏈接: http://chabaoo.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 消息實體
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實現(xiàn))
這篇文章主要給大家介紹了關(guān)于短網(wǎng)址的原理與生成方法,利用的是Java實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Maven 版本管理與 flatten-maven-plugin 插件的使用解析
這篇文章主要介紹了Maven 版本管理與 flatten-maven-plugin 插件的使用解析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解
這篇文章主要介紹了FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解,需要的朋友可以參考下2017-08-08

