DSL 拡張

バージョン 5.3 以降、IntegrationFlowExtension が導入され、カスタムまたは構成された EIP オペレーターで既存の Java DSL を拡張できるようになりました。必要なのは、IntegrationFlow Bean 定義で使用できるメソッドを提供するこのクラスの拡張だけです。拡張クラスは、カスタム IntegrationComponentSpec 構成にも使用できます。たとえば、既存の IntegrationComponentSpec 拡張機能で、欠落またはデフォルトのオプションを実装できます。以下のサンプルは、複合カスタムオペレーターと、デフォルトのカスタム outputProcessor に対する AggregatorSpec 拡張の使用箇所を示しています。

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