ch12-Spring Integration
EIP(Enterprise Integration Patterns,企业集成模式)
Enterprise Integration Pattern -
组成简介:https://www.cnblogs.com/loveis715/p/5185332.html
Integration Pattern Language

spring integration
一个简单的集成流(例子)
本节课目标

一个简单的集成流
- 集成流配置
- XML配置
- Java配置
- 使用DSL的Java配置
- XML配置
1 | <dependency> |
消息

集成流( integration flow)

集成流的组件介绍
集成流的组件
- Channels(通道) —Pass messages from one element to another.
- Filters(过滤器) —Conditionally allow messages to pass through the flow based on some criteria.
- Transformers(转换器) —Change message values and/or convert message payloads from one type to another.
- Routers(路由器) —Direct messages to one of several channels, typically based on message headers.
- Splitters(切分器) —Split incoming messages into two or more messages, each sent to different channels.
- Aggregators(聚合器) —The opposite of splitters, combining multiple messages coming in from separate channels into a single message.
- Service activators(服务激活器) —Hand a message off to some Java method for processing, and then publish the return value on an output channel.
- Channel adapters(通道适配器) —Connect a channel to some external system or transport. Can either accept input or write to the external system.
- Gateways(网关) —Pass data into an integration flow via an interface.
消息通道(Message channels)
- PublishSubscribeChannel 1对多,1个发布多个订阅
- QueueChannel FIFO
- PriorityChannel 优先级队列,不按照FIFO出队
- RendezvousChannel
- DirectChannel(缺省)
- ExecutorChannel
- FluxMessageChannel
1 |
|

过滤器(Filters)
1 |
|

路由器(Routers )
1 |
|

切分器(Splitters)
1 | public class OrderSplitter { |

服务激活器(Service activators )
MessageHandler实现
- 处理完流就截止
1 |
|
GenericHandler实现
- 有返回值
1 |
|

与transfomer的区别
- transfomer:对数据进行转换加工
- 服务激活器:激活另一个服务,可能是存储或者外部的功能
网关(Gateways )
只需要写一个接口。
Gateways 是应用程序代码和消息系统之间的桥梁。它们抽象了消息发送和接收的细节,使得应用程序代码可以通过方法调用的方式与消息系统交互,而无需直接使用消息API。这样可以使应用程序代码保持简洁,同时也便于测试。
双向网关
- requets channel 输入
- repley channel 获得返回值(Spring会在这个管道上一直等,同步)
1 | import org.springframework.integration.annotation.MessagingGateway; |

通道适配器(Channel adapters)
Adapters 则是用于将消息从一种格式转换为另一种格式,或者从一种传输协议转换为另一种传输协议。Inbound把外部系统的消息格式转为spring integration消息,outbound把spring integration消息转为外部系统消息。
例如,JMS适配器可以将JMS消息转换为Spring Integration通用消息,HTTP适配器可以将HTTP请求和响应转换为Spring Integration消息。
1 |
|

- poller:多长时间触发一次,每秒触发一个消息的源头
通道适配器(DSL定义)
1 |
|
端点模块(Endpoint modules)

电子邮件集成流(本节课目标)
本节课目标

电子邮件端点模块
1 | <dependency> |
构建集成流
- TacoOrderEmailIntegrationConfig.java (DSL方式)

-
adaptor:
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
27package tacos.email;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.mail.dsl.Mail;
public class TacoOrderEmailIntegrationConfig {
public IntegrationFlow tacoOrderEmailFlow(
EmailProperties emailProps,
EmailToOrderTransformer emailToOrderTransformer,
OrderSubmitMessageHandler orderSubmitHandler) {
//创建集成流
return IntegrationFlows
.from(Mail.imapInboundAdapter(emailProps.getImapUrl()),
e -> e.poller(
Pollers.fixedDelay(emailProps.getPollRate())))
.transform(emailToOrderTransformer)
.handle(orderSubmitHandler)
.get();
}
}- transform(emailToOrderTransformer):邮件的内容转成order对象
transformer
1 | package tacos.email; |
