2014-07-17 44 views
0

是否有機會在java代碼內更改/設置入站通道適配器的某些屬性值? 我需要從ftp服務器下載特定的文件。在運行應用程序之前我不知道哪個文件,所以我必須動態設置filename-pattern。 我發現有一個關於這個問題,但沒有正確答案,所以我再次問。 這是我的inbound-channel-adapter配置:使用java代碼動態設置ftp的屬性:入站通道適配器

<int-ftp:inbound-channel-adapter local-directory="ftp" 
      channel="getFtpChannel" 
      session-factory="ftpClientFactory" 
      charset="UTF-8" 
      remote-directory="${remote-download-directory}" 
      remote-file-separator="/" 
      auto-create-local-directory="true" 
      delete-remote-files="false" 
      filename-pattern=""> 
    <int:poller fixed-rate="10000"/> 
</int-ftp:inbound-channel-adapter> 

我想設置/更改filename-pattern。 這是我的代碼,根據這個屬性,但只有當我把它在我的配置接收文件:

ConfigurableApplicationContext context = 
     new FileSystemXmlApplicationContext("/src/citrus/resources/citrus-context.xml"); 
PollableChannel channel = 
     context.getBean("getFtpChannel", PollableChannel.class); 
channel.receive(); 
try { 
    Thread.sleep(10000); 
} catch (InterruptedException e) { 
    fail(e.getMessage()); 
} 
context.close(); 

回答

2

不,你不能在運行時改變filename-pattern,因爲該選項被填充到FtpSimplePatternFileListFilterfinalpath

但是,您可以實施自己的FileListFilter<?>並將其注入<int-ftp:inbound-channel-adapter>filter選項。

由於您的FileListFilter<?>將成爲一個bean,您可以簡單地從上下文中獲取並調用它的setPattern()

UPDATE

事情是這樣的:

public class MyFtpPatterFileListFilter extends AbstractFileListFilter<FTPFile> { 

    private final AntPathMatcher matcher = new AntPathMatcher(); 

    private volatile String pattern; 

    public void setPattern(String pattern) { 
     this.pattern = pattern; 
    } 

    @Override 
    protected boolean accept(FTPFile file) { 
     return !StringUtils.hasText(this.pattern) || this.matcher.match(this.pattern, this.getFilename(file)); 
    } 

    private String getFilename(FTPFile file) { 
     return (file != null) ? file.getName() : null; 
    } 

} 

使用

<int-ftp:inbound-channel-adapter filter="myFtpFilter"> 

和修改:

MyFtpPatterFileListFilter filter = context.getBean("myFtpFilter", MyFtpPatterFileListFilter.class); 

filter.setPattern("*.txt"); 
+0

我知道,我所要求的A L ot,但你能給我一些提示,我該怎麼做?我必須實現'boolean accept()'。我應該通過'消息'對象這個方法?和(如果是的話)我怎樣才能訪問它的模式? – mlethys

+0

增加了一些潛在的實現。 –

+0

經過一些修改後,它可以很好地工作。非常感謝! – mlethys

相關問題