DSL 扩展
从版本 5.3 开始,IntegrationFlowExtension
以允许使用自定义或组合的 EIP 运算符扩展现有 Java DSL。
所需要的只是这个类的扩展,它提供可以在IntegrationFlow
bean 定义。
扩展类也可以用于自定义IntegrationComponentSpec
配置;例如,missed 或 default 选项可以在现有的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
definition 将与新的和现有的 DSL 运算符一起使用:
@Bean
public IntegrationFlow customFlowDefinition() {
return
new CustomIntegrationFlowDefinition()
.log()
.upperCaseAfterSplit()
.channel("innerChannel")
.customAggregate(customAggregatorSpec ->
customAggregatorSpec.expireGroupsUponCompletion(true))
.logAndReply();
}