1

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();
    }
Mhel
  • 50
  • 1
  • 7

1 Answers1

3

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

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thank you so much @Artem Bilan! :D Here's my 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(); } ``` Just removed the FileExistsMode since i need the default behavior and set the value of remote directory. – Mhel Mar 18 '20 at 23:23