如何删除Spring集成的ftp inbound-channel-adapter中的本地文件

问题描述 投票:0回答:3

我有一个集成案例,从 FTP 获取 xml 有效负载,然后使用 http 出站通道将有效负载发送到 Web 服务,ftp 入站通道适配器有一个名为 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
3个回答
1
投票

向出站网关添加

ExpressionEvaluatingRequestHandlerAdvice
以删除文件。有关示例,请参阅重试和更多示例中的表达式评估建议演示 - 它根据成功或失败删除或重命名文件。


1
投票

感谢Gray的建议,这是我更正后的配置

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

0
投票

创建ExpressionEvaluatingRequestHandlerAdvice的bean并将setOnSuccessExpression设置为payload.delete

  @Bean
  public ExpressionEvaluatingRequestHandlerAdvice deleteLocalFileAdvice() {
    ExpressionEvaluatingRequestHandlerAdvice handlerAdvice =
        new ExpressionEvaluatingRequestHandlerAdvice();
    SpelExpressionParser spelParser = new SpelExpressionParser();
    handlerAdvice.setOnSuccessExpression(spelParser.parseExpression("payload.delete"));
    return handlerAdvice;
  }

并在MessageHandler的adviceChain中提供bean

  @Bean
  @ServiceActivator(inputChannel = "ftpFileDownloadInboundChannel", adviceChain = 
  "deleteLocalFileAdvice")
  public MessageHandler ftpInboundMessageHandler() {
    //process indbound file
  }

并向您的@InboundChannelAdapter提供通道

  @Bean
  @InboundChannelAdapter(channel = "ftpFileDownloadInboundChannel",
      poller = @Poller(fixedDelay = "3000"))
  public MessageSource<File> ftpMessageSource() {
    // MessageSource config..
  }
© www.soinside.com 2019 - 2024. All rights reserved.