使用Spring-Integration下载FTP文件?

时间:2014-06-03 08:53:51

标签: java spring ftp spring-integration

我想定期从ftp服务器下载文件(仅当文件发生变化时)。因此,我想使用Spring-Integration 4.0。

基于注释的配置与int-ftp相同的以下设置是什么?

<int-ftp:inbound-channel-adapter id="ftpInbound"
            channel="ftpChannel"
            session-factory="ftpClientFactory"
            filename-pattern="*.txt"
            auto-create-local-directory="true"
            delete-remote-files="false"
            remote-directory="/">
    <int:poller fixed-rate="1000"/>
</int-ftp:inbound-channel-adapter>

我从以下开始,但不知道如何使用session-factoryremote-directory等属性配置频道。

@Configuration
@EnableIntegration
public class Application {
    @Bean
    public SessionFactory<FTPFile> sessionFactory() {
        DefaultFtpSessionFactory ftp = new DefaultFtpSessionFactory();
        ftp.setHost("ftp.test");
        ftp.setPort(21);
        ftp.setUsername("anonymous");
        ftp.setPassword("anonymous");
        return ftp;
    }

    @InboundChannelAdapter(value = "ftpChannel", poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
    public String connect() {
        // return the ftp file
    }

    @ServiceActivator(inputChannel = "ftpChannel")
    public void foo(String payload) {
        System.out.println("paylod: " + payload);
    }

}

1 个答案:

答案 0 :(得分:3)

(S)FTP入站适配器位于更复杂的一侧;我们在DSL中工作以使这更容易,但是,目前,您需要@Bean FtpInboundFileSynchronizer连接适当的属性并将其注入

然后

@Bean
@InboundChannelAdapter(value = "ftpChannel", poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource receive() {
    FtpInboundFileSynchronizingMessageSource messageSource = new FtpInboundFileSynchronizingMessageSource(synchronizer());
    ...
    return messageSource;
}