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

news/2025/3/4 10:54:39/

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/news/1576518.html

相关文章

从 Spring Boot 2 升级到 Spring Boot 3 的终极指南

一、升级前的核心准备 1. JDK 版本升级 Spring Boot 3 强制要求 Java 17 及以上版本。若当前项目使用 Java 8 或 11&#xff0c;需按以下步骤操作&#xff1a; 安装 JDK 17&#xff1a;从 Oracle 或 OpenJDK 官网下载&#xff0c;配置环境变量&#xff08;如 JAVA_HOME&…

go语言中字符串嵌套

在Go语言中&#xff0c;字符串嵌套通常是指在字符串中包含另一个字符串。可以通过以下几种方式实现&#xff1a; 1. 使用双引号和转义字符 如果需要在字符串中嵌套双引号&#xff0c;可以使用转义字符 \ 来表示内部的双引号。例如&#xff1a; s : "He said, \"He…

centos虚拟机安装

以下是一个详细的 VMware CentOS 虚拟机安装教程&#xff0c;结合了最新的信息和步骤&#xff1a; 一、准备工作 1. 下载 VMware 软件 访问 VMware 官方网站&#xff1a;VMware Workstation 官网。点击“现在安装”并下载适合您操作系统的 VMware Workstation。 2. 下载 Ce…

stm32中的定时器TIM控制器原理

一、定时器的基本组成 让计数器按照一个时钟频率从0加到一个数&#xff08;或 从一个数减到0&#xff09;&#xff0c;加到这个数&#xff08;重装载值&#xff09;后触发一个中断&#xff0c;触发这个中断后&#xff0c;我们就知道过了n秒&#xff08;一个固定的时间&#xff…

UE5切换关卡函数OpenLevel,输入模式结构体,UI界面

1.输入模式结构体 FInputModeGameOnly&#xff1a;玩家只能与游戏世界交互&#xff0c;UI 不可交互。FInputModeGameAndUI&#xff1a;玩家可以与游戏世界和 UI 同时交互。FInputModeUIOnly&#xff1a;玩家只能与 UI 交互&#xff0c;无法与游戏世界进行互动。 FInputModeGam…

SQL 基础 BETWEEN 的常见用法

在SQL中&#xff0c;BETWEEN是一个操作符&#xff0c;用于选取介于两个值之间的数据。 它包含这两个边界值。BETWEEN操作符常用于WHERE子句中&#xff0c;以便选取某个范围内的值。 以下是BETWEEN的一些常见用法&#xff1a; 选取介于两个值之间的值&#xff1a; 使用 BETWE…

AI本地化部署:全球AI芯片与服务器需求的新引擎

人工智能技术正在经历从集中化向分布式部署的深刻转变。各国政府和企业出于数据安全、隐私保护、网络延迟等考虑&#xff0c;纷纷推动AI系统在本地部署。这一趋势正在重塑全球AI基础设施市场格局&#xff0c;催生对AI芯片和服务器的新一轮需求爆发。 一、AI本地化部署的必然性…

深入解析 supervision 库:功能、用法与应用案例

1. 引言 在计算机视觉任务中&#xff0c;数据的后处理和可视化是至关重要的环节&#xff0c;尤其是在目标检测、分割、跟踪等任务中。supervision 是一个专门为这些任务提供高效数据处理和可视化支持的 Python 库。本文将深入介绍 supervision 的功能、使用方法&#xff0c;并…