Bikash Mohapatra
Bikash Mohapatra

Reputation: 89

Download files to a local folder by using Spring Integration in spring boot application

I am new to spring integration framework. Currently i am working on a project which has a requirement to download the files to a local directory.

My goal is to complete the below task

1.Download the files by suing spring integration to a local directory

2.Trigger a batch job.It means to read the file and extract a specific column information.

I am able to connect to SFTP server.But facing difficulty how to use spring integration java DSL to download the files and trigger a batch job.

Below code to connect to SFTP Session Factory

@Bean
    public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(sftpHost);
        factory.setPort(sftpPort);
        factory.setUser(sftpUser);

        if (sftpPrivateKey != null) {
            factory.setPrivateKey(sftpPrivateKey);
            factory.setPrivateKeyPassphrase(privateKeyPassPhrase);
        } else {
            factory.setPassword("sftpPassword");
        }

        factory.setPassword("sftpPassword");
        logger.info("Connecting to SFTP Server" + factory.getSession());
        System.out.println("Connecting to SFTP Server" + factory.getSession());
        factory.setAllowUnknownKeys(true);
        return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
    }

Below code to download the files from remote to local

@Bean
    public IntegrationFlowBuilder integrationFlow() {
        return IntegrationFlows.from(Sftp.inboundAdapter(sftpSessionFactory()));

    }

I am using spring integration dsl. i am not able to get what to code here.

I am trying many possible ways to do this.But not able to get how to proceed with this requirement.

Can anyone one help me how to approach at this and if possible share me a sample code for reference?

Upvotes: 2

Views: 709

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

The Sftp.inboundAdapter() produces messages with a File as a payload. So, having that IntegrationFlows.from(Sftp.inboundAdapter(sftpSessionFactory())) you can treat as a first task done.

Your problem from here that you don't make an integrationFlow, but rather return that IntegrationFlowBuilder and register it as a @Bean. That's where it doesn't work for you.

You need to continue a flow definition and call its get() in the end to return an integrationFlow instance which already has to be registered as a bean. If this code flow is confusing a bit, consider to implement an IntegrationFlowAdapter as a @Component.

To trigger a batch job you need consider to use a FileMessageToJobRequest in a .transform() EIP-method and then a JobLaunchingGateway in a .handle() EIP-method.

See more info in docs:

https://docs.spring.io/spring-integration/reference/html/dsl.html#java-dsl https://docs.spring.io/spring-integration/reference/html/sftp.html#sftp-inbound https://docs.spring.io/spring-batch/docs/4.3.x/reference/html/spring-batch-integration.html#spring-batch-integration-configuration

BTW, the last one has a flow sample exactly for your use-case.

Upvotes: 2

Related Questions