gpt4 book ai didi

java - WatchEventType.DELETE 似乎不起作用

转载 作者:行者123 更新时间:2023-12-02 02:11:39 25 4
gpt4 key购买 nike

我想做的是跟踪删除的文件并应用某些逻辑(获取 ID 并更新实体)。我发现我们可以在 channel 适配器内传递监视事件列表,包括

FileReadingMessageSource.WatchEventType.DELETE

但是当我从文件夹中删除文件时,我没有看到任何事件被触发,并且变压器从未被应用

@Bean
public IntegrationFlow integrationFlow(FileToMovieTransformer fileToMovieTransformer) {

return this.integrationFlowBuilder()
.transform(fileToMovieTransformer)
.channel(movieHandlerChannel())
.get();

}

private IntegrationFlowBuilder integrationFlowBuilder() {

return IntegrationFlows.from(

Files.inboundAdapter(new File(localFilmFolder))
.autoCreateDirectory(true)
.useWatchService(true)
.watchEvents(FileReadingMessageSource.WatchEventType.CREATE, FileReadingMessageSource.WatchEventType.DELETE)
.patternFilter("*.xml"),

e -> e.poller(Pollers.fixedDelay(10, TimeUnit.SECONDS)
));
}

最佳答案

我想说你对待DELETE的方式是错误的:

/**
* Directory entry deleted.
*
* <p> When a directory is registered for this event then the {@link WatchKey}
* is queued when it is observed that an entry is deleted or renamed out of
* the directory. The event {@link WatchEvent#count count} for this event
* is always {@code 1}.
*/
public static final WatchEvent.Kind<Path> ENTRY_DELETE =
new StdWatchEventKind<Path>("ENTRY_DELETE", Path.class);

因此,已经没有任何内容可以作为消息发送到下游。我们在这里肯定谈论的是FileReadingMessageSource。但是使用DELETE就没有什么可读的了。我错过了什么吗?

这是迄今为止我们在文档中的内容:

The ENTRY_DELETE events have effect for the ResettableFileListFilter implementations and, therefore, their files are provided for the remove() operation. This means that (when this event is enabled), filters such as the AcceptOnceFileListFilter will have the file removed, meaning that, if a file with the same name appears, it will pass the filter and be sent as a message.

因此,要在发生 DELETE 事件时实现您想要执行的操作,您需要实现 ResettableFileListFilter 并与 SimplePatternFileListFilter 一起实现将它们合成到CompositeFileListFilter中。

当文件被删除时,会发出 DELETE 事件,我们最终得到如下逻辑:

if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
if (getFilter() instanceof ResettableFileListFilter) {
((ResettableFileListFilter<File>) getFilter()).remove(file);
}

其中提到的 CompositeFileListFilter 肯定实现了这个 ResettableFileListFilter 并将委托(delegate)给您自己的实现。

关于java - WatchEventType.DELETE 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com