我正在尝试使用ExpressionEvaluatingRequestHandlerAdvice删除已推送到远程服务器的源文件:
@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;
}
在下面的代码中:
@Bean
public IntegrationFlow integrationFlow() {
return IntegrationFlows.from(fileReader(), spec -> spec.poller(Pollers.fixedDelay(1000)))
.transform(transformer, "transform")
.handle(
Sftp.outboundAdapter(sftpSessionFactory, FileExistsMode.REPLACE)
.remoteDirectory(sftpRemoteDirectory),
c -> c.advice(expressionAdvice(c))
)
.get();
}
@Bean
public FileReadingMessageSource fileReader() {
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(new File(localSourceDirectory));
return source;
}
还有我的Transformer
班级:
@Component
public class Transformer {
public String transform(String filePath) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filePath)));
return "Transformed content: " + content;
}
}
但是,当我检查源目录时,该文件仍然存在。 我在这里想念什么?请帮助。
我正在使用Spring Integration 5.2.4。
谢谢!
这是基于@ArtemBilan的答案的工作代码:
@Bean
public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c) {
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
// advice.setOnSuccessExpressionString("payload.delete()");
advice.setOnSuccessExpressionString("headers[file_originalFile].delete()");
advice.setOnFailureExpressionString("payload + ' failed to upload'");
advice.setTrapException(true);
return advice;
}
答案 0 :(得分:1)
再一次:
public String transform(String filePath) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(filePath)));
return "Transformed content: " + content;
}
因此,您的.transform(transformer, "transform")
产生了String
而不是File
。 Sftp.outboundAdapter()
可以这样做,因为它可以将字符串转换为远程文件内容。但是advice.setOnSuccessExpressionString("payload.delete()");
对String
对象应该做什么?我相信您想删除一个文件,因此您需要完全具有File
对象,该建议才能正常工作。
FileReadingMessageSource
为我们填充了一个FileHeaders.ORIGINAL_FILE
标头。
因此,您可以将要删除的表达式更改为此:
headers[file_originalFile].delete()
你应该没事。