spring - 使用 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;
}
解决方案
您需要添加到其中@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")
推荐阅读
- postgresql - 如何让spring用AUTO_INCREMENT创建一个列?
- python - 仅在 docker 环境中出现 Python 错误(名称或服务未知)
- sql - 查找不在另一列中的 ID
- c# - 如何修复 NHibernate.Cfg.HibernateConfigException
- java - 无效的初始堆大小 -Xms4096M
- android - 我不明白我的规则有什么问题
- twig - 树枝中的背景媒体图像
- javascript - 在滚动时将 div 粘贴到顶部并稍后取消粘贴
- dictionary - 在 Vuex 状态中使用 Map
- sql - 如何使用一个条件从 T2 更新 T1 的列值?