0
votes

I have configured SFTP inbound channel adapter for polling files from SFTP server to local directory in regular interval. I need to set the SFTP inbound channel adapter for 16, such SFTP folder location. How can I achieve it using single adapter and channel.

****Here is my configuration.****

**<?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>

Updated Config

<?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>

2nd Update

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

1 Answers

0
votes

Consider to use <int-sftp:outbound-gateway> with MGET command and payload as a pattern to retrive appropriate files from all desired remote directories.

That pattern can be provided from the generic 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"/>

See the documentation on the matter: http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/sftp.html#sftp-outbound-gateway