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

bash 复制代码
tar -zxvf apache-activemq-5.16.7-bin.tar.gz -C /opt
1.3 启动 ActiveMQ

进入解压后的目录,启动 ActiveMQ:

bash 复制代码
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 的依赖:

xml 复制代码
<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 的连接信息:

properties 复制代码
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 的队列:

java 复制代码
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 队列中的消息:

java 复制代码
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 中定义 队列:

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 测试发送消息
java 复制代码
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

控制台应输出:

c 复制代码
Sent message: HelloActiveMQ
Received message: HelloActiveMQ

3. 使用 ActiveMQ Web 界面查看消息

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

4. 发送对象消息

在 JmsProducer 发送 对象:

java 复制代码
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());
    }
}

创建消息对象:

java 复制代码
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 +
                '}';
    }

}

消费者处理对象消息:

java 复制代码
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 发送对象:

java 复制代码
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

控制台应输出:

go 复制代码
Sent message----> id:1,content:HelloActiveMQ
Received object message: CustomMessage{content='HelloActiveMQ', id=1}

注意事项

  • 确保 ActiveMQ 服务器正常运行,并且 application.properties 中的连接信息正确。
  • 如果需要使用主题(Topic)进行消息传递,可以在配置中设置 spring.jms.pub-sub-domain=true,并相应地修改消息生产者和消费者的代码。
相关推荐
程序员阿卢2 小时前
01-基于springboot框架调用ollama下的模型完成基本功能
spring boot·后端·ollama·通义千问模型qwen
烧饼Fighting3 小时前
Jenkins自动化编译部署Spring Boot项目
spring boot·自动化·jenkins
闪电悠米3 小时前
黑马点评-Redis 消息队列-01_why_redis_mq
java·数据库·spring boot·redis·缓存·junit·消息队列
我登哥MVP3 小时前
Spring Boot 从“会用”到“精通”:内容协商原理
java·spring boot·后端·spring·java-ee·maven·lua
Flittly4 小时前
【AgentScope Java新手村系列】(1)框架简介与环境搭建
java·spring boot·笔记·spring·ai
星辰徐哥12 小时前
Spring Boot 微服务架构设计与实现
spring boot·后端·微服务
星辰徐哥12 小时前
Spring Boot 数据导入导出与报表生成
spring boot·后端·ui
明夜之约12 小时前
Spring Boot 自动装配源码
java·spring boot·后端
Leaton Lee12 小时前
Spring Boot分层架构详解:从Controller到Service再到Mapper的完整流程
java·spring boot·后端·架构
Micro麦可乐12 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统