使用单个适配器和单个通道的多个远程目录的SFTP入站适配器配置

时间:2015-01-19 10:16:14

标签: spring-integration

我已经配置了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;
    }


}

1 个答案:

答案 0 :(得分:0)

考虑将<int-sftp:outbound-gateway>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