javakotlinapache-camelcamel-ftp

Process 2 different files with camel


how can i read two different files with camel (using ftp) and write an unique JSON? From what i know the "from" can only read one file.

from("ftp://myftp@localhost:21" +
                "/myFolder/" +
                "?" +
                "password=RAW(myPassword)" +
                "&include=file1.txt" +
                "&passiveMode=true" +
                "&delete=true")
            .log("Connected to FTP 1")

Solution

  • I think what you need is a pollEnrich pattern which would look something like this:

    from("ftp:...&include=file1.txt...")
        .log("Connected to FTP 1")
        .pollEnrich("ftp:...&include=file2.txt...", new MyFileAggregationStrategy() )
    

    or, if you need to specify a dynamic file name, like this:

    from("ftp:...&include=file1.txt...")
        .log("Connected to FTP 1")
        .pollEnrich().simple("ftp:...&include=${header.file2Name}...")
            .aggregationStrategy( new MyFileAggregationStrategy() )
    

    You will need to create an AggregationStrategy, and that could either create your JSON directly, or you could sent the output to a processor that creates your JSON.

    You will need to check the manual on pollEnrich, to ensure you handle correctly what happens if a file1.txt appears without a corresponding file2.txt, so that it doesn't block unless you want it to (in which case you need to be able to handle nulls in your AggregationStrategy).

    I hope this helps.