Moving processed files to another directory using

2019-02-27 23:22发布

问题:

I am trying to poll local directory using ftp inbound adapter to fetch files for further processing. I want to move the file to another local directory and delete it from origination. Not getting a way to achieve it. Here's what I have so far:

<int-ftp:inbound-channel-adapter id="ftpInbound"
    channel="ftpChannel" session-factory="ftpClientFactory"
    filename-pattern="*.xml" auto-create-local-directory="false"
    delete-remote-files="false" remote-directory="/" local-directory="//C://FBS//testmq">
    <int:poller fixed-rate="20000" />
</int-ftp:inbound-channel-adapter>

<int:channel id="ftpChannel">
    <int:queue />
</int:channel>

回答1:

Use transaction synchronization with a pseudo transaction manager; see the file example in the documentation. Here's the configuration from that section of the docs:

<int-file:inbound-channel-adapter id="inputDirPoller"
    channel="someChannel"
    directory="/foo/bar"
    filter="filter"
    comparator="testComparator">
    <int:poller fixed-rate="5000">
        <int:transactional transaction-manager="transactionManager" synchronization-factory="syncFactory" />
    </int:poller>
</int-file:inbound-channel-adapter>

<int:transaction-synchronization-factory id="syncFactory">
    <int:after-commit expression="payload.renameTo(new java.io.File('/success/' + payload.name))" 
           channel="committedChannel" />
    <int:after-rollback expression="payload.renameTo(new java.io.File('/failed/' + payload.name))"
           channel="rolledBackChannel" />
</int:transaction-synchronization-factory>

Continue reading into the next section...

Referring to the above section, you may be thinking it would be useful to take these 'success' or 'failure' actions when a flow completes, even if there is no 'real' transactional resources (such as JDBC) downstream of the poller. For example, consider a followed by an ftp:outbout-channel-adapter/. Neither of these components is transactional but we might want to move the input file to different directories, based on the success or failure of the ftp transfer.

To provide this functionality, the framework provides a PseudoTransactionManager, enabling the above configuration even when there is no real transactional resource involved. If the flow completes normally, the beforeCommit and afterCommit synchronizations will be called, on failure the afterRollback will be called. Of course, because it is not a real transaction there will be no actual commit or rollback. The pseudo transaction is simply a vehicle used to enable the synchronization features.