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

文件聚合器

从版本 5.5 开始,引入了FileAggregator以覆盖当启用 START/END 标记时FileSplitter用例的另一侧。 为方便起见,FileAggregator实现了所有三种序列详情策略:spring-doc.cadn.net.cn

  • 带有 HeaderAttributeCorrelationStrategy 属性的 FileHeaders.FILENAME 用于关联键计算。 当在 FileSplitter 上启用标记时,它不会填充序列详细信息头,因为 START/END 标记消息也包含在序列大小中。 对于每一行输出(包括 START/END 标记消息),FileHeaders.FILENAME 仍会被填充。spring-doc.cadn.net.cn

  • The FileMarkerReleaseStrategy - 检查组中的 FileSplitter.FileMarker.Mark.END 消息,然后将 FileHeaders.LINE_COUNT 头部的值与组大小减去 2 - FileSplitter.FileMarker 个实例进行比较。 它还实现了一个便捷的 GroupConditionProvider 联系人,用于在 AbstractCorrelatingMessageHandler 中调用 conditionSupplier 函数。 有关更多信息,请参见 消息组条件spring-doc.cadn.net.cn

  • FileAggregatingMessageGroupProcessor 仅会从组中移除 FileSplitter.FileMarker 条消息,并将其余消息收集到列表负载中以进行生产。spring-doc.cadn.net.cn

以下列表展示了配置 FileAggregator 的可能方式:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow fileSplitterAggregatorFlow(TaskExecutor taskExecutor) {
    return f -> f
            .split(Files.splitter()
                    .markers()
                    .firstLineAsHeader("firstLine"))
            .channel(c -> c.executor(taskExecutor))
            .filter(payload -> !(payload instanceof FileSplitter.FileMarker),
                    e -> e.discardChannel("aggregatorChannel"))
            .<String, String>transform(String::toUpperCase)
            .channel("aggregatorChannel")
            .aggregate(new FileAggregator())
            .channel(c -> c.queue("resultChannel"));
}
@Bean
fun fileSplitterAggregatorFlow(taskExecutor: TaskExecutor?) =
    integrationFlow {
        split(Files.splitter().markers().firstLineAsHeader("firstLine"))
        channel { executor(taskExecutor) }
        filter<Any>({ it !is FileMarker }) { discardChannel("aggregatorChannel") }
        transform(String::toUpperCase)
        channel("aggregatorChannel")
        aggregate(FileAggregator())
        channel { queue("resultChannel") }
    }
@serviceActivator(inputChannel="toAggregateFile")
@Bean
public AggregatorFactoryBean fileAggregator() {
    AggregatorFactoryBean aggregator = new AggregatorFactoryBean();
    aggregator.setProcessorBean(new FileAggregator());
    aggregator.setOutputChannel(outputChannel);
    return aggregator;
}
<int:chain input-channel="input" output-channel="output">
    <int-file:splitter markers="true"/>
    <int:aggregator>
        <bean class="org.springframework.integration.file.aggregator.FileAggregator"/>
    </int:aggregator>
</int:chain>

如果 FileAggregator 的默认行为不满足目标逻辑,建议配置一个聚合器端点并指定独立的策略。 有关更多信息,请参阅 FileAggregator JavaDocs。spring-doc.cadn.net.cn