DSL 基础知识
这org.springframework.integration.dsl包包含IntegrationFlowBuilder前面提到的 API 和一些IntegrationComponentSpec实现,它们也是构建器,并提供流畅的 API 来配置具体端点。
这IntegrationFlowBuilder基础设施为基于消息的应用程序(例如通道、端点、轮询器和通道拦截器)提供常见的企业集成模式 (EIP)。
- 重要
-
这
IntegrationComponentSpec是一个FactoryBean实现,因此其getObject()方法不得从 Bean 定义中调用。 这IntegrationComponentSpec对于 Bean 定义,实现必须保持原样,框架将管理其生命周期。 目标的 Bean 方法参数注入IntegrationComponentSpec类型 (aFactoryBeanvalue) 必须用于IntegrationFlowbean 定义而不是 bean 方法引用。
端点在 DSL 中表示为动词,以提高可读性。 以下列表包括常见的 DSL 方法名称和关联的 EIP 端点:
-
转换→
Transformer -
过滤→
Filter -
手柄→
ServiceActivator -
拆分→
Splitter -
聚合→
Aggregator -
路线→
Router -
桥→
Bridge
从概念上讲,集成过程是通过将这些端点组合成一个或多个消息流来构造的。
请注意,EIP 没有正式定义术语“消息流”,但将其视为使用众所周知的消息传递模式的工作单元是有用的。
DSL 提供了一个IntegrationFlow组件来定义通道和它们之间的端点的组合,但现在IntegrationFlow仅在应用程序上下文中填充实际 Bean 的配置角色,在运行时不使用。
但是,用于IntegrationFlow可以自动接线为Lifecycle控制start()和stop()对于委托给与此关联的所有 Spring Integration 组件的整个流程IntegrationFlow.
以下示例使用IntegrationFlowfluent API 来定义IntegrationFlowbean 使用 EIP 方法IntegrationFlowBuilder:
@Bean
public IntegrationFlow integerFlow() {
return IntegrationFlow.from("input")
.<String, Integer>transform(Integer::parseInt)
.get();
}
这transform方法接受 lambda 作为端点参数来对消息有效负载进行作。
这个方法的真正参数是GenericTransformer<S, T>实例。
因此,任何提供的转换器 (ObjectToJsonTransformer,FileToStringTransformer,和其他)可以在此处使用。
在被窝里,IntegrationFlowBuilder识别MessageHandler和它的端点,使用MessageTransformingHandler和ConsumerEndpointFactoryBean分别。
考虑另一个例子:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlow.from("input")
.filter("World"::equals)
.transform("Hello "::concat)
.handle(System.out::println)
.get();
}
前面的示例组成了Filter → Transformer → Service Activator.
流程是“'单向'”。
也就是说,它不提供回复消息,而仅将有效负载打印到 STDOUT。
使用直接通道自动将终结点连接在一起。
|
lambda 和
Message<?>参数在 EIP 方法中使用 lambda 时,“input”参数通常是消息有效负载。
如果您希望访问整个消息,请使用采用
这将在运行时失败,并显示 相反,请使用:
|
|
Bean 定义覆盖
Java DSL 可以为流定义中内联定义的对象注册 Bean,也可以复用现有的注入 Bean。
如果为内联对象和现有 Bean 定义定义了相同的 Bean 名称,则 |