Skip to Content
Technical Articles

Dynamic Setting of Archive Directory for Post Processing in SFTP Sender Adapter

In this blog, how to move file to different locations after processing is finished is demonstrated. This blog also serves as answer to question: Move original file to another folder on SFTP server in case of exception.

The structure of this blog is as follows. First, requirements from the question are specified. Then, dummy scenario is created to fulfil requirements. Finally, solution is demonstrated.

Requirements

  • Files in ‘input’ directory must be processed
  • When processing is successful, the processed file must be moved to ‘output’ directory
  • When processing is failed, the processed file must be moved to ‘error’ directory

Scenario

XML files will be read from ‘input’ directory. XML files will contain single node ‘Payload’ having single sub-node called ‘Success’. If ‘Success’ is true, processing will be successful. If ‘Success’ is false, process will end with Error End.

Possible Payloads

Success

<Payload>
    <Success>true</Success>
</Payload>

Failure

<Payload>
    <Success>false</Success>
</Payload>

Solution

The solution uses Content Modifiers to set the property ‘archiveDirectory’. In successful scenario, ‘archiveDirectory’ is set to ‘/output’. In failure scenario, Exception Subprocess is used to catch the exception and Content Modifier is used therein to set ‘archiveDirectory’ to ‘/error’.

Flow Steps

Source for SFTP

Here, Source > Directory is set to ‘/input’ as per requirements. Please fill in Address and Credential Name as per your configurations.

Success? Router

The Success? Router routes based on Success property’s value. If ‘/Payload/Success’ is ‘true’ then ‘archiveDirectory’ property is set to ‘output’ otherwise flow ends with Error End.

Set Archive Directory to ‘output’

If successful, ‘archiveDirectory’ property is set to ‘output’.

Set Archive Directory to ‘error’

If failed, ‘archiveDirectory’ property is set to ‘error’.

Processing for SFTP

After processing is complete (whether succesfully or not), the file is moved to a location specified dynamically using ‘archiveDirectory’ property.

Parameter Value
Post-Processing Move File
Archive Directory /${property.archiveDirectory}/${file:name}

Execution

For testing this flow, Success.xml and Failure.xml files, containing payloads specified in Possible Payloads section above are put in ‘input’ directory.

After flow polls files, Success.xml file is put in ‘output’ directory and Failure.xml file is put in ‘error’ directory.

Alternate Scenario(s)

Error occurs when sending the file to receiver SFTP Server

In this scenario, file is sent to SFTP server. However, ‘Create Directories’ option on SFTP Receiver is disabled and the directory in which file must be put is not created on server to deliberately cause exception.

Integration Process

SFTP Receiver Target Configuration

Please fill in Address and Credential Name as per your configuration.

SFTP Receiver Processing Configuration

Note that ‘Create Directories’ option is disabled.

Example Run

1. Input

File ‘Non Existing Directory.xml’ is uploaded to ‘input’ directory. The file contents are same as Success under Possible Payloads above.

2. Success? Router

As ‘/Payload/Success’ is ‘true’, ‘Yes’ branch is followed.

3. Set Archive Directory to ‘output’

Exchange Property ‘archiveDirectory’ is set to ‘output’.

4. Send file to SFTPReceiver

File is sent to SFTPReceiver.

5. Error occurs because the directory ‘nonexisting’ does not exist

As ‘nonexisting’ directory does not exist, exception is thrown by SFTP and is caught in CPI as:

org.apache.camel.component.file.GenericFileOperationFailedException: Cannot change directory to: nonexisting, cause: 2: No such file or directory

6. Exception Subprocess is triggered

As exception occured, Exception Subprocess is triggered.

7. Set Archive Directory to ‘error’

Exchange Property ‘archiveDirectory’ is set to ‘/error’.

8. Exception Subprocess ends with Message End and triggers the Processing tab of SFTP Sender

In the processing tab, the configuration for Post Processing is to Move File to /${property.archiveDirectory}/${file:name}. As per these configurations, SFTP Sender Adapter moves the read file from ‘/input’ directory to ‘/error’ directory.

9. Output

Conclusion

As demonstrated, file can be moved to different locations when processing is finished using dynamic path. This is achieved when integration process ends with Message End i.e, message is completed either through success path or error path via Exception Subprocess, The exchange property ‘archiveDirectory’ was set at runtime and used to specify path where file should be moved dynamically.

3 Comments
You must be Logged on to comment or reply to a post.
  • Hi ,

    Consider a situation , where a file is picked from SFTP but due to some issue failed to deliver to Target in this case as per the above scenario the file will be moved to Error folder . But what about the original file from input , it still resides in the same folder . Any way to delete that original file ? So to avoid re-processing of the same file.This is the same situation which is described in that query you referred.

    Thanks,

    Manoj

    • Hi Manoj,

      Thank you for your comment.

      When file fails to be delivered to Target, the exception will be thrown by SFTP Server that will be caught by Exception Subprocess. Exception Subprocess will set the exchange property ‘archiveDirectory’ to ‘/error’. When Exception Subprocess finishes with Message End, Processing tab of SFTP Sender Adapter will be triggered. As per the configurations, the file will be “moved” from ‘/output’ directory to ‘/error’ directory.

      Section ‘Error occurs when sending the file to receiver SFTP Server’ under ‘Alternate Scenario(s)’ demonstrates the example run of the scenario you explained.

      Hope this helps,

      Bala

  • Thanks Bala 😊 for the clarification . I was under impression the Sender SFTP “Processing” takes places as soon as file is picked .But it’s nice to see that it gets processed after iflow execution .