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.mzph.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…