2
votes

I have a condition where I should read the file from SFTP remote location with the following steps:

  1. Read file from SFTP location say input folder.

  2. Process file and call a REST API with the content of file.

  3. If the call is successful move the remote SFTP file to archive folder else move the remote SFTP file to error folder.

I am thinking of two approaches. I don't know which are possible.

First, read the file from SFTP remote location to local and delete it from remote. Then call the REST API. According to response of REST call, success or error upload the file to remote folder.

Second, read the file from SFTP remote location to local. Then call the REST API. According to response of REST call, success or error upload the file to remote folder.

Can anyone enlighten me which of the approach is possible and convenient? I would appreciate if you can mention the channel adapters as well.

So far I am able to call REST API.

<int-sftp:inbound-channel-adapter id="sftpInbondAdapter"
        auto-startup="true" 
        channel="receiveChannel" 
        session-factory="sftpSessionFactory"
        local-directory="${local.dir}" 
        remote-directory="${sftp.dir.input}"
        auto-create-local-directory="true" 
        delete-remote-files="true"
        filename-pattern="*.txt">
        <int:poller fixed-rate="60000" max-messages-per-poll="1" />
    </int-sftp:inbound-channel-adapter>
    <int:channel id="receiveChannel"/>

    <int:splitter input-channel="receiveChannel" output-channel="singleFile"/>
    <int:channel id="singleFile"/>

    <int:service-activator input-channel="singleFile"
        ref="sftpFileListenerImpl" method="processMessage" output-channel="costUpdate" />
    <int:channel id="costUpdate" /> 

     <int:header-enricher input-channel="costUpdate" output-channel="headerEnriched">
         <int:header name="content-type" value="application/json" />
     </int:header-enricher>
     <int:channel id="headerEnriched" />

     <int-http:outbound-gateway
        url="${cost.center.add.rest.api}" request-channel="headerEnriched"
        http-method="POST" expected-response-type="java.lang.String" reply-channel="costAdded" >
    </int-http:outbound-gateway>
    <int:publish-subscribe-channel id="costAdded" />

I want to move the remote file to another location in remote folder once the API call is success after evaluating the API call response. My question is how do I move the remote file to another remote location based on the response of http:outbound-gateway?

1
Sorry, you scenario looks good. What issue are you facing? Deleting or not deleting remote file doesn't not make sence for you, since AcceptOnceFileListFilter is there for local files by default. - Artem Bilan
Actually, I want to move the remote file to another location in remote folder once the API call is success after evaluating the API call response. My question is how do I move the remote file to another remote location based on the response of http:outbound-gateway? - nebula
Now it is clear. You haven't specify the concrete question before... Is Gary's answer appropriate to you? - Artem Bilan
After http:outbound-gateway response how would I get the file from singleFile channel? And based on the file name how to move file in sftp location from one folder to another? - nebula
I edited my answer; please try to be more specific with your questions in future. - Gary Russell

1 Answers

1
votes

See the retry-and-more sample, specifically the Expression Evaluating Advice Demo - it shows how to take different actions based on the success/failure of an upload.

EDIT:

In response to your comments on Artem's answer; Since you have delete-remote-files="true" there's no remote file to move; you must first set that to false.

Then, use the advice as I suggested to process success or failure - use an ftp:outbound-gateway and mv the remote file. See the gateway documentation for the mv command.