跳到主要内容

DSL 扩展

QWen Plus 中英对照 DSL Extensions

从 5.3 版本开始,引入了 IntegrationFlowExtension 以允许使用自定义或组合的 EIP 操作符扩展现有的 Java DSL。所需要做的就是扩展这个类,并提供可以在 IntegrationFlow bean 定义中使用的方法。扩展类也可以用于自定义 IntegrationComponentSpec 配置;例如,可以在现有的 IntegrationComponentSpec 扩展中实现缺失或默认选项。下面的示例演示了一个复合自定义操作符以及用于默认自定义 outputProcessorAggregatorSpec 扩展的用法:

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(", ")));
}

}
java

对于方法链流程,这些扩展中的新 DSL 操作符必须返回扩展类。这样,目标 IntegrationFlow 定义将能够使用新旧 DSL 操作符:

@Bean
public IntegrationFlow customFlowDefinition() {
return
new CustomIntegrationFlowDefinition()
.log()
.upperCaseAfterSplit()
.channel("innerChannel")
.customAggregate(customAggregatorSpec ->
customAggregatorSpec.expireGroupsUponCompletion(true))
.logAndReply();
}
java