Clear lock cache in NioFileLocker (#2998)
Fixes https://github.com/spring-projects/spring-integration/issues/2980
This commit is contained in:
committed by
Artem Bilan
parent
084aaf7e57
commit
8bb0e194c0
@@ -34,7 +34,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
*/
|
||||
final class FileChannelCache {
|
||||
|
||||
private static ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<File, FileChannel>();
|
||||
private static ConcurrentMap<File, FileChannel> channelCache = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
private FileChannelCache() {
|
||||
|
||||
@@ -40,7 +40,7 @@ import org.springframework.messaging.MessagingException;
|
||||
*/
|
||||
public class NioFileLocker extends AbstractFileLockerFilter {
|
||||
|
||||
private final ConcurrentMap<File, FileLock> lockCache = new ConcurrentHashMap<File, FileLock>();
|
||||
private final ConcurrentMap<File, FileLock> lockCache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean lock(File fileToLock) {
|
||||
@@ -68,7 +68,7 @@ public class NioFileLocker extends AbstractFileLockerFilter {
|
||||
|
||||
@Override
|
||||
public void unlock(File fileToUnlock) {
|
||||
FileLock fileLock = this.lockCache.get(fileToUnlock);
|
||||
FileLock fileLock = this.lockCache.remove(fileToUnlock);
|
||||
try {
|
||||
if (fileLock != null) {
|
||||
fileLock.release();
|
||||
|
||||
@@ -20,17 +20,20 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
/**
|
||||
* @author Iwein Fuld
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public class NioFileLockerTests {
|
||||
|
||||
@@ -47,13 +50,18 @@ public class NioFileLockerTests {
|
||||
};
|
||||
|
||||
@Test
|
||||
public void fileListedByFirstFilter() throws IOException {
|
||||
public void fileListedByFirstFilter() throws Exception {
|
||||
NioFileLocker filter = new NioFileLocker();
|
||||
File testFile = new File(workdir, "test0");
|
||||
testFile.createNewFile();
|
||||
assertThat(filter.filterFiles(workdir.listFiles()).get(0)).isEqualTo(testFile);
|
||||
filter.lock(testFile);
|
||||
assertThat(filter.filterFiles(workdir.listFiles()).get(0)).isEqualTo(testFile);
|
||||
filter.unlock(testFile);
|
||||
Field channelCache = FileChannelCache.class.getDeclaredField("channelCache");
|
||||
channelCache.setAccessible(true);
|
||||
assertThat(((Map<?, ?>) channelCache.get(null))).isEmpty();
|
||||
assertThat(((Map<?, ?>) TestUtils.getPropertyValue(filter, "lockCache", Map.class))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -64,7 +72,8 @@ public class NioFileLockerTests {
|
||||
testFile.createNewFile();
|
||||
assertThat(filter1.filterFiles(workdir.listFiles()).get(0)).isEqualTo(testFile);
|
||||
filter1.lock(testFile);
|
||||
assertThat(filter2.filterFiles(workdir.listFiles())).isEqualTo((List<File>) new ArrayList<File>());
|
||||
assertThat(filter2.filterFiles(workdir.listFiles())).isEqualTo(new ArrayList<File>());
|
||||
filter1.unlock(testFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user