简体   繁体   中英

How to upload file via SFTP inside an IntegrationFlow chain using Spring Integration?

What I'm trying to do is to check my local source directory for new file(s), do a simple transformation then send file(s) to a remote server via SFTP Using Spring Integration.

I'm using file adapter to poll my source directory for new files. Now, I want to upload the file to remote server right after transforming it. Is there a message handler that do exactly that so I can trigger the upload inside my IntegrationFlow chain or should I write the file and SFTP upload it on a separate process (Create a scheduled job just for upload process)?

    @Bean
    public IntegrationFlow integrationFlow() {
        return IntegrationFlows.from(fileReader(), spec -> spec.poller(Pollers.fixedDelay(1000)))
                .transform(transformer, "transform")
                .handle( message handler )
                .get();
    }

    @Bean
    public FileReadingMessageSource fileReader() {
        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setDirectory(new File("src/main/resources/file/outbox"));
        return source;
    }

Updated Code:

@Bean
    public IntegrationFlow integrationFlow() {
        return IntegrationFlows.from(fileReader(), spec -> spec.poller(Pollers.fixedDelay(1000)))
                .transform(transformer, "transform")
                .handle(Sftp.outboundAdapter(sftpSessionFactory)
                        .remoteDirectory(sftpRemoteDirectory)
                )
                .get();
    }

You are on the right track. There is indeed a channel adapter for SFTP to upload payload of the message as a file into a remote directory:

handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL)

See more info in the SFTP chapter of the Spring Integration documentation:

https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-outbound

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM