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

server/2025/3/4 2:15:58/

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.ppmy.cn/server/172225.html

相关文章

【心得】一文梳理高频面试题 HTTP 1.0/HTTP 1.1/HTTP 2.0/HTTP 3.0的区别并附加记忆方法

面试时很容易遇到的一个问题—— HTTP 1.0/HTTP 1.1/HTTP 2.0/HTTP 3.0的区别&#xff0c;其实这四个版本的发展实际上是一环扣一环的&#xff0c;是逐步完善的&#xff0c;本文希望帮助读者梳理清楚各个版本之间的区别&#xff0c;并且给出当前各个版本的应用情况&#xff0c;…

实例分割 | yolov11训练自己的数据集

前言 因工作要求使用的都是yolov5系列的模型&#xff0c;今天学习一下最先进的yolov11&#xff0c;记录一下环境配置及训练过程。 1.项目下载及环境安装 源码位置&#xff1a;yolov11 可以看到&#xff0c;这里要求python版本大于等于3.8&#xff0c;我这里安装python3.10.…

Vue3生命周期以及与Vue2的区别

文章目录 一、Vue3生命周期核心阶段与钩子函数二、Vue3生命周期示例&#xff1a;选项式 vs 组合式 API选项式 API 示例&#xff08;Vue2&#xff09;组合式 API 示例&#xff08;Vue3&#xff09; 三、Vue3与Vue2生命周期的核心差异1. 钩子函数更名2. 组合式 API 的影响3. 新增…

力扣打卡第三天

力扣打卡第三天 题目 分析题目 理解快乐数概念结束条件&#xff1a; 快乐数的结果为 1&#xff0c;返回True无限循环找不到 1&#xff0c;变相说法就是数值在到达一定数量之后会开始重复进入一个闭环&#xff0c;也就是只需要发现出现之前有的数值就重复了&#xff0c;也就可…

【Docker基础】理解 Registry 镜像仓库:分类、工作机制、命令与实操

文章目录 一、什么是 Docker RegistryDocker Registry 的主要功能&#xff1a;镜像的结构&#xff1a;Docker Registry 的组成&#xff1a;使用例子&#xff1a;Docker Hub&#xff1a; 二、镜像仓库分类三、镜像仓库工作机制四、常用的镜像仓库1. DockerHub2. 国内镜像源配置镜…

《Effective Objective-C》阅读笔记(下)

目录 内存管理 理解引用计数 引用计数工作原理 自动释放池 保留环 以ARC简化引用计数 使用ARC时必须遵循的方法命名规则 变量的内存管理语义 ARC如何清理实例变量 在dealloc方法中只释放引用并解除监听 编写“异常安全代码”时留意内存管理问题 以弱引用避免保留环 …

华为AP 4050DN-HD的FIT AP模式改为FAT AP,家用FAT基本配置

在某鱼买了两台华为AP 4050DN-HD , AP是二手的 , 在AC上上过线 , 所以就不能开机自选为FIP模式了 我没有AC无线控制器 , 就是买一个自己玩 , AP又是FIT瘦AP模式 ,所以我就想把AP的瘦AP模式改为FAT胖AP模式 1. 准备工作 1.1下载好对应软件&#xff0c;进入到 企业业务网站去下…

Qt空项目代码解释

一、 背景 创建的是一个 QWidget 项目。 二、main.cpp 1、图片 2、代码解释 &#xff08;1&#xff09;QApplication Qt 图形化界面中一定有 QApplication &#xff08;2&#xff09;Widget w; 是 QWidget 的子类。 &#xff08;3&#xff09;w.show(); 继承父类的显示…