Spring Integration:如何使用IntegrationFlow在sftp上动态创建子目录

时间:2018-10-18 16:26:00

标签: spring-integration spring-integration-dsl spring-integration-sftp

我有一个用例,用于在某些动态创建的子目录下将文件传输到sftp。 我使用自定义SftpMessageHandler方法和网关来工作。但是这种方法的问题在于,成功上传后,它并没有删除本地临时文件。 为了解决这个问题,现在我将IntegrationFlow与表达式Advice(如下所示)一起使用,这确实删除了本地文件,但是我不知道如何动态创建远程subDirs。我读过有关远程目录表达的信息,但不确定如何使用/实现它。

有人解决了这个问题吗?任何帮助表示赞赏!

@Bean
public IntegrationFlow sftpOutboundFlow() {

    return IntegrationFlows.from("toSftpChannel")
              .handle(Sftp.outboundAdapter(this.sftpSessionFactory())
                      .remoteFileSeparator("/")
                      .useTemporaryFileName(false)
                      .remoteDirectory("/temp"), c -> c.advice(expressionAdvice(c)))
                                     .get();
}



@Bean
public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c) {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setOnSuccessExpressionString("payload.delete()");
    advice.setOnFailureExpressionString("payload + ' failed to upload'");
    advice.setTrapException(true);
    return advice;
}

@MessagingGateway
public interface UploadGateway {
    @Gateway(requestChannel = "toSftpChannel")
    void upload(File file);
}

1 个答案:

答案 0 :(得分:2)

Sftp.outboundAdapter()的远程目录具有以下选项:

/**
 * Specify a remote directory path.
 * @param remoteDirectory the remote directory path.
 * @return the current Spec
 */
public S remoteDirectory(String remoteDirectory) {
}

/**
 * Specify a remote directory path SpEL expression.
 * @param remoteDirectoryExpression the remote directory expression
 * @return the current Spec
 */
public S remoteDirectoryExpression(String remoteDirectoryExpression) {
}

/**
 * Specify a remote directory path {@link Function}.
 * @param remoteDirectoryFunction the remote directory {@link Function}
 * @param <P> the expected payload type.
 * @return the current Spec
 */
public <P> S remoteDirectory(Function<Message<P>, String> remoteDirectoryFunction) {
}

因此,如果故事是关于 dynamic 子目录的,则可以选择remoteDirectoryExpressionremoteDirectory(Function)并针对其中的message或某些bean计算目标路径。应用程序上下文。

例如:

.remoteDirectoryExpression("'rootDir/' + headers.subDir")

还要记住,对于不存在的目录,您也需要配置.autoCreateDirectory(true)