返回

Spring Integration对STOMP Support支持

发布时间:2022-12-22 06:10:25 275
# webkit# spring# 服务器# 服务器# 信息

Spring Integration对STOMP Support支持_spring

Spring Integration 版本 4.2 引入了 STOMP(简单面向文本的消息传递协议)客户端支持。 它基于Spring Framework的消息传递模块stomp package中的架构,基础架构和API。 Spring Integration 使用许多 Spring STOMP 组件(例如 和 )。 有关更多信息,请参阅 Spring 框架参考手册中的 Spring Framework STOMP 支持一章。​​StompSession​​​​StompClientSupport​

您需要将此依赖项包含在项目中:

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-stomp</artifactId>
<version>6.0.0</version>
</dependency>

对于服务器端组件,您需要添加 和/或依赖项。​​org.springframework:spring-websocket​​​​io.projectreactor.netty:reactor-netty​

概述

若要配置 STOMP,应从 STOMP 客户端对象开始。 Spring 框架提供了以下实现:

  • ​WebSocketStompClient​​:基于Spring WebSocket API构建,支持标准JSR-356 WebSocket,Jetty 9和SockJS,用于使用SockJS客户端进行基于HTTP的WebSocket仿真。
  • ​ReactorNettyTcpStompClient​​:基于项目构建。ReactorNettyTcpClientreactor-netty

您可以提供任何其他实现。 请参阅这些类的 Javadoc。​​StompClientSupport​

该类被设计为一个工厂,用于为所提供的产品生成一个,所有剩余的工作都是通过对该类的回调和抽象来完成的。 使用 Spring 集成适配器抽象,我们需要提供一些托管共享对象来将我们的应用程序表示为 STOMP 客户端及其唯一会话。 为此,Spring Integration提供了管理任何提供的单之间的抽象。 这允许对特定的 STOMP 代理使用入站出站通道适配器(或两者)。 有关更多信息,请参阅(及其实现)JavaDocs。​​StompClientSupport​​​​StompSession​​​​StompSessionHandler​​​​StompSessionHandler​​​​StompSession​​​​StompSessionManager​​​​StompSession​​​​StompSessionHandler​​​​StompSessionManager​

STOMP 入站通道适配器

这是一个一站式组件,它将您的 Spring 集成应用程序订阅到提供的 STOMP 目标并从它们接收消息(通过使用连接上提供的 STOMP 帧转换而来)。 您可以在运行时更改目标(以及 STOMP 订阅),方法是在 上使用适当的注释。​​StompInboundChannelAdapter​​​​MessageProducer​​​​MessageConverter​​​​StompSession​​​​@ManagedOperation​​​​StompInboundChannelAdapter​

有关更多配置选项,请参阅 STOMP 命名空间支持和 Javadoc。​​StompInboundChannelAdapter​

STOMP 出站通道适配器

是 for 和 用于通过 (由共享提供)将传出实例发送到 STOMP(在运行时使用 SpEL 表达式预配置或确定)。​​StompMessageHandler​​​​MessageHandler​​​​​​​​Message<?>​​​​destination​​​​StompSession​​​​StompSessionManager​

有关更多配置选项,请参阅 STOMP 命名空间支持和 Javadoc。​​StompMessageHandler​

单块头映射

STOMP 协议提供标头作为其帧的一部分。 STOMP 框架的整个结构具有以下格式:

....
COMMAND
header1:value1
header2:value2

Body^@
....

Spring 框架提供了表示这些标头的功能。 有关更多详细信息,请参阅 ​​Javadoc​​。 STOMP 帧与实例相互转换,这些标头映射到实例和从实例映射。 Spring 集成为 STOMP 适配器提供了默认实现。 实现为 。 它分别为入站和出站适配器提供和操作。​​StompHeaders​​​​Message<?>​​​​MessageHeaders​​​​HeaderMapper​​​​StompHeaderMapper​​​​fromHeaders()​​​​toHeaders()​

与许多其他 Spring 集成模块一样,引入了该类以将标准 STOMP 标头映射到 ,并作为标头名称前缀。 此外,具有该前缀的所有实例在发送到目标时都会映射到 。​​IntegrationStompHeaders​​​​MessageHeaders​​​​stomp_​​​​MessageHeaders​​​​StompHeaders​

有关更多信息,请参阅 Javadoc 中这些类和 STOMP 命名空间支持中的属性说明。​​mapped-headers​

踩踏集成事件

许多 STOMP 操作都是异步的,包括错误处理。 例如,STOMP 有一个服务器帧,当客户端帧通过添加标头请求一个服务器帧时,它会返回该服务器帧。 为了提供对这些异步事件的访问,Spring 集成会发出实例,您可以通过实现 或使用 (请参阅接收 Spring 应用程序事件)来获取这些实例。​​RECEIPT​​​​RECEIPT​​​​StompIntegrationEvent​​​​ApplicationListener​​​​​

具体来说,当 a 由于无法连接到 STOMP 代理而接收时发出。 另一个例子是 . 它处理 STOMP 帧,这是服务器对此发送的不正确(未接受)消息的响应。​​StompExceptionEvent​​​​AbstractStompSessionManager​​​​stompSessionListenableFuture​​​​onFailure()​​​​StompMessageHandler​​​​ERROR​​​​StompMessageHandler​

作为异步应答中回调的一部分发出,用于发送到 . 可以是正数,也可以是负数,具体取决于帧是否在时间段内从服务器接收到,您可以在实例上配置该时间段。 它默认为(以毫秒为单位,即 15 秒)。​​StompMessageHandler​​​​StompReceiptEvent​​​​StompSession.Receiptable​​​​StompSession​​​​StompReceiptEvent​​​​RECEIPT​​​​receiptTimeLimit​​​​StompClientSupport​​​​15 * 1000​

仅当要发送的消息的 STOMP 标头不是 时,才会添加回调。 您可以分别在 通过其选项和 上启用自动标头生成。​​StompSession.Receiptable​​​​RECEIPT​​​​null​​​​RECEIPT​​​​StompSession​​​​autoReceipt​​​​StompSessionManager​

请参阅 STOMP 适配器 Java 配置,以获取有关如何配置 Spring 集成以接受这些实例的更多信息。​​ApplicationEvent​

STOMP 适配器 Java 配置

以下示例显示了 STOMP 适配器的综合 Java 配置:

@Configuration
@EnableIntegration
public class StompConfiguration {

@Bean
public ReactorNettyTcpStompClient stompClient() {
ReactorNettyTcpStompClient stompClient = new ReactorNettyTcpStompClient("127.0.0.1", 61613);
stompClient.setMessageConverter(new PassThruMessageConverter());
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
stompClient.setTaskScheduler(taskScheduler);
stompClient.setReceiptTimeLimit(5000);
return stompClient;
}

@Bean
public StompSessionManager stompSessionManager() {
ReactorNettyTcpStompSessionManager stompSessionManager = new ReactorNettyTcpStompSessionManager(stompClient());
stompSessionManager.setAutoReceipt(true);
return stompSessionManager;
}

@Bean
public PollableChannel stompInputChannel() {
return new QueueChannel();
}

@Bean
public StompInboundChannelAdapter stompInboundChannelAdapter() {
StompInboundChannelAdapter adapter =
new StompInboundChannelAdapter(stompSessionManager(), "/topic/myTopic");
adapter.setOutputChannel(stompInputChannel());
return adapter;
}

@Bean
@ServiceActivator(inputChannel = "stompOutputChannel")
public MessageHandler stompMessageHandler() {
StompMessageHandler handler = new StompMessageHandler(stompSessionManager());
handler.setDestination("/topic/myTopic");
return handler;
}

@Bean
public PollableChannel stompEvents() {
return new QueueChannel();
}

@Bean
public ApplicationListener stompEventListener() {
ApplicationEventListeningMessageProducer producer = new ApplicationEventListeningMessageProducer();
producer.setEventTypes(StompIntegrationEvent.class);
producer.setOutputChannel(stompEvents());
return producer;
}

}

STOMP 命名空间支持

Spring 集成 STOMP 命名空间实现入站和出站通道适配器组件。 若要将其包含在配置中,请在应用程序上下文配置文件中提供以下命名空间声明:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-stomp="http://www.springframework.org/schema/integration/stomp"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/stomp
https://www.springframework.org/schema/integration/stomp/spring-integration-stomp.xsd">
...

了解元素​​​

以下清单显示了 STOMP 出站通道适配器的可用属性:

<int-stomp:outbound-channel-adapter
id=""
channel=""
stomp-session-manager=""
header-mapper=""
mapped-headers=""
destination=""
destination-expression=""
auto-startup=""
phase=""/>

组件 Bean 名称。 使用 加 的 Bean 别名注册。 如果未设置该属性,则会在应用程序上下文中创建并注册 ,并将此属性的值作为 Bean 名称。 在这种情况下,端点注册时会使用 Bean 名称加 .​​MessageHandler​​​​id​​​​.handler​​​​channel​​​​DirectChannel​​​​id​​​​id​​​​.adapter​

标识连接到此适配器的通道(如果存在)。 看。 自选。​​id​​​​id​

对 Bean 的引用,它封装了低级连接和处理操作。 必填。​​StompSessionManager​​​​StompSession​

引用实现 的 bean,它将 Spring 集成映射到 踩踏帧标头。 它与 互斥。 默认为 .​​HeaderMapper​​​​MessageHeaders​​​​mapped-headers​​​​StompHeaderMapper​

要映射到 STOMP 帧标头的 STOMP 标头的名称的逗号分隔列表。 仅当未设置引用时,才能提供它。 此列表中的值也可以是与标头名称(如 或 )匹配的简单模式。 特殊标记 () 表示所有标准 STOMP 标头(内容长度、收据、检测信号等)。 默认情况下,它们包含在内。 如果要添加自己的标头,并且还希望映射标准标头,则必须包含此令牌或使用 提供自己的实现。​​header-mapper​​​​myheader*​​​​*myheader​​​​STOMP_OUTBOUND_HEADERS​​​​HeaderMapper​​​​header-mapper​

STOMP 消息发送到的目标的名称。 它与 .​​destination-expression​

一个 SpEL 表达式,要在运行时针对作为根对象的每个 Spring 集成进行评估。 它与 .​​Message​​​​destination​

指示此终结点是否应自动启动的布尔值。 默认为 .​​true​

此终结点应在其中启动和停止的生命周期阶段。 值越低,此终结点启动得越早,停止得越晚。 缺省值为 。 值可以为负数。 请参阅智能生命周期​。​​Integer.MIN_VALUE​

了解元素​​​

以下清单显示了 STOMP 入站通道适配器的可用属性:

<int-stomp:inbound-channel-adapter
id=""
channel=""
error-channel=""
stomp-session-manager=""
header-mapper=""
mapped-headers=""
destinations=""
send-timeout=""
payload-type=""
auto-startup=""
phase=""/>

 

组件 Bean 名称。 如果未设置该属性,则会在应用程序上下文中创建并注册 ,并将此属性的值作为 Bean 名称。 在这种情况下,端点使用 Bean 名称加上 .​​channel​​​​DirectChannel​​​​id​​​​id​​​​.adapter​

 

标识连接到此适配器的通道。

 

应将实例发送到的 Bean 引用。​​MessageChannel​​​​ErrorMessage​

 

请参阅​​<int-stomp:outbound-channel-adapter>​​上的相同选项。

 

要从 STOMP 帧标头映射的 STOMP 标头的名称的逗号分隔列表。 仅当未设置引用时,才能提供此信息。 此列表中的值也可以是与标头名称匹配的简单模式(例如,或 )。 特殊标记 () 表示所有标准 STOMP 标头(内容长度、收据、检测信号等)。 默认情况下,它们包含在内。 如果要添加自己的标头并希望也映射标准标头,则还必须包含此令牌或使用 提供自己的实现。​​header-mapper​​​​myheader*​​​​*myheader​​​​STOMP_INBOUND_HEADERS​​​​HeaderMapper​​​​header-mapper​

 

请参阅​​<int-stomp:outbound-channel-adapter>​​上的相同选项。

 

要订阅的 STOMP 目标名称的逗号分隔列表。 目标列表(以及订阅)可以在运行时通过 和 注释进行修改。​​addDestination()​​​​removeDestination()​​​​@ManagedOperation​

 

如果通道可以阻止,则向通道发送消息时等待的最长时间(以毫秒为单位)。 例如,如果达到其最大容量,则 can 会阻塞,直到空间可用。​​QueueChannel​

 

要从传入的 STOMP 帧转换的目标的 Java 类型的完全限定名称。 默认为 .​​payload​​​​String.class​

 

请参阅​​<int-stomp:outbound-channel-adapter>​​上的相同选项。

 

请参阅​​<int-stomp:outbound-channel-adapter>​​上的相同选项。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线