From 0798c8df956844df59ebed0aa5c404143df7e6a8 Mon Sep 17 00:00:00 2001 From: pziobron <64628007+pziobron@users.noreply.github.com> Date: Thu, 29 Jun 2023 20:23:33 +0200 Subject: [PATCH] GH-8659: Fix WatchService to react for renames Fixes https://github.com/spring-projects/spring-integration/issues/8659 * GH-8659: Updating the documentation as requested * GH-8659: Fixes requested after coder review **Cherry-pick to `6.1.x`, `6.0.x` & `5.5.x`** --- .../integration/file/FileReadingMessageSource.java | 8 +++++++- src/reference/asciidoc/file.adoc | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/FileReadingMessageSource.java b/spring-integration-file/src/main/java/org/springframework/integration/file/FileReadingMessageSource.java index 13a519733b..07890600e7 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/FileReadingMessageSource.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/FileReadingMessageSource.java @@ -93,6 +93,7 @@ import org.springframework.util.Assert; * @author Gary Russell * @author Artem Bilan * @author Steven Pearce + * @author Patryk Ziobron */ public class FileReadingMessageSource extends AbstractMessageSource implements ManageableLifecycle { @@ -533,6 +534,11 @@ public class FileReadingMessageSource extends AbstractMessageSource implem logger.debug(() -> "Watch event [" + event.kind() + "] for file [" + file + "]"); if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())) { + Path filePath = file.toPath(); + if (this.pathKeys.containsKey(filePath)) { + WatchKey watchKey = this.pathKeys.remove(filePath); + watchKey.cancel(); + } if (getFilter() instanceof ResettableFileListFilter resettableFileListFilter) { resettableFileListFilter.remove(file); } @@ -554,7 +560,7 @@ public class FileReadingMessageSource extends AbstractMessageSource implem } else { logger.debug(() -> "A file [" + file + "] for the event [" + event.kind() + - "] doesn't exist. Ignored."); + "] doesn't exist. Ignored. Maybe DELETE event is not watched ?"); } } } diff --git a/src/reference/asciidoc/file.adoc b/src/reference/asciidoc/file.adoc index 83256a5d14..7bd135ec47 100644 --- a/src/reference/asciidoc/file.adoc +++ b/src/reference/asciidoc/file.adoc @@ -371,6 +371,11 @@ For this purpose, the `watch-events` property (`FileReadingMessageSource.setWatc With such an option, we can use one downstream flow logic for new files and use some other logic for modified files. The following example shows how to configure different logic for create and modify events in the same directory: +It is worth mentioning that the `ENTRY_DELETE` event is involved in the rename operation of sub-directory of the watched directory. +More specifically, `ENTRY_DELETE` event, which is related to the previous directory name, precedes `ENTRY_CREATE` event which notifies about the new (renamed) directory. +On some operating systems (like Windows), the `ENTRY_DELETE` event has to be registered to deal with that situation. +Otherwise, renaming watched sub-directory in the File Explorer could result in the new files not being detected in that sub-directory. + ==== [source,xml] ----