首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Spring 核心技术解析【纯干货版】- XIV:Spring 消息模块 Spring-Jms 模块精讲

  • 25-02-20 15:20
  • 2430
  • 5703
blog.csdn.net

在现代分布式系统中,消息队列(Message Queue,MQ)扮演着至关重要的角色,它不仅能够解耦系统各个模块,还能提升系统的可扩展性和可靠性。JMS(Java Message Service)作为 Java EE 规范中的一部分,为 Java 应用提供了一套标准的消息通信 API。然而,JMS 原生 API 相对复杂,涉及较多底层操作,而 Spring-JMS 模块的出现极大地简化了 JMS 在 Spring 应用中的使用,使得消息的发送与接收更加直观且易于维护。

本篇文章将深入解析 Spring-JMS 模块,介绍其核心功能,并通过 ActiveMQ 作为消息代理,提供一个 基于 XML 配置的完整示例,帮助开发者快速掌握 Spring-JMS 的使用方式。


文章目录

      • 1、Spring-Jms 模块介绍
        • 1.1、Spring-Jms 模块概述
        • 1.2、Spring-Jms 模块依赖
        • 1.3、Spring-Jms 模块作用
      • 2、Spring-JMS 案例
        • 2.1、添加依赖
        • 2.2、 配置 Spring XML(spring-jms-config.xml)
        • 2.3、创建消息生产者(Producer)
        • 2.4、创建消息消费者(Consumer)
        • 2.5、启动 Spring 上下文并发送消息
        • 2.6、启动 ActiveMQ
        • 2.7、运行流程
      • X、后记


1、Spring-Jms 模块介绍

1.1、Spring-Jms 模块概述

Spring JMS 模块,是为了简化在 Spring 应用中使用消息传递服务而设计的模块。它提供了对 JMS(Java Message Service)规范的支持,使得开发者能够方便地发送和接收消息,与消息代理(如 ActiveMQ、RabbitMQ 等)进行交互。

Spring JMS 模块简化了消息生产者和消费者端点的配置,同时也集成了 Spring 的事务管理机制,使得消息驱动的架构变得更加易于实现和管理。

1.2、Spring-Jms 模块依赖

Spring-Tx 模块的依赖有四个,分别是 Spring-Beans 模块、Spring-Core 模块、Spring-Tx 模块以及 Spring-Messaging 模块。

其中 Spring Beans 模块是对 Spring Bean 进行定义,实现 IOC 基础功能的模块。Spring-Core 是 Spring 中的基础模块,它提供了框架运行所必需的核心功能。而 Spring Tx 模块,是 Spring 中处理事务管理的模块。

pring Messaging 模块主要关注于消息的抽象处理,支持多种消息传递协议,并且特别强化了对反应式编程模型的支持,使得开发者能更方便地创建高性能、可扩展的分布式系统。

1.3、Spring-Jms 模块作用

Spring-JMS 的核心作用:

  1. 简化 JMS API 操作:Spring-JMS 提供了一套模板化工具(如 JmsTemplate),封装了连接、会话管理等细节,使得发送和接收消息更加便捷。
  2. 支持消息驱动(Message-Driven):提供 @JmsListener 注解,可以将方法声明为 JMS 消息监听器,接收消息时自动触发。
  3. 支持事务管理:与 Spring 的事务管理集成,支持声明式事务,确保消息在消费时的 一致性和可靠性。
  4. 与 Spring IoC 容器集成:允许使用 Spring 配置管理 JMS 连接工厂(ConnectionFactory)、目的地(Queue/Topic)、消息监听容器等组件。
  5. 支持消息转换(Message Conversion):提供 MessageConverter 机制,支持将对象自动转换为 JMS 消息格式(如 JSON、XML)。
  6. 支持多种 JMS 提供者: 可与 ActiveMQ、Artemis、RabbitMQ(通过 JMS 兼容接口) 等消息中间件集成。

2、Spring-JMS 案例

本案例演示如何在 Spring目中使用 Spring-JMS 进行消息传递,基于 ActiveMQ 作为消息代理。

2.1、添加依赖

在 pom.xml 中添加 Spring-JMS 和 ActiveMQ 相关依赖:

<dependencies>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>5.3.39version>
    dependency>

    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jmsartifactId>
        <version>5.3.39version>
    dependency>

    
    <dependency>
        <groupId>org.apache.activemqgroupId>
        <artifactId>activemq-spring-boot-starterartifactId>
        <version>2.0.1version>
    dependency>

    
    <dependency>
        <groupId>javax.jmsgroupId>
        <artifactId>javax.jms-apiartifactId>
        <version>2.0.1version>
    dependency>
dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
2.2、 配置 Spring XML(spring-jms-config.xml)

使用 XML 配置文件 定义 连接工厂、JmsTemplate、监听器 等:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">

    
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <constructor-arg value="tcp://localhost:61616"/>  
    bean>

    
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    bean>

    
    <bean id="jmsListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destinationName" value="test.queue"/>
        <property name="messageListener" ref="messageReceiver"/>
    bean>

    
    <bean id="messageReceiver" class="com.example.jms.MessageReceiver"/>
beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
2.3、创建消息生产者(Producer)

使用 JmsTemplate 发送消息:

package com.example.jms;

import org.springframework.jms.core.JmsTemplate;

import javax.jms.Queue;

public class MessageSender {
    private JmsTemplate jmsTemplate;
    private Queue queue;

    // 构造方法注入
    public MessageSender(JmsTemplate jmsTemplate, Queue queue) {
        this.jmsTemplate = jmsTemplate;
        this.queue = queue;
    }

    public void sendMessage(String message) {
        System.out.println("发送消息:" + message);
        jmsTemplate.convertAndSend(queue, message);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
2.4、创建消息消费者(Consumer)

实现 MessageListener 接口 来监听消息:

package com.example.jms;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MessageReceiver implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                String text = ((TextMessage) message).getText();
                System.out.println("收到消息:" + text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
2.5、启动 Spring 上下文并发送消息
package com.example.jms;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Queue;

public class JmsApp {
    public static void main(String[] args) {
        // 加载 Spring XML 配置
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-jms-config.xml");

        // 获取 JmsTemplate 和队列
        JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
        Queue queue = (Queue) context.getBean("testQueue");

        // 发送消息
        MessageSender sender = new MessageSender(jmsTemplate, queue);
        sender.sendMessage("Hello, Spring-JMS!");

        System.out.println("消息已发送!");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
2.6、启动 ActiveMQ

确保 ActiveMQ 已启动:

activemq start
  • 1

默认 Web 控制台地址(可查看队列消息):

http://localhost:8161/admin
  • 1

默认用户名/密码:

admin / admin
  • 1
2.7、运行流程
  • 先启动 ActiveMQ 服务器。
  • 运行 JmsApp 发送消息。
  • MessageReceiver 监听到消息并打印到控制台。

X、后记

在本篇文章中,我们详细介绍了 Spring-JMS 模块的作用、依赖结构以及如何在非 Spring Boot 环境下集成 ActiveMQ,实现消息的发送与消费。通过使用 Spring-JMS,开发者可以更加高效地管理消息通信,并与 Spring 生态系统无缝集成,如结合事务管理、消息转换等特性,进一步提升系统的健壮性和可维护性。

在实际项目中,我们可以基于 Spring-JMS 结合其他 MQ 组件(如 RabbitMQ、Kafka)构建更加高效的异步消息处理系统。如果你对 Spring 消息驱动架构 感兴趣,可以进一步学习 Spring Cloud Stream,以便在微服务架构下实现更加灵活的事件驱动模式。希望本篇文章能够帮助你更好地理解 Spring-JMS,并在项目中灵活运用!

注:本文转载自blog.csdn.net的栗筝i的文章"https://blog.csdn.net/weixin_45187434/article/details/145697459"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

103
后端
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top