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