檔案聚合器

從 5.5 版本開始,引入了 FileAggregator,用於在啟用 START/END 標記時覆蓋 FileSplitter 的另一種用例。為方便起見,FileAggregator 實現了所有三種序列詳細資訊策略

  • 使用帶有 FileHeaders.FILENAME 屬性的 HeaderAttributeCorrelationStrategy 來計算關聯鍵。當 FileSplitter 上啟用標記時,它不會填充序列詳細資訊頭,因為 START/END 標記訊息也包含在序列大小中。FileHeaders.FILENAME 仍然會為發出的每一行填充,包括 START/END 標記訊息。

  • FileMarkerReleaseStrategy - 檢查組中是否存在 FileSplitter.FileMarker.Mark.END 訊息,然後將 FileHeaders.LINE_COUNT 頭值與組大小減去 2(即 FileSplitter.FileMarker 例項數)進行比較。它還實現了一個方便的 GroupConditionProvider 契約,用於在 AbstractCorrelatingMessageHandler 中使用 conditionSupplier 函式。更多資訊請參閱 訊息組條件

  • FileAggregatingMessageGroupProcessor 僅從組中移除 FileSplitter.FileMarker 訊息,並將其餘訊息收集到列表 payload 中進行生成。

以下列表顯示了配置 FileAggregator 的可能方法

  • Java DSL

  • Kotlin DSL

  • Java

  • XML

@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。