SpringBoot学习笔记14-Messaging消息传递

本文深入探讨SpringBoot中消息传递的实现,涵盖JMS的ActiveMQ和Artemis支持,AMQP的RabbitMQ集成,以及Apache Kafka的使用。内容包括发送和接收消息、配置连接工厂以及使用嵌入式消息服务器进行测试。
摘要由CSDN通过智能技术生成

以spring官方文档为基础,官方地址:Spring Boot_Messaging

Spring框架为消息传递提供了广泛的支持,从使用JmsTemplate简化JMS API的使用,到完整的基础设施来异步接收消息。Spring AMQP为高级消息队列协议提供了类似的特性集。Spring Boot还为RabbitTemplateRabbitMQ提供了自动配置选项。Spring WebSocket原生地包含了对STOMP消息传递的支持,而Spring Boot则通过启动器和少量的自动配置来支持这一点。Spring Boot也支持Apache Kafka

1. JMS

javax.jms.ConnectionFactory接口提供了创建javax.jms.Connection的标准方法,用于与JMS broker进行交互。尽管Spring需要ConnectionFactory来处理JMS,但通常不需要直接使用它,而是可以依赖于更高级别的消息传递抽象。(详情请参阅Spring Framework参考文档的JMS相关章节)Spring Boot还自动配置必要的基础设施来发送和接收消息。

1.1 ActiveMQ 支持

ActiveMQ 是开源,多协议,基于Java的消息服务器。

当ActiveMQ在类路径上可用时,Spring Boot还可以配置ConnectionFactory。如果存在broker,则会自动启动并配置嵌入式broker(前提是配置中没有指定broker URL,且在配置中没有禁用嵌入式broker)。

如果使用spring-boot-starter-activemq,就会提供连接或嵌入ActiveMQ实例所需的依赖项,就像与JMS集成的Spring基础设施一样。

ActiveMQ配置由spring.activemq.*中的外部配置属性控制。

默认情况下,ActiveMQ被自动配置为使用VM传输,它会启动嵌入在同一个JVM实例中的broker。

可以通过配置spring.activemq.in-memory来禁用嵌入式broker,如下所示:

spring.activemq.in-memory=false

如果配置broker URL,嵌入式broker也会被禁用,如下所示:

spring.activemq.broker-url=tcp://192.168.1.210:9876
spring.activemq.user=admin
spring.activemq.password=secret

如果想完全控制嵌入式broker,请参阅ActiveMQ文档了解更多信息。

默认情况下,CachingConnectionFactory用合理的设置包装了本地的ConnectionFactory,可以通过spring.jms.*中的外部配置属性来控制这些设置。

spring.jms.cache.session-cache-size=5

如果更愿意使用原生池,可以通过向org.messaginghub:pooled-jms添加依赖项来实现,并相应地配置JmsPoolConnectionFactory,如下例所示:

spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=50

更多支持的选项请参见ActiveMQProperties。还可以注册实现ActiveMQConnectionFactoryCustomizer的任意数量的bean,以便进行更高级的定制。

1.2 ActiveMQ Artemis 支持

Artemis是下一代的ActiveMQ:下一代高性能,非阻塞架构,基于事件驱动的消息系统。

当Spring Boot检测到ActiveMQ Artemis在类路径上可用时,它可以自动配置ConnectionFactory。如果存在broker,则会自动启动并配置嵌入式broker(除非已显式设置mode属性)。受支持的模式分为嵌入式模式(明确表示需要嵌入式broker,如果broker在类路径上不可用就会发生错误)和原生模式(使用netty传输协议连接到broker)。在配置后一种模式时,Spring Boot会配置一个ConnectionFactory,它使用默认设置broker到运行在本地机器上的broker。

如果使用spring-boot-starter-artemis,将提供连接到现有ActiveMQ Artemis实例所需的依赖项,以及与JMS集成的Spring基础设施。增加org.apache.activemq:artemis-jms-server将允许使用嵌入式模式。

ActiveMQ Artemis配置由spring.artemis.*中的外部配置属性控制。例如,可以在application.properties中声明以下部分:

spring.artemis.mode=native
spring.artemis.broker-url=tcp://192.168.1.210:9876
spring.artemis.user=admin
spring.artemis.password=secret

在嵌入broker时,可以选择是否启用持久性,并列出应该提供的路径。可以将它们指定为逗号分隔的列表,以使用默认选项创建它们,或者可以定义类型为org.apache.activemq.artemis.jms.server.config.JMSQueueConfiguration或org.apache.activemq.artemis.jms.server.config.TopicConfiguration,分别用于高级队列和主题配置。

默认情况下,CachingConnectionFactory用合理的设置包装了本地的ConnectionFactory,可以通过spring.jms中的外部配置属性来控制这些设置。

spring.jms.cache.session-cache-size=5

如果更愿意使用原生池,可以通过向org.messaginghub:pooled-jms添加依赖项来实现,并相应地配置JmsPoolConnectionFactory,如下例所示:

spring.artemis.pool.enabled=true
spring.artemis.pool.max-connections=50

1.3 使用JNDI ConnectionFactory

应用服务器上运行应用程序时,Spring Boot会尝试使用JNDI来定位JMS ConnectionFactory。缺省情况下,检查java:/JmsXA和java:/XAConnectionFactory位置。可以使用spring.jms.jndi-name指定一个位置,示例如下:

spring.jms.jndi-name=java:/MyConnectionFactory

1.4 发送消息

Spring的JmsTemplate是自动配置的,可以自动将它直接连接到自己的bean中,如下面的例子所示:

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {
   

    private final JmsTemplate jmsTemplate;

    public MyBean(JmsTemplate jmsTemplate) {
   
        this.jmsTemplate = jmsTemplate;
    }

    public void someMethod() {
   
        this.jmsTemplate.convertAndSend("hello");
    }

}

JmsMessagingTemplate可以以类似的方式注入。如果定义了DestinationResolver或MessageConverter bean,它会自动关联到自动配置的JmsTemplate。

1.5 接收消息

使用JMS时,可以用@JmsListener对任何bean进行注释,以创建侦听器端点。如果没有定义JmsListenerContainerFactory,则会自动配置一个默认的JmsListenerContainerFactory。如果定义了DestinationResolver、MessageConverter或javax.jms.ExceptionListener bean,它们就会自动与默认工厂关联起来。

默认情况下,默认工厂是事务性的。如果在存在JtaTransactionManager的基础设施中运行,默认情况下,它与侦听器容器相关联。如果没有,则启用sessionTransacted标志。在后一种场景中,可以通过在侦听器方法(或其委托)上添加@Transactional,将本地数据存储事务与传入消息的处理关联起来。这确保在本地事务完成后确认传入的消

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot Messaging: Messaging APIs for Enterprise and Integration Solutions by Felipe Gutierrez English | 5 Jun. 2017 | ISBN: 1484212258 | 196 Pages | PDF | 6.36 MB Build messaging applications using the power of Spring Boot; use Spring application events over the Web; use WebSocket, SockJS, and STOMP messaging with Spring MVC; and use Spring JMS, Redis Pub/Sub and Spring AMQP for reliable messaging solutions. This book covers all the Spring Messaging APIs using Spring Boot. Written by a Pivotal engineer, Spring Boot Messaging is an authoritative guide to the many messaging APIs and how to use these for creating enterprise and integration solutions. You will learn and integrate these messaging APIs with more complex enterprise and cloud applications: for example, you will see how to use Spring Cloud Stream for creating message-driven and cloud native microservices. In addition, you’ll discover the new Spring Integration DSL and use it with Spring Cloud Stream to build integration solutions using every enterprise integration pattern. Finally, you’ll see Spring Reactor and Spring Cloud to take your application to the next level. After reading this book, you will come away with a case study application walk-through and will be able to use it as a template for building your own Spring messaging applications or messaging features within your enterprise or cloud application. What You'll Learn Use the main Spring messaging APIs with Spring Framework 5 Build messaging applications over the Web Use WebSocket, SockJS, and STOMP messaging Integrate Spring JMS and Spring AMQP into your applications Work with Spring Cloud Stream and microservices Who This Book Is For Enterprise Java developers who have at least some previous experience with the Spring Framework and/or the Spring platform.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值