当前位置: 首页 > news >正文

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 {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private 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 {

    @Bean
    public 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 {

    @Autowired
    private 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 {

    @Autowired
    private JmsTemplate jmsTemplate;// 仍然使用 test-queue

    @Autowired
    private 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;
    }

    @Override
    public 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 {

    @Autowired
    private 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,并相应地修改消息生产者和消费者的代码。

相关文章:

  • 关于opencv中solvepnp中UPNP与DLS与EPNP的参数
  • 神经网络:AI的网络神经
  • pytest中pytest.ini文件的使用
  • 【USRP】NVIDIA Sionna:用于 6G 物理层研究的开源库
  • Linux的用户与权限--第二天
  • 2.反向传播机制简述——大模型开发深度学习理论基础
  • 【2025小白版】计算复试/保研机试模板(个人总结非GPT生成)附代码
  • 【科研绘图系列】R语言绘制数值的美国地图(USA map)
  • JavaScript实现倒计时函数
  • Spring Boot 学习笔记
  • 特征选择之递归特征消除(REF)
  • 【零基础到精通Java合集】第十五集:Map集合框架与泛型
  • MySQL夺命连环13问
  • AT89C51手册解读:特性、引脚、操作模式及编程详解
  • 【大模型科普】AIGC技术发展与应用实践(一文读懂AIGC)
  • 深入解析Java线程模型:从BIO到NIO的性能跃迁之路
  • 文件上传靶场(1--9关)
  • Kotlin使用心得:提升开发效率的实战技巧(一)
  • 前端面试场景题葵花宝典之四
  • 如何直接导出某个conda环境中的包, 然后直接用 pip install -r requirements.txt 在新环境中安装
  • 西安公司网站建设/3分钟搞定网站seo优化外链建设
  • wordpress菜单页面定位/aso优化技巧大aso技巧
  • wordpress 大型站/搜索引擎优化的定义是什么
  • 演示 又一个wordpress站点/百度seo营销
  • 潍坊做网站建设/今日国际重大新闻
  • 温州网站建设制作/邯郸seo优化