简体   繁体   English

等待所有文件被消耗,然后再触发下一条路由

[英]Wait for all files to be consumed before triggering next route

I have route consuming a file location (recursive) and a route processing files at another file location. 我的路由消耗了一个文件位置(递归),而路由处理了另一个文件位置的文件。 Sometimes the first route might find multiple files. 有时第一个路线可能会找到多个文件。

from(fileLocation)
 .autoStartup(true)
 .routeId("file-scanner")
 .to(newFileLocation)
 .to("direct:processFile")

from("direct:processFile")
 .routeId("file-processor")
 .bean(*doing some stuff with new file location*)

So what ends up being the case sometimes is that file-scanner copies one file, file-processor process the file, and then file-scanner copies one more file and then it runs again. 因此,有时最终情况是file-scanner复制一个文件, file-processor处理该文件,然后file-scanner复制一个文件,然后再次运行。

What I essentially want is that file-scanner copies all the files before file-processor starts to process the files. 我本质上想要的是file-scannerfile-processor开始处理文件之前复制所有文件。 In that way, I can run the processing only once. 这样,我只能运行一次处理。

The fileLocation I consume from is defined with a config looking like this: 我从中使用的fileLocation由一个配置定义,如下所示:

recursive=true&noop=true&idempotent=false&delay=3600000&include=.*.json&autoCreate=false&startingDirectoryMustExist=true&readLock=none

All decisions revolve around batch consumer exchange properties. 所有决策都围绕批量消费者交换属性。 I think, you can implement two very different solution: 我认为,您可以实现两个非常不同的解决方案:

  • Solution, based on aggregator integration pattern. 解决方案,基于聚合器集成模式。 You need to aggregate all files from batch into, maybe, list of strings, because your files contains JSON. 您需要将所有文件从批处理聚合到字符串列表中,因为您的文件包含JSON。 Aggregator option " completionFromBatchConsumer " can help you to aggregate all files consumed from a File endpoint in that given poll. 聚合器选项“ completeryFromBatchConsumer ”可以帮助您聚合在给定轮询中从文件端点消耗的所有文件。 After aggregation you can process aggregated files all together. 聚合后,您可以一起处理聚合文件。 Maybe you can develop custom aggregation strategy that will implement the logic of your bean, marked "doing some stuff with new file location". 也许您可以开发定制的聚合策略,该策略将实现bean的逻辑,标记为“用新文件位置做一些事情”。

  • Trigger, based on controlbus integration pattern: 触发,基于controlbus集成模式:

from(fileLocation) 
    .autoStartup(true)  
    .routeId("file-scanner")  
    .to(newFileLocation)
    .choice().when(exchangeProperty("CamelBatchComplete"))
    .process(new Processor() {
       @Override
       public void process(Exchange exchange) throws Exception {
           exchange.getContext().startRoute("file-processor");
       }
    })
    .end();

    from(newFileLocation).  
    .routeId("file-processor") 
    .autoStartup(false)   
    .bean(*doing some stuff with new file location*)
    .choice().when(exchangeProperty("CamelBatchComplete"))
    .process(new Processor() {
       @Override
       public void process(Exchange exchange) throws Exception {
           exchange.getContext().stopRoute("file-processor");
       }
    })
    .end();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM