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