springapache-camelcamel-ftp

Moving files to archive directory using apache camel ftp component


I have few files stored in a database, these files were fetched from a FTP server and processed.

I am trying to move these files to archive directory on FTP server and to do that I am using producerTemplate.

This is what I have done so far.

try {
    DefaultCamelContext camelContext = new DefaultCamelContext();
    ProducerTemplate template = camelContext.createProducerTemplate();
    template.request("ftp://xxxxx/include=fileFromDb.xml&move=archive/fileFromDb.xml", outExchange -> {
        String body = outExchange.getIn().getBody(String.class);                    
    });
} catch (Exception ex) {
    // update the status in database to indicate archive failed
}

But this is failing with following error and can't get this resolved.

WARN  o.a.c.c.f.remote.RemoteFileProducer - Writing file failed with: Cannot write null body to file: archive/ID-192-168-1-113-tpgi-com-au-1627667653835-1-2

I have tried implementing solutions from other answers but they didn't worked for either.

If I write a custom route like below, it works fine but because I need perform post move actions I prefere using producer template.

from("ftp://xxxxx/include=fileFromDb.xml&move=archive/fileFromDb.xml")
    .log("file moved successfully").

Solution

  • If I write a custom route like below, it works fine but because I need perform post move actions I prefere using producer template.

    You could use onCompletion to call another route to handle the post move actions.

    String sftpURI = "sftp:localhost:2222/upload" +
    "?username="+username+"&password="+password
    + "&move=done";
    
    String sftpPostMoveURI = "sftp:localhost:2222/upload/done" +
    "?username="+username+"&password="+password
    + "&fileName=${headers.CamelFileName}";
    
    from(sftpURI)
        .routeId("ReadFileUsingSFTPRoute")
        .onCompletion().onCompleteOnly()
            .setBody().constant("")
            .to("direct:postMoveActions")
        .end()
        .log("file ${headers.CamelFileName} moved");
    
    from("direct:postMoveActions")
        .routeId("PostMoveActionsRoute")
        .log("doing post move actions!")
        .pollEnrich().simple(sftpPostMoveURI).timeout(5000)
        .log("File: ${body}");