Spring-Integration:使用通用根目录创建多目录sftp目录失败

时间:2019-04-30 10:08:32

标签: spring-integration spring-integration-dsl

我有多个线程,它们通过Sftp.outboundAdapter上传sftp文件。每个线程都需要创建一个具有公共根目录路径。我得到一个异常,告诉我公用根存在:

org.springframework.messaging.MessageDeliveryException: Failed to transfer file
...
Caused by: org.springframework.core.NestedIOException: failed to create remote directory '/my/import/de3fb731-6a56-11e9-bfd2-0242ac140022'.; nested exception is 4: Eine Datei kann nicht erstellt werden, wenn sie bereits vorhanden ist. 
    at org.springframework.integration.sftp.session.SftpSession.mkdir(SftpSession.java:232)
    at org.springframework.integration.file.remote.RemoteFileUtils.makeDirectories(RemoteFileUtils.java:71)
    at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:545)
    at org.springframework.integration.file.remote.RemoteFileTemplate.doSend(RemoteFileTemplate.java:337)
    ... 150 common frames omitted
Caused by: com.jcraft.jsch.SftpException: Eine Datei kann nicht erstellt werden, wenn sie bereits vorhanden ist. 
    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
    at com.jcraft.jsch.ChannelSftp.mkdir(ChannelSftp.java:2182)
    at org.springframework.integration.sftp.session.SftpSession.mkdir(SftpSession.java:229)
    ... 153 common frames omitted

我猜在RemoteFileUtils#makeDirectories中确定pathsToCreate时存在竞争状态。这两个线程都确定需要创建路径段,但是当两个都尝试创建时,第二个失败。

一个可能的解决方法是提前创建公用路径,但这很笨拙,因为Sftp.outboundGateway中没有这样的命令,看来我必须用autoCreateDirectories上传一个虚拟文件并删除然后是虚拟文件。

是否有更好的方法来解决此问题?将mkdirs命令添加到sftp出站网关是否有意义?当autoCreateDirectories为true时,是否应该有一个标志来忽略由于现有目录引起的错误?

1 个答案:

答案 0 :(得分:1)

  

可能的解决方法是提前创建公用路径,但这很笨拙,因为Sftp.outboundGateway中没有这样的命令

您可以在初始化期间自行调用RemoteFileUtils.makeDirectories(通过SftpRemoteFileTemplate.execute(session -> ...)获得会话)。

但是,我同意该实用程序应该捕获并忽略该异常。请打开issue on GitHub

欢迎捐款。