Spring Integration-Message Endpoints概念介绍
Message Endpoints概念介绍
Spring Integration是基于管道-过滤器架构的,Message Endpoint是过滤器中的一部分。在简介中提到过,endpoint主要是用来关联框架与应用业务代码的,就像MVC模型中controller只需要与url绑定即可获取到请求数据进行业务处理一样,endpoint只需要与message channel进行映射即可。
这里将与同学们共同学习Spring Integration提供的一些Endpoint类型。
Message Transformer
消息转换器用于转换消息内容及消息结构体,可以对消息内容进行修改。如:报文格式转化(JSON-XML互转),报文格式化,数据脱敏,报文头修改等。
Message Filter
消息过滤器用于处理消息是否需要传递,可以用来进行权限及认证控制、限流控制等。
Message Router
消息路由器用于决定消息下一步发送到哪个通道,一般根据报文内容或消息元数据来确定。
Splitter
分流器可以将一个消息分成多个消息,并且可以将这些消息分发给不同的输出通道。
Aggregator
聚合器与分流器相反,用于将多个消息聚合为一个消息,但是聚合器要比分流器复杂的多,因为为了聚合不同的消息需要维护待聚合消息的状态,以此决定一组消息是否完成了聚合或者是否已经超时。Spring Integration提供了CorrelationStrategy和ReleaseStrategy两种策略、超时配置以及一个丢弃通道,用于决定当聚合超时候是否发送部分消息。
Service Activator
Service Activator用于调用service对象的方法,在调用过程中activator提取请求消息的payload作为参数去调用service的方法,当service方法有返回值时将会被封装成Message对象并发送到输出通道。因此在配置Service Activator时需要配置消息输入通道及消息输出通道(如果有返回值),如果没有配置输出通道,也可以通过消息头中的"Return Address"来指定输出通道。
Channel Adapter
Channel Adapter用来提供不同协议消息的适配。
Endpoint Bean Names
Endpoint有多种配置方式:
Service Activator:
@Componentpublic class SomeComponent { @ServiceActivator(inputChannel = ...) public String someMethod(...) { ... }}@Componentpublic class SomeComponent { @EndpointId("someService") @ServiceActivator(inputChannel = ...) public String someMethod(...) { ... }}@Configurationpublic class SomeConfiguration { @Bean @ServiceActivator(inputChannel = ...) public MessageHandler someHandler() { ... }}@Configurationpublic class SomeConfiguration { @Bean("someService.handler") ① @EndpointId("someService") ② @ServiceActivator(inputChannel = ...) public MessageHandler someHandler() { ... }}
Channel Adapter:
@EndpointId("someAdapter")@InboundChannelAdapter(channel = "channel", poller = @Poller(fixedDelay = "5000" ))public String pojoSource() { ... }@Bean("someAdapter.source")@EndpointId("someAdapter")@InboundChannelAdapter(channel = "channel", poller = @Poller(fixedDelay = "5000" ))public MessageSource<?> source() { return () -> { ... }; }