spring integration sftp:如何在下载完成后手动触发下载操作并执行其他操作?

时间:2017-10-12 08:19:46

标签: spring-boot spring-integration spring-batch

我在我的项目中使用spring batch + spring integration sftp。 我不希望在应用启动时触发下载。我希望在步骤1中触发下载过程并在步骤2之后将所有文件下载到本地,不确定如何实现

1 个答案:

答案 0 :(得分:1)

您需要<int-sftp:outbound-gateway>使用MGET命令:

  

mget操作产生的消息有效负载是List<File>对象 - 一个File对象列表,每个对象代表一个检索到的文件。

     

远程目录在file_remoteDirectory标题中提供,文件名的模式在file_remoteFile标题中提供。

https://docs.spring.io/spring-integration/docs/4.3.12.RELEASE/reference/html/sftp.html#sftp-outbound-gateway

在Java DSL中,它看起来像:

.handle(Sftp.outboundGateway(sessionFactory(), AbstractRemoteFileOutboundGateway.Command.MGET,
                        "payload")
                        .options(AbstractRemoteFileOutboundGateway.Option.RECURSIVE)
                        .regexFileNameFilter("(subSftpSource|.*1.txt)")
                        .localDirectoryExpression("'" + getTargetLocalDirectoryName() + "' + #remoteDirectory")
                        .localFilenameExpression("#remoteFileName.replaceFirst('sftpSource', 'localTarget')"))

其中payload是远程目录评估的SpEL表达式。在这种情况下,它实际上是请求消息的有效负载:

String dir = "sftpSource/";
registration.getInputChannel().send(new GenericMessage<>(dir + "*"));

如果远程目录是静态的,并且未从批处理中更改,则可以在XML定义中将其用作LiteralExpression - expresion="'myRemoteDir'"

由于此MGET命令的结果是List<File>,您应该考虑使用Splitter作为下一步。