Neewbie
Neewbie

Reputation: 69

Auto create not existing directory Spring Integration SFTP

Currently i use this configuration for Spring Integration SFTP:

@Bean
  @ServiceActivator(inputChannel = "toSftpChannel")
  public MessageHandler handler() {
    final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpressionString("\'" + properties.getFolder() + "\'"
        + ".concat(headers['region'])");
    handler.setAutoCreateDirectory(true);
    return handler;
  }

And all works, but sometimes i get an exception

MessageDeliveryException: Failed to transfer file [/test/home-dir/feed.xml -> feed.xml] from local directory to remote directory.; nested exception is org.springframework.core.NestedIOException: failed to create remote directory '/test-sftp'.; nested exception is 4: 
    at org.springframework.integration.file.remote.RemoteFileTemplate.doSend(RemoteFileTemplate.java:347)
    at org.springframework.integration.file.remote.RemoteFileTemplate.lambda$send$0(RemoteFileTemplate.java:298)
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:437)
    at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:298)
    at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:286)
    at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:278)
    at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:205)

The main problem, that i already have directory /test-sftp, and strange thing, that in properties.getFolder() (application.properties) i set /test-sftp/dir/, and but get an exception about half of the path. How it is possible? My goal is exclude this part of path from creation. Is it possible to create only dir from headers['region']. Example. I have /test-sftp/dir/ and need only create /test-sftp/dir/one /test-sftp/dir/two. But if dir from /test-sftp/dir/ not exists, then just fail.

P.S. I have all accesses and fail to upload file to SFTP only 5 from 100 times (all directories exists).

Upvotes: 0

Views: 2257

Answers (2)

Neewbie
Neewbie

Reputation: 69

Solved. We used customers SFTP which have many firewalls. We have Caused By: Pipe Closed, so, seems like sftp close long running open sessions and spring integration sftp do not realize it fast. So, the solution was to delete CachingSessionFactory for the time. Now all works without any issues.

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174709

You need to show the entire stack trace; don't truncate stack traces here.

but get an exception about half of the path.

SFTP doesn't support creating a directory tree in one step; we have to create them recursively. See RemoteFileUtils.makeDirectories().

Upvotes: 1

Related Questions