2011-04-18 107 views
6

我有以下Spring配置Apache的駱駝FTP消費者加載相同的文件一次又一次

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://camel.apache.org/schema/spring 
     http://camel.apache.org/schema/spring/camel-spring.xsd"> 

    <bean id="downloadLogger" class="com.thomsonreuters.oa.sdi.camel.DownloadLogger" /> 

    <bean id="fileFilter" class="com.thomsonreuters.oa.sdi.camel.IgnoreReadyFilesFilter" /> 

    <camelContext xmlns="http://camel.apache.org/schema/spring"> 
     <route> 
      <from uri="ftp://url_to_ftp?password=*******&amp;noop=true&amp;stepwise=false&amp;binary=true&amp;consumer.delay=10s&amp;recursive=true&amp;filter=#fileFilter" /> 
      <process ref="downloadLogger" /> 
      <to uri="file:data/outbox" /> 
     </route> 
    </camelContext> 

</beans> 

在ftp方我有,我要下載的文件3個文件夾。我想達到以下情形:

  1. 在FTP上的文件(isntance 5)在第一數據拉動消費的固定量加載這些文件複製到目標文件夾
  2. 在第二次嘗試加載文件,FTP狀態仍然是相同的(5檔)和駱駝FTP消費者少了點什麼(除了檢查新文件)
  3. 到FTP到達新的2個文件,在這個數據上拉消費者只下載這些新的兩個文件

目前,我目前的解決方案每次都會下載所有文件un dataload進程,我如何管理下載文件的信息以防止下載重複文件(我的意思是已經從ftp複製文件),我可以編寫自己的篩選器,篩選出已經下載的文件,但是我相信那裏應該內置功能會給我這個控制權(也許idempotentRepository,實際上我不確定)...

+0

什麼版本的駱駝您使用的是?這5個文件是在同一個文件夾中,還是在不同的子文件夾中? – 2011-04-19 07:42:19

+0

駱駝版本是2.7.0。有1個根目錄和3個子文件夾,每個子文件夾可能包含不同數量的文件。更具體地說,讓我們考慮一個子文件夾最初包含5個文件,然後到這個文件夾到達另外2個文件。我想阻止下載已經在目標文件夾中的文件。 – endryha 2011-04-19 08:35:13

+0

嗯noop = true也意味着冪等性。但是嘗試在uri中使用idempotent = true – 2011-04-19 14:05:54

回答

12

如果您希望Camel能夠在重新啓動之間記住它以前下載過哪些文件,則需要使用持久性冪等存儲庫。

您需要設置上的FTP端點此選項:idempotentRepository

看到更多細節在這裏:http://camel.apache.org/file2 (注:FTP組件繼承從文件組件的選項)

有wiki頁面上的一些示例如何使用不同的商店。你也可以建立自定義商店。

+0

謝謝,這正是我想要實現的。 – endryha 2011-04-20 15:42:35

4

最後我結束了以下解決方案:

public class SdiRouteBuilder extends RouteBuilder { 
    @Override 
    public void configure() throws Exception { 
     from("ftp://[email protected]_to_ftp/RootFolder?" + 
       "password=******&noop=true&stepwise=false&binary=true&consumer.delay=10s&recursive=true&filter=#fileFilter") 
       .idempotentConsumer(header("CamelFileName"), FileIdempotentRepository.fileIdempotentRepository(new File("data", "repo.dat"))) 
       .process(new DownloadLogger()) 
       .to("file:data/outbox"); 
    } 
} 
+0

感謝您的提示。它似乎不直接爲ftp工作(爲文件使用者設置屬性)。它似乎只有在路由上使用idempotentConsumer時纔有效。 – dermoritz 2014-07-10 07:26:34

1

也許@endryha在2011年回答做工精良,但不能與駱駝2.20.1

在駱駝2.20.1,這些代碼將創建兩個idempotentRepository

  1. FTP默認內存idempotentRepository
  2. idempotentConsumer定製idempotentRepository(基於文件的日是的情況下)

因此正確使用idempotentRepository的方式(我去除大部分參數可讀性)

"ftp://[email protected]_to_ftp/RootFolder?&idempotent=true&idempotentRepository=#myIdempotentRepo" 

和豆

@Bean 
private IdempotentRepository<String> myIdempotentRepo() { 
    return FileIdempotentRepository.fileIdempotentRepository(new File("data", "repo.dat"); 
}