Skip to content

Commit 5848136

Browse files
committed
spring-projectsGH-8659: Fix the WatchServiceDirectoryScanner to detect new file after renaming the sub-folder of the watched folder (Java 7 WatchService)
1 parent 13980af commit 5848136

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/FileReadingMessageSource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ private void processFilesFromNormalEvent(Set<File> files, File parentDir, WatchE
533533
logger.debug(() -> "Watch event [" + event.kind() + "] for file [" + file + "]");
534534

535535
if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())) {
536+
if (this.pathKeys.containsKey(file.toPath())) {
537+
WatchKey watchKey = this.pathKeys.remove(file.toPath());
538+
watchKey.cancel();
539+
}
536540
if (getFilter() instanceof ResettableFileListFilter<File> resettableFileListFilter) {
537541
resettableFileListFilter.remove(file);
538542
}
@@ -554,7 +558,7 @@ private void processFilesFromNormalEvent(Set<File> files, File parentDir, WatchE
554558
}
555559
else {
556560
logger.debug(() -> "A file [" + file + "] for the event [" + event.kind() +
557-
"] doesn't exist. Ignored.");
561+
"] doesn't exist. Ignored. Maybe DELETE event is not watched ?");
558562
}
559563
}
560564
}

spring-integration-file/src/test/java/org/springframework/integration/file/WatchServiceDirectoryScannerTests.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ public boolean remove(File fileToRemove) {
130130
File top2 = File.createTempFile("tmp", null, this.rootDir);
131131
File foo2 = File.createTempFile("foo", ".txt", this.foo);
132132
File bar2 = File.createTempFile("bar", ".txt", this.bar);
133-
File baz = new File(this.foo, "baz");
134-
baz.mkdir();
135-
File baz1 = File.createTempFile("baz", ".txt", baz);
133+
File bazA = new File(this.foo, "bazA");
134+
bazA.mkdir();
135+
File baz1 = File.createTempFile("baz", ".txt", bazA);
136136
files = scanner.listFiles(this.rootDir);
137137
int n = 0;
138138
Set<File> accum = new HashSet<>(files);
@@ -170,7 +170,7 @@ public boolean remove(File fileToRemove) {
170170

171171
assertThat(accum).containsAll(filesForOverflow);
172172

173-
File baz2 = File.createTempFile("baz2", ".txt", baz);
173+
File baz2 = File.createTempFile("baz2", ".txt", bazA);
174174

175175
n = 0;
176176
while (n++ < 300 && accum.size() < 605) {
@@ -207,7 +207,19 @@ public boolean remove(File fileToRemove) {
207207

208208
assertThat(removeFileLatch.await(10, TimeUnit.SECONDS)).isTrue();
209209

210-
File baz3 = File.createTempFile("baz3", ".txt", baz);
210+
File bazB = new File(this.foo, "bazB");
211+
bazA.renameTo(bazB);
212+
File baz3 = File.createTempFile("baz3", ".txt", bazB);
213+
214+
while (n++ < 300 && files.size() < 1) {
215+
Thread.sleep(100);
216+
files = scanner.listFiles(this.rootDir);
217+
accum.addAll(files);
218+
}
219+
220+
assertThat(files).hasSize(1).contains(baz3);
221+
222+
File baz4 = File.createTempFile("baz4", ".txt", bazB);
211223

212224
n = 0;
213225
Message<File> fileMessage = null;
@@ -216,7 +228,7 @@ public boolean remove(File fileToRemove) {
216228
}
217229

218230
assertThat(fileMessage).isNotNull();
219-
assertThat(fileMessage.getPayload()).isEqualTo(baz3);
231+
assertThat(fileMessage.getPayload()).isEqualTo(baz4);
220232
assertThat(fileMessage.getHeaders().get(FileHeaders.RELATIVE_PATH, String.class))
221233
.startsWith(TestUtils.applySystemFileSeparator("foo/baz/"));
222234

0 commit comments

Comments
 (0)