此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.3! |
Service Activator 和.handle()
方法
这.handle()
EIP 方法的目标是调用任何MessageHandler
implementation 或任何方法。
另一种选择是使用 lambda 表达式定义 “活动”。
因此,我们引入了一个泛型GenericHandler<P>
functional 接口。
其handle
method 需要两个参数:P payload
和MessageHeaders headers
(从版本 5.1 开始)。
有了这个,我们可以定义一个 flow,如下所示:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlow.from("flow3Input")
.<Integer>handle((p, h) -> p * 2)
.get();
}
前面的示例将它接收的任何整数加倍。
但是,Spring 集成的一个主要目标是loose coupling
,通过从消息负载到消息处理程序的目标参数的运行时类型转换。
由于 Java 不支持 lambda 类的泛型类型解析,因此我们引入了一种解决方法,即payloadType
参数,以及LambdaMessageProcessor
.
这样做会将硬转换工作委托给 Spring 的ConversionService
,它使用提供的type
以及请求的 message 到 target 方法参数。
以下示例显示了生成的IntegrationFlow
可能看起来像:
@Bean
public IntegrationFlow integerFlow() {
return IntegrationFlow.from("input")
.<byte[], String>transform(p - > new String(p, "UTF-8"))
.handle(Integer.class, (p, h) -> p * 2)
.get();
}
我们也可以注册一些BytesToIntegerConverter
在ConversionService
去掉那个额外的.transform()
:
@Bean
@IntegrationConverter
public BytesToIntegerConverter bytesToIntegerConverter() {
return new BytesToIntegerConverter();
}
@Bean
public IntegrationFlow integerFlow() {
return IntegrationFlow.from("input")
.handle(Integer.class, (p, h) -> p * 2)
.get();
}
另请参阅lambda 和Message<?>
参数.