如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

领域特定语言扩展

从版本 5.3 开始,引入了一个 IntegrationFlowExtension,用于通过自定义或组合的 EIP 操作符扩展现有的 Java DSL。 只需扩展此类,提供可在 IntegrationFlow Bean 定义中使用的相应方法即可。 该扩展类也可用于自定义 IntegrationComponentSpec 配置;例如,可以在现有的 IntegrationComponentSpec 扩展中实现缺失或默认选项。 下面的示例演示了复合自定义操作符的使用,以及针对默认自定义 outputProcessorAggregatorSpec 扩展用法:spring-doc.cadn.net.cn

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 运算符:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow customFlowDefinition() {
    return
            new CustomIntegrationFlowDefinition()
                    .log()
                    .upperCaseAfterSplit()
                    .channel("innerChannel")
                    .customAggregate(customAggregatorSpec ->
                            customAggregatorSpec.expireGroupsUponCompletion(true))
                    .logAndReply();
}