DSL 擴充套件
從 5.3 版本開始,引入了 IntegrationFlowExtension,允許使用自定義或組合的 EIP 運算子擴充套件現有 Java DSL。所需要的只是此類的擴充套件,它提供了可以在 IntegrationFlow bean 定義中使用的方法。擴充套件類還可以用於自定義 IntegrationComponentSpec 配置;例如,可以在現有的 IntegrationComponentSpec 擴充套件中實現缺少或預設的選項。下面的示例演示了一個複合自定義運算子和 AggregatorSpec 擴充套件在預設自定義 outputProcessor 中的用法
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();
}