0
votes

I have a integration case that get xml payload from FTP then use http outbound channel to sent the payload to webservice, ftp inbound-channel-adapter has a mandatory attribute named local-directory, remote ftp files will be downloaded here, however when I restart, seems all files in local-directory will be handled again, may I know how to avoid this? One possible way is to delete local file in ftp inbound-channel-adapter, how to do it, can you advise?

thanks

My Spring integration configurations

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

2 Answers

1
votes

Add an ExpressionEvaluatingRequestHandlerAdvice to the outbound gateway to remove the file. See the Expression Evaluating Advice Demo in the retry-and-more sample for an example - it removes or renames the file, depending on success or failure.

1
votes

Thanks Gray's advise, here is my corrected configuration

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