DSL 扩展
从版本 5.3 开始,引入了 IntegrationFlowExtension,允许通过自定义或组合的 EIP 操作符来扩展现有的 Java DSL。只需扩展此类,并提供可在 IntegrationFlow bean 定义中使用的方法即可。该扩展类也可用于自定义 IntegrationComponentSpec 配置;例如,可以在现有的 IntegrationComponentSpec 扩展中实现缺失或默认的选项。以下示例演示了一个组合自定义操作符,以及为默认自定义 outputProcessor 使用 AggregatorSpec 扩展:
public class CustomIntegrationFlowDefinition
extends IntegrationFlowExtension<CustomIntegrationFlowDefinition> {
public CustomIntegrationFlowDefinition upperCaseAfterSplit() {
return split()
.transform("payload.toUpperCase()");
}
public CustomIntegrationFlowDefinition customAggregate(Consumer<CustomAggregatorSpec> aggregator) {
return register(new CustomAggregatorSpec(), aggregator);
}
}
public class CustomAggregatorSpec extends AggregatorSpec {
CustomAggregatorSpec() {
outputProcessor(group ->
group.getMessages()
.stream()
.map(Message::getPayload)
.map(String.class::cast)
.collect(Collectors.joining(", ")));
}
}
对于方法链流程,这些扩展中的新DSL运算符必须返回扩展类。这样,目标IntegrationFlow定义将能够同时使用新的和现有的DSL运算符:
@Bean
public IntegrationFlow customFlowDefinition() {
return
new CustomIntegrationFlowDefinition()
.log()
.upperCaseAfterSplit()
.channel("innerChannel")
.customAggregate(customAggregatorSpec ->
customAggregatorSpec.expireGroupsUponCompletion(true))
.logAndReply();
}