Move file after successful ftp transfer using Java DSL

后端 未结 2 1338
忘掉有多难
忘掉有多难 2020-12-18 13:38

I\'ve defined a flow using spring-integration java dsl to ftp transfer a file, handle it, then transfer it back in an \"archive\" dir, and at last move it in a local archive

2条回答
  •  长情又很酷
    2020-12-18 14:31

    As a workaround I refactored my code in giving up with the file adapter:

    @Bean
    public IntegrationFlow ftpInboundFlow() {
        return IntegrationFlows
            .from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
                    .filter(compositeWithAcceptOnceFilter())                        .remoteDirectory(ftpProperties.getRemoteDirectory())
                    .localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
            consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
            .handle(new GenericHandler() {
    
                @Override
                @Transactional
                public Object handle(File payload, Map headers) {
                    logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
                    /* handle file content here ... */
                    /* ... then move it to archive */
                    File dstFile = new File("archive", payload);
                    FileUtils.moveFile(payload, dstFile);
                    return dstFile;
                }
    
            }) /* Fine GenericHandler */
            /* Archive the remote file */
            .handleWithAdapter(a -> a.ftp(ftpSessionFactory())
                    .remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))              
            .get();
    

    }

提交回复
热议问题