我如何使用camel(使用ftp)读取两个不同的文件并编写唯一的JSON? 据我所知,“来自”只能读取一个文件。
from("ftp://myftp@localhost:21" +
"/myFolder/" +
"?" +
"password=RAW(myPassword)" +
"&include=file1.txt" +
"&passiveMode=true" +
"&delete=true")
.log("Connected to FTP 1")
最佳答案
我认为您需要的是一个 pollEnrich
模式,如下所示:
from("ftp:...&include=file1.txt...")
.log("Connected to FTP 1")
.pollEnrich("ftp:...&include=file2.txt...", new MyFileAggregationStrategy() )
或者,如果您需要指定动态文件名,如下所示:
from("ftp:...&include=file1.txt...")
.log("Connected to FTP 1")
.pollEnrich().simple("ftp:...&include=${header.file2Name}...")
.aggregationStrategy( new MyFileAggregationStrategy() )
您需要创建一个 AggregationStrategy ,这可以直接创建 JSON,也可以将输出发送到创建 JSON 的处理器。
您需要查看 pollEnrich 上的手册,以确保您正确处理如果 file1.txt
出现而没有相应的 file2.txt
时发生的情况,这样它就不会阻塞,除非您希望它阻塞(其中如果您需要能够在 AggregationStrategy 中处理 null)。
我希望这会有所帮助。
关于java - 用camel处理2个不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59155404/