简体   繁体   English

每天使用Camel从FTP服务器读取带有时间戳的文件

[英]Read a datestamped file from an FTP server everyday using Camel

I am trying to read a date stamped file from an FTP server each day, at say 16:15. 我正在尝试每天从FTP服务器读取一个带有日期戳的文件,例如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. 为了了解如何执行此操作,我尝试每分钟一次连接到FTP服务器,并每次增加文件号。

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. 运行此程序时,它每分钟打印一次"Triggered the process" It is also calling getFtpServerUrl . 它还正在调用getFtpServerUrl

It is not calling RateServiceImpl.update . 它没有调用RateServiceImpl.update It is not logging "Downloaded file ${file:name} complete." 它没有记录"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? 每分钟都有一种更简单的方法来构造fileName吗?
  • Why doesn't RateServiceImpl.update get called? 为什么不调用RateServiceImpl.update

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. 要从处理器添加路由,您需要将路由添加到camelContext而不使用父类RouteBuilder ,这是错误的。

The ftp consumer has built in support for cron expressions, so you can configure it to run daily at 4:15 pm. ftp使用者内置了对cron表达式的支持,因此您可以将其配置为每天下午4:15运行。 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 在这里的cron

And I wrote a blog about it 我写了一个博客

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

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