此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
服务激活器和.handle()
方法
这.handle()
EIP 方法的目标是调用任何MessageHandler
实现或某些 POJO 上的任何方法。另一种选择是使用 lambda 表达式定义“活动”。因此,我们引入了一个泛型GenericHandler<P>
功能接口。 其handle
方法需要两个参数:P payload
和MessageHeaders headers
(从 5.1 版开始)。有了这个,我们可以定义一个流程,如下所示:
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlow.from("flow3Input")
.<Integer>handle((p, h) -> p * 2)
.get();
}
前面的示例将它收到的任何整数加倍。
然而,Spring Integration 的一个主要目标是loose coupling
,通过从消息有效负载到消息处理程序的目标参数的运行时类型转换。由于 Java 不支持 lambda 类的泛型类型解析,因此我们引入了一种解决方法,其中包含额外的payloadType
大多数 EIP 方法的参数和LambdaMessageProcessor
. 这样做将艰苦的转换工作委托给 Spring 的ConversionService
,它使用提供的type
以及请求的消息到目标方法参数。以下示例显示了生成的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<?>
参数.