如何从spring批处理调用sftp:outbound-gateway?

时间:2014-09-12 09:10:31

标签: spring-batch spring-integration

我想从批处理tasklet调用sftp:outbound-gateway,以便从sftp服务器下载文件。 我已经看过与此主题相关的其他帖子,但我不确定我做错了什么。任何人都可以根据我的配置给我一个提示吗?我的批处理工作所以问题只是在批处理步骤中调用sftp组件。我已经用注释标记了Spring Integration部分,因此更容易阅读相关配置。

我可以在我的日志中看到:DEBUG [o.s.i.e.SourcePollingChannelAdapter]在投票期间没有收到任何消息,返回' false'。所以我没有收到文件,但为什么?

提前感谢您花时间进行分析!

<bean id="ftsSftpClientFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="${my.import.sftp.localhost}"/>
    <property name="user" value="${my.import.sftp.username}"/>
    <property name="password" value="${my.import.sftp.passwort}"/>
</bean>

  <!-- Start: Spring Integration -->

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

    <int:channel id="requestChannel" />

    <int-sftp:outbound-gateway id="sftpGateway"
                               session-factory="ftsSftpClientFactory"
                               request-channel="requestChannel"
                               reply-channel="replyChannel"
                               auto-startup="true"
                               command="get"
                               command-options="-P"
                               expression="payload"
                               remote-directory="."
                               local-directory="${my.import.sftp.copy.file.destinationpath}">
    </int-sftp:outbound-gateway>

<bean name="copyFileTasklet" class="com.mydomain.CopyFileTasklet">
    <property name="channel" ref="replyChannel" />
    <property name="pollableChannel" ref="requestChannel" />
</bean>
   
<!-- Start: Spring Batch -->
<bean name="myImportTask" class="com.mydomain.MyImportTask">
    <property name="job" ref="unternehmungImportJob"/>
    <property name="jobLauncher" ref="jobLauncher"/>
</bean>

<bean id="jobDetail"
      class="com.mydomain.MyImportJob">
    <property name="myImportTask" ref="myImportTask" />
</bean>

<!--suppress SpringBatchModel -->
<batch:job id="myImportJob">
    <batch:step id="copy-file-step" next="my-import-step">
        <batch:tasklet ref="copyFileTasklet"/>
    </batch:step>
    <batch:step id="my-import-step">
        <batch:tasklet>
            <batch:chunk reader="myItemReader"
                         writer="myItemWriter"
                         commit-interval="10000">
                <!--
                skip-limit="10000"
                <batch:skippable-exception-classes>
                   <batch:include class="java.lang.Exception"/>
                   <batch:exclude class="java.io.FileNotFoundException"/>
                </batch:skippable-exception-classes> -->
            </batch:chunk>
            <batch:transaction-attributes isolation="DEFAULT" propagation="REQUIRED"/>
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="myItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="linesToSkip" value="1"/>
    <property name="encoding" value="${my.import.batch.encoding}" />
    <property name="resource" value="${my.import.batch.input.resource}"/>
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer" ref="lineTokenizer"/>
            <property name="fieldSetMapper">
                <bean class="com.mydomain.MyImportMapper"/>
            </property>
        </bean>
    </property>
</bean>

<bean id="myItemWriter" class="com.mydomain.MyItemWriter">
    <property name="myApplicationService" ref="defaultmyApplicationService" />
</bean>

<bean id="lineTokenizer" class="com.mydomain.DelimitedLineTokenizerWithEOF">
    <property name="delimiter" value="${my.import.batch.delimiter}" />
    <property name="eofMarker" value="${my.import.batch.eof.marker}" />
</bean>

public class CopyFileTasklet实现Tasklet {

private MessageChannel requestChannel;

private PollableChannel replyChannel;

public void setRequestChannel(MessageChannel requestChannel) {
    this.requestChannel = requestChannel;
}

public void setReplyChannel(PollableChannel replyChannel) {
    this.replyChannel = replyChannel;
}

@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

    Message<?> result = replyChannel.receive(10000);
    Object file = result.getPayload();       
    return RepeatStatus.FINISHED;
}

}

1 个答案:

答案 0 :(得分:1)

您未从自定义Tasklet启动整合流程的问题。当然,如果您之前没有发送过请求,则无法从replyChannel收到任何内容。

如果您只需要处理Integration Flow并从中获取结果,那么最好使用<gateway>中的POJI Tasklet

public interface SftpGateway {

   File download(String fileName);

}

<gateway id="sftpGateway" service-interface="com.my.proj.SftpGateway"
    default-request-channel="requestChannel"/>


<bean name="copyFileTasklet" class="com.mydomain.CopyFileTasklet">
    <property name="sftpGateway" ref="sftpGateway" />
</bean>

类似的东西。