diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractPersistentAcceptOnceFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractPersistentAcceptOnceFileListFilter.java index 2fa5c7c6bf..1a8f91775a 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractPersistentAcceptOnceFileListFilter.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractPersistentAcceptOnceFileListFilter.java @@ -77,17 +77,27 @@ public abstract class AbstractPersistentAcceptOnceFileListFilter extends Abst String oldValue = this.store.putIfAbsent(key, newValue); if (oldValue == null) { // not in store flushIfNeeded(); - return true; + return fileStillExists(file); } // same value in store if (!isEqual(file, oldValue) && this.store.replace(key, oldValue, newValue)) { flushIfNeeded(); - return true; + return fileStillExists(file); } return false; } } + /** + * Check if the file still exists; default implementation returns true. + * @param file the file. + * @return true if the filter should return true. + * @since 4.3.19 + */ + protected boolean fileStillExists(F file) { + return true; + } + /** * {@inheritDoc} * @since 4.0.4 diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/FileSystemPersistentAcceptOnceFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/FileSystemPersistentAcceptOnceFileListFilter.java index e0b68ca6cb..c8a1eb4cd8 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/FileSystemPersistentAcceptOnceFileListFilter.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/FileSystemPersistentAcceptOnceFileListFilter.java @@ -42,4 +42,15 @@ public class FileSystemPersistentAcceptOnceFileListFilter extends AbstractPersis return file.getAbsolutePath(); } + /** + * Check that the file still exists, to avoid a race condition when multi-threaded and + * another thread removed the file while we were waiting for the lock. + * @since 4.3.19 + */ + @Override + protected boolean fileStillExists(File file) { + return file.exists(); + } + + } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/filters/PersistentAcceptOnceFileListFilterTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/PersistentAcceptOnceFileListFilterTests.java index ccc1f18a2c..83bc9e5caf 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/filters/PersistentAcceptOnceFileListFilterTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/filters/PersistentAcceptOnceFileListFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,6 +126,11 @@ public class PersistentAcceptOnceFileListFilterTests extends AcceptOnceFileListF new SimpleMetadataStore(), "rollback:"); File[] files = new File[] {new File("foo"), new File("bar"), new File("baz")}; List passed = filter.filterFiles(files); + assertEquals(0, passed.size()); + for (File file : files) { + file.createNewFile(); + } + passed = filter.filterFiles(files); assertTrue(Arrays.equals(files, passed.toArray())); List now = filter.filterFiles(files); assertEquals(0, now.size()); @@ -137,6 +142,9 @@ public class PersistentAcceptOnceFileListFilterTests extends AcceptOnceFileListF now = filter.filterFiles(files); assertEquals(0, now.size()); filter.close(); + for (File file : files) { + file.delete(); + } } @Test