使用 ExpressionEvaluatingRequestHandlerAdvice 将负载推送到远程服务器后无法删除负载

Unable to delete payload after pushing it to remote server using ExpressionEvaluatingRequestHandlerAdvice

我正在尝试使用 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;
    }

还有我的Transformerclass:


@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 集成 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;
    }

再来一次:

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 object 做什么?我相信您想删除一个文件,所以您需要 File object 才能使该建议正常工作。

FileReadingMessageSource 为我们填充了 FileHeaders.ORIGINAL_FILE header。 因此,您可以将要删除的表达式更改为:

headers[file_originalFile].delete()

你应该没问题。