Read a datestamped file from an FTP server everyday using Camel

586 views Asked by At

I am trying to read a date stamped file from an FTP server each day, at say 16:15. In an attempt to understand how to do this I am trying to connect to an FTP server, once a minute and incementing the file number each time.

The code that I have so far written is:

private String readFtp = "quartz2://exchange/readFtp?cron=";
private String cronExpression = "1 * * * * ?";

private int i=0;

@Override
public void configure() throws Exception {
    from(readFtp+cronExpression).process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {

            System.out.println("Triggered the process");

            from(getFtpServerUrl())
                    .bean(RateServiceImpl.class, "update")
                    .log("Downloaded file ${file:name} complete.");
        }
    });
}

private String getFtpServerUrl() {

    i++;
    System.out.println("Here with i=" + i);
    return String.format("ftp://%s:21/%s?username=%s&password=%s&fileName=%s", ftpServer, ftpPath, ftpUsername, ftpPassword, "rate"+i+".xml");
}

When I run this, it is printing "Triggered the process" once a minute. It is also calling getFtpServerUrl.

It is not calling RateServiceImpl.update. It is not logging "Downloaded file ${file:name} complete.".

  • Am I going about this in the wrong way?
  • Is there a simpler way to construct the fileName each minute?
  • Why doesn't RateServiceImpl.update get called?
1

There are 1 answers

0
Claus Ibsen On

You are doing this the wrong way. To add routes from a processor, you need to add the routes to the camelContext and not using the parent class RouteBuilder which is wrong.

The ftp consumer has built in support for cron expressions, so you can configure it to run daily at 4:15 pm. And then you would need to use a file filter to filter the files it find, and only pick the file you want.

The filter is documented at

The cron a bit here as well

And I wrote a blog about it