Apache camel apachecamel:路由中的多线程

Apache camel apachecamel:路由中的多线程,apache-camel,Apache Camel,字符串processFiles=”file://somedirectory?readLock=rename&preMove=inprogress/&move=../processed/&moveFailed=../error/" 字符串后处理器=”file://somedirectory/inprogress"; 从(进程文件) .螺纹(10) .routeId(“someId”) .to(“bean:somebean”) 从(后处理器) .routeId(“后期处理”) .to(“bean

字符串processFiles=”file://somedirectory?readLock=rename&preMove=inprogress/&move=../processed/&moveFailed=../error/"

字符串后处理器=”file://somedirectory/inprogress";

从(进程文件) .螺纹(10) .routeId(“someId”)
.to(“bean:somebean”)

从(后处理器) .routeId(“后期处理”) .to(“bean:postProcessorBean”)

使用多个线程从特定位置读取文件。然后在“somebean”中完成处理

现在,我想在所有线程完成第一条路由后进行一些后处理。 我不知道什么时候该调用后处理器。 我上面所做的是给出错误的结果。在所有线程完成“Somebean”之前调用后处理器。 我想知道当所有线程都完成“somebean”后,如何调用postProcess

只是为了说明问题: 1.我们有很多文件,每个文件在一个位置有数百万条记录。我们需要读取这些文件并将数据保存在数据库中。 2.完成后,更新其他表中的状态

解决方案已经到位。但目前它需要更多的时间。因此,我们尝试在驼峰路由级别使用线程,以便可以同时处理多个文件。
现在,我们可以将时间减至最少,但无法进行后处理(即步骤2)

在一些可配置数量的消息或超时后使用重新加入使用聚合器模式:

from("file:src/main/resources/data/parallel-file-processing?noop=true")
    .threads(10)
    .process(new PreProcessor())
    .aggregate(constant(true), new ArrayListAggregationStrategy())
    .completionFromBatchConsumer()
    .process(new PostProcessor());
预处理器
实现
处理器
接口,在10个单独的线程中完成耗时的工作

使用取自驼峰聚合器的
ArrayListAggregatorStrategy
将预处理结果聚合为
aggregate

最后,后处理也并行进行:

from("file:src/main/resources/data/parallel-file-processing?noop=true")
    .threads(10)
    .process(new PreProcessor())
    .aggregate(constant(true), new ArrayListAggregationStrategy())
    .completionFromBatchConsumer()
    .parallelProcessing()  // <- for parallel processing
    .process(new PostProcessor());
from(“文件:src/main/resources/data/parallel file processing?noop=true”)
.螺纹(10)
.process(新的预处理器())
.aggregate(常量(true),新的ArrayListAggregationStrategy())
.completionFromBatchConsumer()

.parallelProcessing()//嗨..我尝试过这种方法..但多线程究竟发生在哪里呢?在聚合器中,一个接一个地拾取文件。post-pocessor是我的后处理逻辑的来源,请解释一下
private static class PostProcessor implements Processor {
    @SuppressWarnings("unchecked")
    @Override
    public void process(final Exchange exchange) throws Exception {
        final Object body = exchange.getIn().getBody();
        final List<GenericFile<File>> list = (List<GenericFile<File>>) body;
        for (final GenericFile<File> genericFile : list) {
            LOG.info("file = " + genericFile.getAbsoluteFilePath());
        }
    }
}
from("file:src/main/resources/data/parallel-file-processing?noop=true")
    .threads(10)
    .process(new PreProcessor())
    .aggregate(constant(true), new ArrayListAggregationStrategy())
    .completionFromBatchConsumer()
    .parallelProcessing()  // <- for parallel processing
    .process(new PostProcessor());