首页 > 解决方案 > 使用 Spring Integration 移入 GCP 存储后如何从本地目录中删除文件

问题描述

我有这段代码,它成功地将文件从本地目录移动到 GCP 存储桶,但我需要在移动到 GCP 后删除它们。

@Bean
@InboundChannelAdapter(channel = "new-file-channel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> streamingAdapter(Storage gcs) {
    FileReadingMessageSource sourceReader = new FileReadingMessageSource();
    sourceReader.setDirectory(new File(INPUT_DIR));
    sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
    return sourceReader;
}

@Bean
@ServiceActivator(inputChannel = "new-file-channel")
public MessageHandler outboundChannelAdapter(Storage gcs) {
    GcsMessageHandler outboundChannelAdapter = new GcsMessageHandler(new GcsSessionFactory(gcs));
    outboundChannelAdapter.setRemoteDirectoryExpression(new ValueExpression<>(this.gcsReadBucket));
    return outboundChannelAdapter;
}

标签: springspring-bootgoogle-cloud-platformspring-integrationintegration

解决方案


您需要添加到其中@ServiceActivator(inputChannel = "new-file-channel")一个adviceChain引用ExpressionEvaluatingRequestHandlerAdvice将执行onSuccessExpression删除该本地文件。

像这样的东西:

    @Bean
    public Advice removeFileAdvice() {
        ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
        advice.setOnSuccessExpressionString("payload.delete()");
        advice.setSuccessChannel(myHandlerSuccessChannel());
        return advice;
    }

它会起作用,因为您的有效负载是 ajava.io.File并且它具有该delete()方法。

比你像我开头说的那样配置:

@ServiceActivator(inputChannel = "new-file-channel", adviceChain="removeFileAdvice")

推荐阅读