2015-01-19 3 views
0

Я настроил адаптер входящего канала SFTP для регулярного опроса файлов с сервера SFTP в локальный каталог. Мне нужно установить адаптер входящего канала SFTP для 16, такое местоположение папки SFTP. Как я могу достичь этого, используя один адаптер и канал.Конфигурация входящего адаптера 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> 

Обновлено 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> 

второе обновление

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.j AVA

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; 
    } 


} 

ответ

0

Рассмотрим использование <int-sftp:outbound-gateway> с MGET команды и payload как pattern в retrive соответствующие файлы из всех требуемых удаленных каталогов.

Эта модель может быть получен из родового 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

+0

Здравствуйте, Артем, я попытался с помощью данной конфигурации кажется, что resultChannel декларация отсутствует. Добавлено это в моей конфигурации. Но, не повезло. Даже если ssh-соединение выполнено успешно, файл не передается в локальный каталог, и структура папок не реплицируется. Обновленная конфигурация добавлена ​​выше. –

+0

Ну, это уже ваша задача построить правильный SFTP 'pattern' –

+0

Можете ли вы дать некоторую ссылку на шаблон SFTP для реализации здесь в моей конфигурации –