简体   繁体   English

使用 Spring 将文件下载到本地文件夹 集成在 spring 启动应用程序中

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

I am new to spring integration framework.我是 spring 集成框架的新手。 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 1.通过调用spring集成下载文件到本地目录

2.Trigger a batch job.It means to read the file and extract a specific column information. 2.触发批处理作业。意思是读取文件并提取特定的列信息。

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.我能够连接到 SFTP 服务器。但是遇到困难如何使用 spring 集成 java DSL 下载文件并触发批处理作业。

Below code to connect to SFTP Session Factory下面的代码连接到 SFTP Session 工厂

@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.我正在使用 spring 集成 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?谁能帮我解决这个问题,如果可能的话,可以分享一个示例代码以供参考吗?

The Sftp.inboundAdapter() produces messages with a File as a payload. Sftp.inboundAdapter()生成带有File作为有效负载的消息。 So, having that IntegrationFlows.from(Sftp.inboundAdapter(sftpSessionFactory())) you can treat as a first task done.因此,拥有IntegrationFlows.from(Sftp.inboundAdapter(sftpSessionFactory()))您可以将其视为完成的第一个任务。

Your problem from here that you don't make an integrationFlow , but rather return that IntegrationFlowBuilder and register it as a @Bean .您的问题是您没有创建integrationFlow ,而是返回该IntegrationFlowBuilder并将其注册为@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.您需要继续流定义并在最后调用它的get()以返回一个已经注册为 bean 的integrationFlow实例。 If this code flow is confusing a bit, consider to implement an IntegrationFlowAdapter as a @Component .如果此代码流有点混乱,请考虑将IntegrationFlowAdapter实现为@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.要触发批处理作业,您需要考虑在.transform() EIP 方法中使用FileMessageToJobRequest ,然后在JobLaunchingGateway .handle() EIP 方法中使用 JobLaunchingGateway。

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 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.顺便说一句,最后一个有一个完全适合您的用例的流程示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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