在Spring Boot中从FTP发送和接收文件

rus*_*off 16 spring ftps spring-integration spring-boot

我是Spring Framework的新手,事实上,我正在学习和使用Spring Boot.最近,在我正在开发的应用程序中,我使Quartz Scheduler工作,现在我想让Spring Integration在那里工作:FTP连接到服务器来写入和读取文件.

我想要的是非常简单(因为我已经能够在以前的Java应用程序中这样做).我有两个Quartz Jobs计划每天在不同的时间点击:其中一个从FTP服务器读取文件,另一个将文件写入FTP服务器.

我将详细介绍我迄今为止开发的内容.

@SpringBootApplication
@ImportResource("classpath:ws-config.xml")
@EnableIntegration
@EnableScheduling
public class MyApp extends SpringBootServletInitializer {

    @Autowired
    private Configuration configuration;

    //...

    @Bean
    public DefaultFtpsSessionFactory  myFtpsSessionFactory(){
        DefaultFtpsSessionFactory sess = new DefaultFtpsSessionFactory();
        Ftp ftp = configuration.getFtp();
        sess.setHost(ftp.getServer());
        sess.setPort(ftp.getPort());
        sess.setUsername(ftp.getUsername());
        sess.setPassword(ftp.getPassword());
        return sess;
    }

}
Run Code Online (Sandbox Code Playgroud)

我将以下课程命名为FtpGateway,如下所示:

@Component
public class FtpGateway {

    @Autowired
    private DefaultFtpsSessionFactory sess;

    public void sendFile(){
        // todo
    }

    public void readFile(){
        // todo
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在阅读文档以学习如何操作.Spring Integration的FTP似乎是事件驱动的,所以我不知道如何在确切的时间触发触发器时执行Jobs中的sendFile()和readFile().

文档告诉我一些使用入站通道适配器(从FTP读取文件?),出站通道适配器(将文件写入FTP?)和出站网关(做什么?):

Spring Integration支持通过FTP/FTPS发送和接收文件,提供三个客户端端点:入站通道适配器,出站通道适配器和出站网关.它还为定义这些客户端组件提供了方便的基于命名空间的配置选项.

所以,我还没有明白如何遵循.

拜托,有人可以给我一个暗示吗?

谢谢!

编辑:

谢谢@M.Deinum.首先,我将尝试一个简单的任务:从FTP读取文件,轮询器将每5秒运行一次.这就是我添加的内容:

@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(myFtpsSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setPreserveTimestamp(true);
    fileSynchronizer.setRemoteDirectory("/Entrada");
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.csv"));
    return fileSynchronizer;
}


@Bean
@InboundChannelAdapter(channel = "ftpChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> ftpMessageSource() {
    FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(inbound);
    source.setLocalDirectory(new File(configuracion.getDirFicherosDescargados()));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

@Bean
@ServiceActivator(inputChannel = "ftpChannel")
public MessageHandler handler() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Object payload = message.getPayload();
            if(payload instanceof File){
                File f = (File) payload;
                System.out.println(f.getName());
            }else{
                System.out.println(message.getPayload());
            }
        }

    };
}
Run Code Online (Sandbox Code Playgroud)

然后,当应用程序运行时,我将一个新的csv文件介绍"Entrada"远程文件夹,但是handler()方法在5秒后没有运行...我做错了什么?

Mr *_*ody 1

请在您的轮询器方法上添加@Scheduled(fixedDelay = 5000)