Spring Boot 整合 JMS-ActiveMQ,并安裝 ActiveMQ

1. 安裝 ActiveMQ

1.1 下載 ActiveMQ

訪問 ActiveMQ 官方下載頁面,根據你的操作系統選擇合適的版本進行下載。這里以 Linux 系統,Java環境1.8版本為例,下載 apache-activemq-5.16.7-bin.tar.gz

1.2 解壓文件

將下載的壓縮包解壓到指定目錄,例如 /opt

tar -zxvf apache-activemq-5.16.7-bin.tar.gz -C /opt
1.3 啟動 ActiveMQ

進入解壓后的目錄,啟動 ActiveMQ:

cd /opt/apache-activemq-5.16.7
./bin/activemq start
1.4 驗證 ActiveMQ 是否啟動成功

打開瀏覽器,訪問 http://localhost:8161,使用默認用戶名 admin 和密碼 admin 登錄 ActiveMQ 的管理控制臺。如果能成功登錄,說明 ActiveMQ 已經啟動成功。
在這里插入圖片描述

1.4 進入ActiveMQ 管理員控制臺

ActiveMQ 啟動成功后,單擊 Manage ActiveMQ broker 超鏈接進入管理員控制臺。
在這里插入圖片描述

2. 創建 Spring Boot 項目并整合 JMS - ActiveMQ

2.1 添加依賴

pom.xml 中添加 Spring Boot 集成 ActiveMQ 的依賴:

<dependencies><!-- Spring Boot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot JMS --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency>
</dependencies>
2.2 配置 ActiveMQ

application.properties 中配置 ActiveMQ 的連接信息:

server.port=8080
# ActiveMQ 服務器地址,默認端口 61616
spring.activemq.broker-url=tcp://localhost:61616
# 配置信任所有的包,這個配置是為了支持發送對象消息
spring.activemq.packages.trust-all=true
# ActiveMQ 用戶名
spring.activemq.user=admin
# ActiveMQ 密碼
spring.activemq.password=admin
2.3 創建消息生產者

創建一個消息生產者類,用于發送消息到 ActiveMQ 的隊列:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;import javax.jms.Queue;@Service
public class JmsProducer {@Autowiredprivate JmsTemplate jmsTemplate;@Autowiredprivate Queue queue;public void sendMessage(String message) {jmsTemplate.convertAndSend(queue, message);System.out.println("Sent message: " + message);}
}
2.4 創建消息消費者

創建一個消息消費者類,用于接收 ActiveMQ 隊列中的消息:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class JmsConsumer {@JmsListener(destination = "test-queue")public void receiveMessage(String message) {System.out.println("Received message: " + message);}
}
2.5 配置 Queue Bean

在 ActiveMqConfig.java 中定義 隊列:

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.jms.Queue;@Configuration
public class ActiveMqConfig {@Beanpublic Queue queue() {return new ActiveMQQueue("test-queue");}
}
2.6 創建 API 測試發送消息
import com.weigang.producer.JmsProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/message")
public class MessageController {@Autowiredprivate JmsProducer producer;@GetMapping("/send")public String sendMessage(@RequestParam String msg) {producer.sendMessage(msg);return "Message sent: " + msg;}
}

然后訪問:http://localhost:8080/message/send?msg=HelloActiveMQ

控制臺應輸出:

Sent message: HelloActiveMQ
Received message: HelloActiveMQ

3. 使用 ActiveMQ Web 界面查看消息

訪問 http://localhost:8161/admin/queues.jsp,可以看到 test-queue 隊列以及發送的消息。
在這里插入圖片描述

4. 發送對象消息

在 JmsProducer 發送 對象:

import com.weigang.model.CustomMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;import javax.jms.Queue;@Service
public class JmsProducer {@Autowiredprivate JmsTemplate jmsTemplate;// 仍然使用 test-queue@Autowiredprivate Queue queue;public void sendMessage(CustomMessage customMessage) {jmsTemplate.convertAndSend(queue, customMessage);System.out.println("Sent message----> id:" + customMessage.getId() + ",content:" + customMessage.getContent());}
}

創建消息對象:

import java.io.Serializable;public class CustomMessage implements Serializable {private static final long serialVersionUID = 1L; // 推薦添加,避免序列化問題private String content;private int id;// 必須有默認構造方法(JMS 反序列化需要)public CustomMessage() {}// 構造方法public CustomMessage(String content, int id) {this.content = content;this.id = id;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "CustomMessage{" +"content='" + content + '\'' +", id=" + id +'}';}}

消費者處理對象消息:

import com.weigang.model.CustomMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;@Component
public class JmsConsumer {@JmsListener(destination = "test-queue")public void receiveMessage(String message) {System.out.println("Received message: " + message);}@JmsListener(destination = "test-queue")public void receiveMessage(CustomMessage message) {System.out.println("Received object message: " + message.toString());}
}

通過 API 發送對象:

import com.weigang.model.CustomMessage;
import com.weigang.producer.JmsProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/message")
public class MessageController {@Autowiredprivate JmsProducer producer;@GetMapping("/send")public String sendMessage(@RequestParam String msg) {producer.sendMessage(msg);return "Message sent: " + msg;}@GetMapping("/sendObj")public String sendMessage(@RequestParam Integer id,@RequestParam String content) {CustomMessage customMessage = new CustomMessage(content, id);producer.sendMessage(customMessage);return "Message sent: " + customMessage;}
}

然后訪問:http://localhost:8080/message/sendObj?id=1&content=HelloActiveMQ

控制臺應輸出:

Sent message----> id:1,content:HelloActiveMQ
Received object message: CustomMessage{content='HelloActiveMQ', id=1}

注意事項

  • 確保 ActiveMQ 服務器正常運行,并且 application.properties 中的連接信息正確。
  • 如果需要使用主題(Topic)進行消息傳遞,可以在配置中設置 spring.jms.pub-sub-domain=true,并相應地修改消息生產者和消費者的代碼。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/72561.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/72561.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/72561.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《幾何原本》命題I.13

《幾何原本》命題I.13 兩條直線相交&#xff0c;鄰角是兩個直角或者相加等于 18 0 ° 180^{\circ} 180°。 若兩角相等&#xff0c;則根據定義&#xff0c;兩角為直角。 兩角若不相等&#xff0c;如圖&#xff0c;則 ( ∠ 1 ∠ 2 ) ∠ 3 ∠ 1 ( ∠ 2 ∠ 3 ) 9 0 ° …

優先級隊列:通過堆的形式實現

描述: 大頂堆: 小頂堆: 索引位置查找: 代碼實現: package com.zy.queue_code.deque;/*** @Author: zy* @Date: 2025-03-05-15:51* @Description:*/ public interface Priority

《OpenCV》—— dlib庫

文章目錄 dlib庫是什么&#xff1f;OpenCV庫與dlib庫對比dlib庫安裝dlib——人臉應用實例——人臉檢測dlib——人臉應用實例——人臉關鍵點定位dlib——人臉應用實例——人臉輪廓繪制 dlib庫是什么&#xff1f; OpenCV庫與dlib庫對比 dlib庫安裝 dlib——人臉應用實例——人臉檢…

藍橋與力扣刷題(藍橋 旋轉)

題目&#xff1a;圖片旋轉是對圖片最簡單的處理方式之一&#xff0c;在本題中&#xff0c;你需要對圖片順時針旋轉 90 度。 我們用一個 nm的二維數組來表示一個圖片&#xff0c;例如下面給出一個 34 的 圖片的例子&#xff1a; 1 3 5 7 9 8 7 6 3 5 9 7 這個圖片順時針旋轉…

隨機播放音樂 偽隨機

import java.util.*;/*** https://cloud.tencent.com.cn/developer/news/1045747* 偽隨機播放音樂*/ public class MusicPlayer {private List<String> allSongs; // 所有歌曲列表private List<String> playedSongs; // 已經播放過的歌曲列表private Map<String…

MiniMind用極低的成本訓練屬于自己的大模型

本篇文章主要講解&#xff0c;如何通過極低的成本訓練自己的大模型的方法和教程&#xff0c;通過MiniMind快速實現普通家用電腦的模型訓練。 日期&#xff1a;2025年3月5日 作者&#xff1a;任聰聰 一、MiniMind 介紹 基本信息 在2小時&#xff0c;訓練出屬于自己的28M大模型。…

區塊鏈中的數字簽名:安全性與可信度的核心

數字簽名是區塊鏈技術的信任基石&#xff0c;它像區塊鏈世界的身份證和防偽標簽&#xff0c;確保每一筆交易的真實性、完整性和不可抵賴性。本文會用通俗的語言&#xff0c;帶你徹底搞懂區塊鏈中的數字簽名&#xff01; 文章目錄 1. 數字簽名是什么&#xff1f;從現實世界到區塊…

LLM自動金融量化-CFGPT

LLM自動金融量化-CFGPT 簡介 CFGPT是一個開源的語言模型,首先通過在收集和清理的中國金融文本數據(CFData-pt)上進行繼續預訓練,包括金融領域特定數據(公告、金融文章、金融考試、金融新聞、金融研究論文)和通用數據(維基百科),然后使用知識密集的指導調整數據(CFD…

解決Docker拉取鏡像超時錯誤,docker: Error response from daemon:

當使用docker pull或docker run時遇到net/http: request canceled while waiting for connection的報錯&#xff0c;說明Docker客戶端在訪問Docker Hub時出現網絡連接問題。可以不用掛加速器也能解決&#xff0c;linux不好用clash。以下是經過驗證的方法&#xff08;感謝軒轅鏡…

03.05 QT事件

實現一個繪圖工具&#xff0c;具備以下功能&#xff1a; 鼠標繪制線條。 實時調整線條顏色和粗細。 橡皮擦功能&#xff0c;覆蓋繪制內容。 撤銷功能&#xff0c;ctrl z 快捷鍵撤銷最后一筆 程序代碼&#xff1a; <1> Widget.h: #ifndef WIDGET_H #define WIDGET…

【文生圖】windows 部署stable-diffusion-webui

windows 部署stable-diffusion-webui AUTOMATIC1111 stable-diffusion-webui Detailed feature showcase with images: 帶圖片的詳細功能展示: Original txt2img and img2img modes 原始的 txt2img 和 img2img 模式 One click install and run script (but you still must i…

go語言因為前端跨域導致無法訪問到后端解決方案

前端服務8080訪問后端8081這端口顯示跨域了 ERROR Network Error AxiosError: Network Error at XMLHttpRequest.handleError (webpack-internal:///./node_modules/axios/lib/adapters/xhr.js:116:14) at Axios.request (webpack-internal:///./node_modules/axios/lib/core/A…

hive之lag函數

從博客上發現兩個面試題&#xff0c;其中有個用到了lag函數。整理學習 LAG 函數是 Hive 中常用的窗口函數&#xff0c;用于訪問同一分區內 前一行&#xff08;或前 N 行&#xff09;的數據。它在分析時間序列數據、計算相鄰記錄差異等場景中非常有用。 一、語法 LAG(column,…

【軟考-架構】1.3、磁盤-輸入輸出技術-總線

GitHub地址&#xff1a;https://github.com/tyronczt/system_architect ?資料&文章更新? 文章目錄 存儲系統&#x1f4af;考試真題輸入輸出技術&#x1f4af;考試真題第一題第二題 存儲系統 尋道時間是指磁頭移動到磁道所需的時間&#xff1b; 等待時間為等待讀寫的扇區…

盛鉑科技PDROUxxxx系列鎖相介質振蕩器(點頻源):高精度信號源

——超低相位噪聲、寬頻覆蓋、靈活集成&#xff0c;賦能下一代射頻系統 核心價值&#xff1a;以突破性技術解決行業痛點 在雷達、衛星通信、高速數據采集等高端射頻系統中&#xff0c;信號源的相位噪聲、頻率穩定度及集成靈活性直接決定系統性能上限。盛鉑科技PDROUxxxx系列鎖…

【安裝】SQL Server 2005 安裝及安裝包

安裝包 SQLEXPR.EXE&#xff1a;SQL Server 服務SQLServer2005_SSMSEE.msi&#xff1a;數據庫管理工具&#xff0c;可以創建數據庫&#xff0c;執行腳本等。SQLServer2005_SSMSEE_x64.msi&#xff1a;同上。這個是 64 位操作系統。 下載地址 https://www.microsoft.com/zh-c…

【文獻閱讀】The Efficiency Spectrum of Large Language Models: An Algorithmic Survey

這篇文章發表于2024年4月 摘要 大語言模型&#xff08;LLMs&#xff09;的快速發展推動了多個領域的變革&#xff0c;重塑了通用人工智能的格局。然而&#xff0c;這些模型不斷增長的計算和內存需求帶來了巨大挑戰&#xff0c;阻礙了學術研究和實際應用。為解決這些問題&…

如何在Github上面上傳本地文件夾

前言 直接在GitHub網址上面上傳文件夾是不行的&#xff0c;需要一層一層創建然后上傳&#xff0c;而且文件的大小也有限制&#xff0c;使用Git進行上傳更加方便和實用 1.下載和安裝Git Git - Downloads 傻瓜式安裝即可 2.獲取密鑰對 打開自己的Github&#xff0c;創建SSH密鑰&…

kafka-web管理工具cmak

一. 背景&#xff1a; 日常運維工作中&#xff0c;采用cli的方式進行kafka集群的管理&#xff0c;還是比較繁瑣的(指令復雜&#xff1f;)。為方便管理&#xff0c;可以選擇一些開源的webui工具。 推薦使用cmak。 二. 關于cmak&#xff1a; cmak是 Yahoo 貢獻的一款強大的 Apac…

python之爬蟲入門實例

鏈家二手房數據抓取與Excel存儲 目錄 開發環境準備爬蟲流程分析核心代碼實現關鍵命令詳解進階優化方案注意事項與擴展 一、開發環境準備 1.1 必要組件安裝 # 安裝核心庫 pip install requests beautifulsoup4 openpyxl pandas# 各庫作用說明&#xff1a; - requests&#x…