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