【问题标题】:how to delete local file in ftp inbound-channel-adapter of Spring integration如何在 Spring 集成的 ftp inbound-channel-adapter 中删除本地文件
【发布时间】:2018-01-31 20:23:00
【问题描述】:

我有一个集成案例,从 FTP 获取 xml 有效负载,然后使用 http 出站通道将有效负载发送到 webservice,ftp inbound-channel-adapter 有一个名为 local-directory 的强制属性,但是将在此处下载远程 ftp 文件当我重新启动时,似乎本地目录中的所有文件都会被再次处理,我可以知道如何避免这种情况吗? 一种可能的方法是在 ftp inbound-channel-adapter 中删除本地文件,怎么做,您能建议吗?

谢谢

我的 Spring 集成配置

<ftp:inbound-channel-adapter
        channel="requestChannel"
        session-factory="ftpClientSessionFactory"
        remote-directory="/outbound"
        local-directory="/temp"
        auto-create-local-directory="true"
        delete-remote-files="false"
        filename-pattern="*.xml"
        temporary-file-suffix=".writing">
    <int:poller fixed-delay="5000" max-messages-per-poll="10"/>
</ftp:inbound-channel-adapter>


<int:chain id="inboundChain" input-channel="requestChannel" output-channel="replyChannel">

    <int:transformer ref="xmlToJsonTransformer" />
    <int:transformer ref="jsonToMapTransformer" />
    <int:header-enricher>
        <int:header name="Content-Type" value="application/json" overwrite="true"/>
    </int:header-enricher>
    <http:outbound-gateway  expected-response-type="java.lang.String"
                           url="http://localhost:8080/postService/postupdate"
                           http-method="POST"
                           extract-request-payload="true"
                            request-factory="requestFactory">
    </http:outbound-gateway>
</int:chain>

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    ExpressionEvaluatingRequestHandlerAdvice 添加到出站网关以删除文件。有关示例,请参见 Expression Evaluating Advice Demo in the retry-and-more sample - 它会根据成功或失败删除或重命名文件。

    【讨论】:

    • 似乎在我将 ExpressionEvaluatingRequestHandlerAdvice 添加到我的 http:outbound-gateway 后它不起作用,你能分享更多细节吗?是因为我的出站网关是http吗?
    • 不要把配置放到cmets中;很难阅读。改为编辑问题。 "payload.delete()" - 有效载荷不再是文件;您需要在第一个转换器之前添加一个标头丰富器 - 将有效负载复制到标头 name="file_originalFile" expression="payload"。然后,根据您的建议,使用headers['file_originalFile'].delete()
    【解决方案2】:

    感谢格雷的建议,这是我更正的配置

    <int:chain id="inboundChain" input-channel="requestChannel" output-channel="replyChannel">
            <int:header-enricher>
                <int:header name="file_originalFile" expression="payload"/>
                <int:header name="file-name" expression="payload.name"/>
                <int:header name="file-failed-path" value="/project/ftp/failed/"/>
                <int:header name="Content-Type" value="application/json" overwrite="true"/>
            </int:header-enricher>
    
            <int:transformer ref="xmlToJsonTransformer" />
            <int:transformer ref="jsonToMapTransformer" />
    
            <http:outbound-gateway  expected-response-type="java.lang.String"
                                   url="http://localhost:8080/postService/postupdate"
                                   http-method="POST"
                                   extract-request-payload="true"
                                    request-factory="requestFactory">
                <http:request-handler-advice-chain>
                    <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
                        <property name="onSuccessExpression" value="headers['file_originalFile'].delete()" />
                        <property name="onFailureExpression"
                                  value="headers['file_originalFile'].renameTo(new java.io.File(headers['file-failed-path']+headers['file-name']))"/>
                    </bean>
                </http:request-handler-advice-chain>
            </http:outbound-gateway>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多