在事务中创建重试延迟以清理 sftp 入站通道文件

Creating a retry delay within a transaction to clean up sftp inbound channel files

我有一个 sftp:inbound-channel-adapter 可以将文件从远程文件夹同步到本地文件夹。当它发现一个扩展名为 .xml 的文件时,它会创建一个 Message<File> 和一个新事务并将其发送到 routerChannel 通道。

approverRouter 查看 xml 内部并确定进程是否可以继续。如果一切正常,则消息将发送到 "okChannel" 并关闭到数据库进行保存。但是,如果情况不正常,则消息将发送到 int:delayer 10 秒,然后重新提交到 approverRouter

如果交易成功或失败,sftpCommittedChannelsftpRolledBackChannel 只是将下载的文件移动到 completed/failed 文件夹。

这里有一些xml来描述这个:

<int-sftp:inbound-channel-adapter id="..." channel="routerChannel" local-filter="localOnlyXmlFilter" ... >
    <int:poller fixed-rate="10000" max-messages-per-poll="-1">
        <int:transactional transaction-manager="transactionManager" synchronization-factory="syncFactory" />
    </int:poller>
</int-sftp:inbound-channel-adapter>

<!-- Takes Message<Map> makes sure all files exist and are valid MD5 checked -->
<int:router input-channel="routerChannel" ref="approverRouter" method="resolveRoute"/>

<bean id="approverRouter" class="com.example.app.readers.ApproverRouter">
    <property name="errorChannel" value="errorChannel"/>
    <property name="retryChannel" value="myRetry"/>
    <property name="okChannel" value="transformHashesToList"/>
    <property name="timeout" value="${timout}"/>
</bean>

<int:delayer id="delayer" input-channel="myRetry" output-channel="routerChannel" default-delay="10000"/>

<int:transaction-synchronization-factory id="syncFactory">
    <int:after-commit channel="sftpCommittedChannel"/>
    <int:after-rollback channel="sftpRolledBackChannel"/>
</int:transaction-synchronization-factory>

我的问题是,当消息发送到延迟器时,在轮询器中创建的事务成功完成。我认为这是因为当消息到达 int:delayer(通过 retryChannel)时,它不再在同一个原始线程中。

我想我需要的是一个同步延迟器,它会增加延迟但将消息保留在其原始事务中。感觉我设计的不对

普通人会怎么做才能产生重试延迟并在成功或失败时仍然整理 sftp 文件夹。

您可以添加无状态请求处理程序重试建议;可以根据退避设置暂停线程。

the documentation and spring-retry