2014-12-02 97 views
0

我在我的應用程序中有以下配置。我正在寫一個文件到ftp文件夾並從ftp位置讀取。ftp出站通道適配器不傳輸相同的文件

  1. 我想從ftp位置獲取文件並將其保存在動態決定的目錄中。這是通過使用兩個鏈式通道適配器完成的。 ftp nbound通道適配器選取文件,將其放入本地目錄,然後文件入站通道適配器選取文件並將其放入其最終目的地。我需要爲這個過程過濾舊文件。 ftp入站通道適配器自定義過濾器在過濾方法中爲我提供了FTPFile對象。該對象給出最後修改日期,而不是文件放入過濾器的日期。由於這個限制,我不得不使用文件入站通道適配器。
  2. 因爲我不知道如何動態生成源目錄,所以我使用純java代碼將所需的文件複製到本地目錄,從ftp出站通道中選取它並放到ftp位置。

這是配置:

<bean id="fileNameGenerator" class="com.polling.util.FileNameGenerator"/> 
    <int-file:inbound-channel-adapter id="filesIn" directory="file:${paths.root}" channel="abc" filter="compositeFilter" > 
     <int:poller id="poller" fixed-rate="500" /> 

    </int-file:inbound-channel-adapter> 
    <int:channel id="abc"/> 
    <bean id="compositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter"> 
     <constructor-arg> 
      <list> 
       <!-- Ensures that the file is whole before processing it --> 
       <bean class="com.polling.util.CustomFileFilter"/> 
       <!-- Ensures files are picked up only once from the directory --> 
       <bean class="org.springframework.integration.file.filters.AcceptOnceFileListFilter" /> 
      </list> 
     </constructor-arg> 
    </bean> 
    <int-file:outbound-channel-adapter channel="abc" id="filesOut" 
     directory-expression="@outPathBean.getPath()" 
     delete-source-files="true" filename-generator="fileNameGenerator" > 
     <int-file:request-handler-advice-chain> 
     <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice"> 
      <property name="onSuccessExpression" value="payload.delete()" /> 
      </bean> 
    </int-file:request-handler-advice-chain> 
     </int-file:outbound-channel-adapter> 
    <bean id="ftpClientFactory" 
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory"> 
    <property name="host" value="${ftp.ip}"/> 
    <property name="port" value="${ftp.port}"/> 
    <property name="username" value="${ftp.username}"/> 
    <property name="password" value="${ftp.password}"/> 
    <property name="clientMode" value="0"/> 
    <property name="fileType" value="2"/> 
    <property name="bufferSize" value="100000"/> 
</bean> 

     <int:channel id="ftpChannel"/> 
<int-ftp:inbound-channel-adapter id="ftpInbound" 
    channel="ftpChannel" 
    session-factory="ftpClientFactory" 
    charset="UTF-8" 
    local-directory="file:${paths.root}" 
    delete-remote-files="true" 
    temporary-file-suffix=".writing" 
    remote-directory="${file.ftpfolder}" 

    filter="compositeFilterRemote" 
    preserve-timestamp="true" 
    auto-startup="true"> 
    <int:poller fixed-rate="1000"/> 
</int-ftp:inbound-channel-adapter> 
<int-ftp:outbound-channel-adapter id="ftpOutbound" 
    channel="ftpChannel" 
    session-factory="ftpClientFactory" 
    charset="UTF-8" 
    remote-file-separator="/" 
    auto-create-directory="true" 
    remote-directory="${file.ftpfolder}" 
    use-temporary-file-name="true" 
    temporary-file-suffix=".writing"> 
    <int-ftp:request-handler-advice-chain> 
     <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice"> 
      <property name="onSuccessExpression" value="payload.delete()" /> 
      </bean> 
    </int-ftp:request-handler-advice-chain> 
    </int-ftp:outbound-channel-adapter> 

在@使用建議Gary的回答,我添加了一個CustomFilter到FTP入境信道濾波器屬性。這將檢查文件名的正則表達式,如下所示:

@Override 
public List<FTPFile> filterFiles(FTPFile[] files) 
{ 
    List<FTPFile> ret = new ArrayList<FTPFile>(); 
    Pattern pattern = Pattern.compile("~.*?~"); 
    Matcher matcher; 
    for (FTPFile file : files) 
    { 
     matcher = pattern.matcher(file.getName()); 
     if(matcher.matches()) 
     { 
      ret.add(file); 
     } 
    } 
    return ret; 
} 

由於這個東西,文件被拾取併發送到最終目的地。

我的問題是:從最終目的地發送到FTP位置

  1. 的文件有不同的模式,我不知道放在哪裏(什麼東西@ @東西)。
  2. 有時ftp文件夾可能會被篡改並刪除文件。如果是這樣,我想春天重寫文件。在<ftp:inbound-channel-adapter>中提到的本地目錄中再次寫入相同的文件。但是,ftp出站通道適配器不會選擇文件並將其放入ftp位置。根據@ Gary的回答,我需要爲入站通道適配器配置過濾器,以避免接受一次過濾器。
  3. 這是否意味着我應該創建兩個單獨的渠道和兩個不同的流程,一個用於後面和一個用於前後?這是我的要求的正確方向嗎?

感謝您的幫助

編輯::

我試圖實現兩個不同的過程。一個用來從remotedir中獲取帶有pattern〜something〜的文件,另一個從本地目錄獲取帶有pattern @ something @ something的東西。雖然基本功能正在工作,但如果遠程目錄中的文件被刪除,我無法再次執行第二個流程。正如加里所說,接受所有過濾器需要到位,但我不知道如何使用自定義過濾器和接受所有過濾器。

任何建議都理解

回答

0

這是什麼工作

我繼續與二而改變了我的bean的配置,包括接受所有過濾器。

這是現在的配置。

 <bean id="fileNameGenerator" class="com.polling.util.FileNameGenerator"/> 
    <int-file:inbound-channel-adapter id="filesIn" directory="file:${paths.root}" channel="abc" filter="compositeFilter" > 
     <int:poller id="poller" fixed-rate="500" /> 

    </int-file:inbound-channel-adapter> 
    <int:channel id="abc"/> 
    <bean id="compositeFilter" class="org.springframework.integration.file.filters.CompositeFileListFilter"> 
     <constructor-arg> 
      <list> 
       <!-- Ensures that the file is whole before processing it --> 
       <bean class="com.polling.util.CustomFileFilter"/> 
       <!-- Ensures files are picked up only once from the directory --> 
       <bean class="org.springframework.integration.file.filters.AcceptOnceFileListFilter" /> 
      </list> 
     </constructor-arg> 
    </bean> 

    <int-file:outbound-channel-adapter channel="abc" id="filesOut" 
     directory-expression="@outPathBean.getPath()" 
     delete-source-files="true" filename-generator="fileNameGenerator" > 
     <int-file:request-handler-advice-chain> 
     <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice"> 
      <property name="onSuccessExpression" value="payload.delete()" /> 
      </bean> 
    </int-file:request-handler-advice-chain> 
     </int-file:outbound-channel-adapter> 
    <bean id="ftpClientFactory" 
    class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory"> 
    <property name="host" value="${ftp.ip}"/> 
    <property name="port" value="${ftp.port}"/> 
    <property name="username" value="${ftp.username}"/> 
    <property name="password" value="${ftp.password}"/> 
    <property name="clientMode" value="0"/> 
    <property name="fileType" value="2"/> 
    <property name="bufferSize" value="100000"/> 
</bean> 

    <int:channel id="ftpChannel1"/> 
<int-ftp:inbound-channel-adapter id="ftpInbound1" 
    channel="ftpChannel1" 
    session-factory="ftpClientFactory" 
    charset="UTF-8" 
    local-directory="file:${paths.root}" 
    delete-remote-files="true" 
    temporary-file-suffix=".writing" 
    remote-directory="." 
    preserve-timestamp="true" 
    auto-startup="true" 
    local-filter="compositeFilterLocal"> 
    <int:poller fixed-rate="1000"/> 
</int-ftp:inbound-channel-adapter> 
    <int-ftp:outbound-channel-adapter id="ftpOutbound1" 
    channel="ftpChannel1" 
    session-factory="ftpClientFactory" 
    charset="UTF-8" 
    remote-file-separator="/" 
    auto-create-directory="true" 
    remote-directory="${file.ftpfolder}" 
    use-temporary-file-name="true" 
    temporary-file-suffix=".writing"> 
    <int-ftp:request-handler-advice-chain> 
     <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice"> 
      <property name="onSuccessExpression" value="payload.delete()" /> 
      </bean> 
    </int-ftp:request-handler-advice-chain> 
</int-ftp:outbound-channel-adapter> 



    <int:channel id="ftpChannel"/> 
<int-ftp:inbound-channel-adapter id="ftpInbound" 
    channel="ftpChannel" 
    session-factory="ftpClientFactory" 
    charset="UTF-8" 
    local-directory="file:${paths.root}" 
    delete-remote-files="true" 
    temporary-file-suffix=".writing" 
    remote-directory="${file.ftpfolder}" 

    filter="compositeFilterRemote" 
    preserve-timestamp="true" 
    auto-startup="true"> 
    <int:poller fixed-rate="1000"/> 
</int-ftp:inbound-channel-adapter> 
<int-ftp:outbound-channel-adapter id="ftpOutbound" 
    channel="ftpChannel" 
    session-factory="ftpClientFactory" 
    charset="UTF-8" 
    remote-file-separator="/" 
    auto-create-directory="true" 
    remote-directory="${file.ftpfolder}" 
    use-temporary-file-name="true" 
    temporary-file-suffix=".writing"> 
    <int-ftp:request-handler-advice-chain> 
     <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice"> 
      <property name="onSuccessExpression" value="payload.delete()" /> 
      </bean> 
    </int-ftp:request-handler-advice-chain> 
    </int-ftp:outbound-channel-adapter> 

<bean id="acceptAllFilter" class="org.springframework.integration.file.filters.AcceptAllFileListFilter" /> 
<bean id="compositeFilterLocal" class="org.springframework.integration.file.filters.CompositeFileListFilter"> 
     <constructor-arg> 
      <list> 
       <!-- Ensures that the file is whole before processing it --> 
       <bean class="com.polling.util.CustomFileFilterLocal"/> 
       <!-- Ensures files are picked up only once from the directory --> 
       <bean class="org.springframework.integration.file.filters.AcceptAllFileListFilter" /> 
      </list> 
     </constructor-arg> 
    </bean> 
    <bean id="compositeFilterRemote" class="org.springframework.integration.file.filters.CompositeFileListFilter"> 
     <constructor-arg> 
      <list> 
       <!-- Ensures that the file is whole before processing it --> 
       <bean class="com.polling.util.CustomFileFilterRemote"/> 
       <!-- Ensures files are picked up only once from the directory --> 
       <bean class="org.springframework.integration.file.filters.AcceptOnceFileListFilter" /> 
      </list> 
     </constructor-arg> 
    </bean> 

如果有人有任何建議讓這個更高效,請讓我知道。截至目前我可以反覆下載文件。所以我繼續這..

謝謝

0

AcceptOnceFileListFilter默認配置。使用入站適配器上的local-filter可使用不同的過濾器(例如AcceptAllFileListFilter)。請參閱the documentation

這就是說,你的應用程序看起來很奇怪 - 看起來你正在獲取文件並簡單地發回它們。

+0

它是一個文件服務器。將文件從ftp更改爲通用文件,複製到文件服務器。當請求下載服務器上的定位文件時,將其放在由ftp頻道選取的本地文件夾中。希望有道理 – kavita 2014-12-04 04:59:18

+0

這涉及到出站適配器不呢?從本地目錄獲取到ftp文件夾的文件是出站適配器的工作權利? – kavita 2014-12-04 05:08:32

+0

否;入站獲取文件,然後通過消息發送給'ftpChannel'(如果它通過'local-filter')。但是,'ftpChannel'是在出站適配器上配置的通道。這就是爲什麼你的應用程序看起來很奇怪 - 獲取一個遠程文件並將其發回。 – 2014-12-04 13:55:35

相關問題