2
votes

I have a file:inbound-channel-adapter that polls a directory for files and then sends it via SFTP to a server. After it's uploaded (which works fine), the original file needs to be deleted; how do I delete the original file after it's uploaded? In file:outbound-channel-adapter, there's a property I can set to autodelete the file.

<file:inbound-channel-adapter 
    id="incomingFiles"      
    channel="myFiles"       
    directory="file:/tmp/kots">
    <int:poller id="poller" fixed-delay="1000"/>
</file:inbound-channel-adapter>

<int:channel id="myFiles"/>

....

<sftp:outbound-channel-adapter 
    id="sftpOutboundAdapter"
    channel="myFiles"
    charset="UTF-8"     
    remote-directory="/tmp/testing"     
    session-factory="sftpSessionFactory"/>
1

1 Answers

5
votes

Transaction Synchronization is for you:

<file:inbound-channel-adapter 
    id="incomingFiles"      
    channel="myFiles"       
    directory="file:/tmp/kots">
    <int:poller id="poller" fixed-delay="1000">
         <int:transactional transaction-manager="transactionManager" synchronization-factory="syncFactory" />
    </int:poller>
</file:inbound-channel-adapter>


<int:transaction-synchronization-factory id="syncFactory">
    <int:after-commit expression="payload.delete()" channel="nullChannel"/>
</int:transaction-synchronization-factory>

Where transactionManager might be org.springframework.integration.transaction.PseudoTransactionManager from out of the box.