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