Spring Integration文件上传异常:clearThreadKey调用问题致部分文件未上传
问题根源
你的配置里,outsftpChannel是默认的点对点DirectChannel,而SFTP出站适配器和执行clearThreadKey的服务激活器同时订阅了这个通道。DirectChannel的消息分发逻辑是轮询分配给多个消费者,直接导致:
- 一半消息被SFTP适配器接收,完成上传流程
- 另一半消息被
clearThreadKey的服务激活器接收,直接清除线程Key但完全没执行上传
这正好对应你看到的“10个文件仅5个成功、clearThreadKey交替触发”的现象。
解决方案
调整配置逻辑,确保clearThreadKey只在SFTP上传成功后执行,具体修改如下:
修改后的integration.xml配置
<int-file:inbound-channel-adapter directory="myowndirectorypath" id="fileInbound" channel="sftpChannel"> <int:poller fixed-rate="1000" max-messages-per-poll="100"/> </int-file:inbound-channel-adapter> <int:channel id="sftpChannel"/> <int:service-activator input-channel="sftpChannel" output-channel="outsftpChannel" expression="@dsf.setThreadKey(#root, headers['file_name'])"/> <int:channel id="outsftpChannel"/> <int-sftp:outbound-channel-adapter id="sftpOutboundAdapter" session-factory="dsf" channel="outsftpChannel" charset="UTF-8" remote-directory-expression="@sftpConfig.determineRemoteDirectory(headers['file_name'])" <!-- 新增:上传成功后转发消息到该通道 --> send-success-channel="postUploadChannel"/> <!-- 新增上传成功专用通道 --> <int:channel id="postUploadChannel"/> <!-- 仅在上传成功后清除线程Key --> <int:service-activator input-channel="postUploadChannel" output-channel="nullChannel" expression="@dsf.clearThreadKey(#root, headers['file_name'])"/>
可选优化:处理上传失败场景
为了避免上传失败时ThreadLocal里的Key残留,可添加失败处理逻辑:
<!-- 新增上传失败专用通道 --> <int:channel id="uploadFailureChannel"/> <int-sftp:outbound-channel-adapter id="sftpOutboundAdapter" session-factory="dsf" channel="outsftpChannel" charset="UTF-8" remote-directory-expression="@sftpConfig.determineRemoteDirectory(headers['file_name'])" send-success-channel="postUploadChannel" <!-- 新增:上传失败后转发消息到该通道 --> send-failure-channel="uploadFailureChannel"/> <!-- 失败时也清除线程Key --> <int:service-activator input-channel="uploadFailureChannel" output-channel="nullChannel" expression="@dsf.clearThreadKey(#root, headers['file_name'])"/>
代码验证
确保你的threadKey是ThreadLocal类型,避免跨线程干扰:
private ThreadLocal<String> threadKey = new ThreadLocal<>();
内容的提问来源于stack exchange,提问作者Molay
相关产品推荐
相关产品推荐

