Working With Message Flows
IntegrationFlowBuilder
提供了一个顶级 API 来生成连接到消息流的集成组件。如果你的集成可以通过一个流完成(这通常是情况),这会很方便。或者,IntegrationFlow
实例可以经由 MessageChannel
实例连接。
IntegrationFlowBuilder
provides a top-level API to produce integration components wired to message flows.
When your integration may be accomplished with a single flow (which is often the case), this is convenient.
Alternately IntegrationFlow
instances can be joined via MessageChannel
instances.
默认情况下,MessageFlow
在 Spring Integration 术语中作为“链
”表现。也就是说,端点由 DirectChannel
实例自动隐式连接。消息流实际上不是作为链构建的,这提供了更大的灵活性。例如,如果你知道流中任何组件的 inputChannel
名称(也就是说,如果你显式定义了它),你可以向其发送消息。你也可以在流中引用外部定义的通道,以允许通道适配器(启用远程传输协议、文件 I/O 等等)取代直接通道。因此,DSL 不支持 Spring Integration chain
元素,因为它在这种情况下没有增加太多价值。
By default, MessageFlow
behaves as a “chain” in Spring Integration parlance.
That is, the endpoints are automatically and implicitly wired by DirectChannel
instances.
The message flow is not actually constructed as a chain, which offers much more flexibility.
For example, you may send a message to any component within the flow, if you know its inputChannel
name (that is, if you explicitly define it).
You may also reference externally defined channels within a flow to allow the use of channel adapters (to enable remote transport protocols, file I/O, and so on), instead of direct channels.
As such, the DSL does not support the Spring Integration chain
element, because it does not add much value in this case.
由于 Spring Integration Java DSL 生成与任何其他配置选项相同的 bean 定义模型,并且基于现有的 Spring Framework @Configuration
基础架构,它可以与 XML 定义一起使用,并与 Spring Integration 消息传递注释配置相连接。
Since the Spring Integration Java DSL produces the same bean definition model as any other configuration options and is based on the existing Spring Framework @Configuration
infrastructure, it can be used together with XML definitions and wired with Spring Integration messaging annotation configuration.
你还可以使用 lambda 来定义直接 IntegrationFlow
实例。以下示例展示了如何操作:
You can also define direct IntegrationFlow
instances by using a lambda.
The following example shows how to do so:
@Bean
public IntegrationFlow lambdaFlow() {
return f -> f.filter("World"::equals)
.transform("Hello "::concat)
.handle(System.out::println);
}
此定义的结果是连接到内隐直接通道的同一组集成组件。唯一的限制是此流以一个命名直接通道 – lambdaFlow.input
- 开始。还有,Lambda 流无法从 MessageSource
或 MessageProducer
启动。
The result of this definition is the same set of integration components that are wired with an implicit direct channel.
The only limitation here is that this flow is started with a named direct channel - lambdaFlow.input
.
Also, a Lambda flow cannot start from MessageSource
or MessageProducer
.
从 5.1 版开始,这种类型的 IntegrationFlow
被封装到了代理程序中以公开生命周期控制,并提供对内部关联的 StandardIntegrationFlow
的 inputChannel
的访问。
Starting with version 5.1, this kind of IntegrationFlow
is wrapped to the proxy to expose lifecycle control and provide access to the inputChannel
of the internally associated StandardIntegrationFlow
.
从 5.0.6 版开始,IntegrationFlow`中组件的生成 bean 名称包括后面的流 bean(作为前缀:
.)。例如,在前面的示例中,
.transform("Hello "::concat)的 `ConsumerEndpointFactoryBean`名称的 bean 是 `lambdaFlow.o.s.i.config.ConsumerEndpointFactoryBean#0
。(o.s.i`缩写自 `org.springframework.integration
,以适应页面。)该端点的 `Transformer`实现 bean 具有 `lambdaFlow.transformer#0`bean 名称(从版本 5.1 开始),其中使用其组件类型,而不是 `MethodInvokingTransformer`类的完全限定名称。当必须在流中生成 bean 名称时,所有 `NamedComponent`都应用相同的模式。这些生成的 bean 名称附加流 ID,以用于解析日志或在某些分析工具中将组件分组,以及避免我们在运行时同时注册集成流程时的竞态条件。有关更多信息,请参阅 Dynamic and Runtime Integration Flows。
Starting with version 5.0.6, the generated bean names for the components in an IntegrationFlow
include the flow bean followed by a dot (.
) as a prefix.
For example, the ConsumerEndpointFactoryBean
for the .transform("Hello "::concat)
in the preceding sample results in a bean name of lambdaFlow.o.s.i.config.ConsumerEndpointFactoryBean#0
.
(The o.s.i
is a shortened from org.springframework.integration
to fit on the page.)
The Transformer
implementation bean for that endpoint has a bean name of lambdaFlow.transformer#0
(starting with version 5.1), where instead of a fully qualified name of the MethodInvokingTransformer
class, its component type is used.
The same pattern is applied for all the NamedComponent
s when the bean name has to be generated within the flow.
These generated bean names are prepended with the flow ID for purposes such as parsing logs or grouping components together in some analysis tool, as well as to avoid a race condition when we concurrently register integration flows at runtime.
See Dynamic and Runtime Integration Flows for more information.