【问题标题】:SFTP inbound adapter configuration for multiple remote directory using single adapter and single channel使用单个适配器和单通道的多个远程目录的 SFTP 入站适配器配置
【发布时间】:2015-01-19 10:16:14
【问题描述】:

我已配置 SFTP 入站通道适配器,用于定期从 SFTP 服务器轮询文件到本地目录。我需要将 SFTP 入站通道适配器设置为 16,例如 SFTP 文件夹位置。如何使用单个适配器和通道实现它。

****这是我的配置。****

**<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
        http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd 
         http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.1.xsd">



         <context:property-placeholder order="0"
        location="classpath:/sftpuser.properties" ignore-unresolvable="true"/>
         <context:property-placeholder order="1"
        location="classpath:/sftpfile.properties" ignore-unresolvable="true"/>


    <bean id="sftpSessionFactory"
        class="org.springframework.integration.file.remote.session.CachingSessionFactory">
        <constructor-arg ref="defaultSftpSessionFactory" />
    </bean>


    <bean id="defaultSftpSessionFactory" 

        class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">

        <property name="host" value="${sftp.host}" />
        <property name="port" value="${sftp.port}" /> 
    <!-- <property name="port" value="22" /> -->
        <property name="user" value="${sftp.username}" />
        <property name="privateKey" value="${private.keyfile}" />
        <property name="privateKeyPassphrase" value="${passphrase}" />
    </bean>

    <!-- Inbound channel adapter for SFTP call . with poll facility -->
    <int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
        auto-startup="true" channel="sftpReceiveChannel" session-factory="sftpSessionFactory"
        local-directory="${inboundlocaldir}" remote-directory="${inboundremotedir}"
        auto-create-local-directory="true" delete-remote-files="false"
        filename-pattern="*.txt" remote-file-separator="/">
     <int:poller fixed-rate="100000" max-messages-per-poll="10" /> 

    </int-sftp:inbound-channel-adapter>

    <int:channel id="sftpReceiveChannel">
        <int:queue />
    </int:channel>

</beans>

更新配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
        http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd 
         http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.1.xsd">

         <context:property-placeholder order="0"
        location="classpath:/sftpuser.properties" ignore-unresolvable="true"/>
         <context:property-placeholder order="1"
        location="classpath:/sftpfile.properties" ignore-unresolvable="true"/>


    <bean id="sftpSessionFactory"
        class="org.springframework.integration.file.remote.session.CachingSessionFactory">
        <constructor-arg ref="defaultSftpSessionFactory" />
    </bean>

    <bean id="defaultSftpSessionFactory" 

        class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">

        <property name="host" value="${sftp.host}" />
        <property name="port" value="${sftp.port}" /> 
        <property name="user" value="${sftp.username}" />
        <property name="privateKey" value="${private.keyfile}" />
        <property name="privateKeyPassphrase" value="${passphrase}" />
    </bean>

        <int:inbound-channel-adapter channel="sftpReceiveChannel"
    expression="'/**/*.txt'">
    <int:poller fixed-rate="1000" max-messages-per-poll="10" /> 
</int:inbound-channel-adapter>

<int-sftp:outbound-gateway session-factory="defaultSftpSessionFactory"
        request-channel="sftpReceiveChannel"
        command="mget"
        expression="payload"
        local-directory="${inboundlocaldir}"
        reply-channel="resultChannel"
         >
     <int:poller fixed-rate="1000" max-messages-per-poll="10" /> 
     </int-sftp:outbound-gateway>
    <int:channel id="sftpReceiveChannel">
        <int:queue />
    </int:channel>

    <int:channel id="resultChannel">
    </int:channel>


</beans>

第二次更新

Sftp-Inboud-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
        http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd 
         http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.1.xsd">

         <context:property-placeholder order="0"
        location="classpath:/sftpuser.properties" ignore-unresolvable="true"/>
         <context:property-placeholder order="1"
        location="classpath:/sftpfile.properties" ignore-unresolvable="true"/>


    <bean id="sftpSessionFactory"
        class="org.springframework.integration.file.remote.session.CachingSessionFactory">
        <constructor-arg ref="defaultSftpSessionFactory" />
    </bean>



    <bean id="defaultSftpSessionFactory" 

        class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">

        <property name="host" value="${sftp.host}" />
        <property name="port" value="${sftp.port}" /> 
        <property name="user" value="${sftp.username}" />
        <property name="privateKey" value="${private.keyfile}" />
        <property name="privateKeyPassphrase" value="${passphrase}" />
    </bean>

  <int:inbound-channel-adapter channel="inboundMGetRecursive"
    ref="sftpRemoteFolderParse" method="handle">
   <int:poller fixed-rate="5000"/>
</int:inbound-channel-adapter>
<bean id= "sftpRemoteFolderParse" class ="com.sftp.SftpRemoteFolderParse"/>

    <int:channel id="output">
    <int:queue />
</int:channel>

<int:channel id="inboundMGetRecursive" >
 <int:queue />
 </int:channel>

<int-sftp:outbound-gateway session-factory="defaultSftpSessionFactory"
    request-channel="inboundMGetRecursive" command="mget" expression="payload"
    command-options="-R" local-directory="C:/Users/Asit/Desktop/asit"
    remote-directory="/u01/IIP/iipuser/test"
    reply-channel="output">

     <int:poller fixed-rate="5000"/>
    </int-sftp:outbound-gateway>

</beans>

SftpRemoteFolderParser.java

package com.sftp;

import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;

public class SftpRemoteFolderParse {
    public Message<?> handle()
    {
        String dir = "/u01/IIP/iipuser/test";
        Message message =  new GenericMessage<Object>(dir + "*");
        System.out.println("the mesage is" +message);
        return message;
    }


}

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    考虑使用&lt;int-sftp:outbound-gateway&gt;MGET 命令和payload 作为pattern 从所有所需的远程目录检索适当的文件。

    可以从通用inbound-channel-adapter 提供该模式:

    <int:inbound-channel-adapter channel="sftpFilesChannel"
        expression="''">
        <int:poller fixed-rate="100000" max-messages-per-poll="10" /> 
    </int:inbound-channel-adapter>
    
    <int-sftp:outbound-gateway session-factory="ftpSessionFactory"
            request-channel="sftpFilesChannel"
            command="mget"
            command-options="-R"
            filename-regex="(subDir|.*\.txt)"
            local-directory="/tmp/out"
            reply-channel="resultChannel"/>
    

    请参阅有关此事的文档:http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/sftp.html#sftp-outbound-gateway

    【讨论】:

    • 嗨 Artem,我已经尝试过上述配置,似乎缺少 resultChannel 声明。在我的配置中添加了它。但是,没有运气。即使 ssh 连接成功,也没有文件传输到本地目录,也没有复制文件夹结构。上面添加了更新的配置。
    • 能否请您参考一下 SFTP 模式,以便在我的配置中实现
    • 我更新了我的答案以更清楚。请参阅新的配置变体和文档。
    • 我无法关联 的表达式,它将远程目录结构作为消息返回
    • 尝试了所讨论的配置,稍作修改。但似乎它正在从没有目录结构的 sftp 位置复制所有文件。配置在“第二次更新”标题中更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2022-11-16
    • 2018-07-06
    相关资源
    最近更新 更多