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

[复制链接]
发表于 2025-10-21 04:05:44 | 显示全部楼层 |阅读模式
1. 安装 ActiveMQ

1.1 下载 ActiveMQ

访问 ActiveMQ 官方下载页面,根据你的操纵体系选择符合的版本举行下载。这里以 Linux 体系,Java情况1.8版本为例,下载 apache-activemq-5.16.7-bin.tar.gz。
1.2 解压文件

将下载的压缩包解压到指定目次,比方 /opt:
  1. tar -zxvf apache-activemq-5.16.7-bin.tar.gz -C /opt
复制代码
1.3 启动 ActiveMQ

进入解压后的目次,启动 ActiveMQ:
  1. cd /opt/apache-activemq-5.16.7
  2. ./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 的依靠:
  1. <dependencies>
  2.     <!-- Spring Boot Web -->
  3.     <dependency>
  4.         <groupId>org.springframework.boot</groupId>
  5.         <artifactId>spring-boot-starter-web</artifactId>
  6.     </dependency>
  7.     <!-- Spring Boot JMS -->
  8.     <dependency>
  9.         <groupId>org.springframework.boot</groupId>
  10.         <artifactId>spring-boot-starter-activemq</artifactId>
  11.     </dependency>
  12. </dependencies>
复制代码
2.2 设置 ActiveMQ

在 application.properties 中设置 ActiveMQ 的毗连信息:
  1. server.port=8080
  2. # ActiveMQ 服务器地址,默认端口 61616
  3. spring.activemq.broker-url=tcp://localhost:61616
  4. # 配置信任所有的包,这个配置是为了支持发送对象消息
  5. spring.activemq.packages.trust-all=true
  6. # ActiveMQ 用户名
  7. spring.activemq.user=admin
  8. # ActiveMQ 密码
  9. spring.activemq.password=admin
复制代码
2.3 创建消息生产者

创建一个消息生产者类,用于发送消息到 ActiveMQ 的队列:
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.jms.core.JmsTemplate;
  3. import org.springframework.stereotype.Service;
  4. import javax.jms.Queue;
  5. @Service
  6. public class JmsProducer {
  7.     @Autowired
  8.     private JmsTemplate jmsTemplate;
  9.     @Autowired
  10.     private Queue queue;
  11.     public void sendMessage(String message) {
  12.         jmsTemplate.convertAndSend(queue, message);
  13.         System.out.println("Sent message: " + message);
  14.     }
  15. }
复制代码
2.4 创建消息消耗者

创建一个消息消耗者类,用于罗致 ActiveMQ 队列中的消息:
  1. import org.springframework.jms.annotation.JmsListener;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class JmsConsumer {
  5.     @JmsListener(destination = "test-queue")
  6.     public void receiveMessage(String message) {
  7.         System.out.println("Received message: " + message);
  8.     }
  9. }
复制代码
2.5 设置 Queue Bean

在 ActiveMqConfig.java 中界说 队列:
  1. import org.apache.activemq.command.ActiveMQQueue;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import javax.jms.Queue;
  5. @Configuration
  6. public class ActiveMqConfig {
  7.     @Bean
  8.     public Queue queue() {
  9.         return new ActiveMQQueue("test-queue");
  10.     }
  11. }
复制代码
2.6 创建 API 测试发送消息

  1. import com.weigang.producer.JmsProducer;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.*;
  4. @RestController
  5. @RequestMapping("/message")
  6. public class MessageController {
  7.     @Autowired
  8.     private JmsProducer producer;
  9.     @GetMapping("/send")
  10.     public String sendMessage(@RequestParam String msg) {
  11.         producer.sendMessage(msg);
  12.         return "Message sent: " + msg;
  13.     }
  14. }
复制代码
然后访问:http://localhost:8080/message/send?msg=HelloActiveMQ
控制台应输出:
  1. Sent message: HelloActiveMQ
  2. Received message: HelloActiveMQ
复制代码
3. 使用 ActiveMQ Web 界面检察消息

访问 http://localhost:8161/admin/queues.jsp,可以看到 test-queue 队列以及发送的消息。

4. 发送对象消息

在 JmsProducer 发送 对象:
  1. import com.weigang.model.CustomMessage;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.jms.core.JmsTemplate;
  4. import org.springframework.stereotype.Service;
  5. import javax.jms.Queue;
  6. @Service
  7. public class JmsProducer {
  8.     @Autowired
  9.     private JmsTemplate jmsTemplate;// 仍然使用 test-queue
  10.     @Autowired
  11.     private Queue queue;
  12.     public void sendMessage(CustomMessage customMessage) {
  13.         jmsTemplate.convertAndSend(queue, customMessage);
  14.         System.out.println("Sent message----> id:" + customMessage.getId() + ",content:" + customMessage.getContent());
  15.     }
  16. }
复制代码
创建消息对象:
  1. import java.io.Serializable;
  2. public class CustomMessage implements Serializable {
  3.     private static final long serialVersionUID = 1L; // 推荐添加,避免序列化问题
  4.     private String content;
  5.     private int id;
  6.     // 必须有默认构造方法(JMS 反序列化需要)
  7.     public CustomMessage() {}
  8.     // 构造方法
  9.     public CustomMessage(String content, int id) {
  10.         this.content = content;
  11.         this.id = id;
  12.     }
  13.     public String getContent() {
  14.         return content;
  15.     }
  16.     public void setContent(String content) {
  17.         this.content = content;
  18.     }
  19.     public int getId() {
  20.         return id;
  21.     }
  22.     public void setId(int id) {
  23.         this.id = id;
  24.     }
  25.     @Override
  26.     public String toString() {
  27.         return "CustomMessage{" +
  28.                 "content='" + content + '\'' +
  29.                 ", id=" + id +
  30.                 '}';
  31.     }
  32. }
复制代码
消耗者处置惩罚对象消息:
  1. import com.weigang.model.CustomMessage;
  2. import org.springframework.jms.annotation.JmsListener;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class JmsConsumer {
  6.     @JmsListener(destination = "test-queue")
  7.     public void receiveMessage(String message) {
  8.         System.out.println("Received message: " + message);
  9.     }
  10.     @JmsListener(destination = "test-queue")
  11.     public void receiveMessage(CustomMessage message) {
  12.         System.out.println("Received object message: " + message.toString());
  13.     }
  14. }
复制代码
通过 API 发送对象:
  1. import com.weigang.model.CustomMessage;
  2. import com.weigang.producer.JmsProducer;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.*;
  5. @RestController
  6. @RequestMapping("/message")
  7. public class MessageController {
  8.     @Autowired
  9.     private JmsProducer producer;
  10.     @GetMapping("/send")
  11.     public String sendMessage(@RequestParam String msg) {
  12.         producer.sendMessage(msg);
  13.         return "Message sent: " + msg;
  14.     }
  15.     @GetMapping("/sendObj")
  16.     public String sendMessage(@RequestParam Integer id,@RequestParam String content) {
  17.         CustomMessage customMessage = new CustomMessage(content, id);
  18.         producer.sendMessage(customMessage);
  19.         return "Message sent: " + customMessage;
  20.     }
  21. }
复制代码
然后访问:http://localhost:8080/message/sendObj?id=1&content=HelloActiveMQ
控制台应输出:
  1. Sent message----> id:1,content:HelloActiveMQ
  2. Received object message: CustomMessage{content='HelloActiveMQ', id=1}
复制代码
注意事项



  • 确保 ActiveMQ 服务器正常运行,而且 application.properties 中的毗连信息精确。
  • 假如必要使用主题(Topic)举行消息转达,可以在设置中设置 spring.jms.pub-sub-domain=true,并相应地修改消息生产者和消耗者的代码。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表