Search in sources :

Example 1 with FileSystemPersistentAcceptOnceFileListFilter

use of org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter in project spring-integration by spring-projects.

the class WatchServiceDirectoryScannerTests method testWatchServiceDirectoryScanner.

@Test
public void testWatchServiceDirectoryScanner() throws Exception {
    FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
    fileReadingMessageSource.setDirectory(folder.getRoot());
    fileReadingMessageSource.setUseWatchService(true);
    fileReadingMessageSource.setWatchEvents(FileReadingMessageSource.WatchEventType.CREATE, FileReadingMessageSource.WatchEventType.MODIFY, FileReadingMessageSource.WatchEventType.DELETE);
    fileReadingMessageSource.setBeanFactory(mock(BeanFactory.class));
    final CountDownLatch removeFileLatch = new CountDownLatch(1);
    FileSystemPersistentAcceptOnceFileListFilter filter = new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "test") {

        @Override
        public boolean remove(File fileToRemove) {
            removeFileLatch.countDown();
            return super.remove(fileToRemove);
        }
    };
    fileReadingMessageSource.setFilter(filter);
    fileReadingMessageSource.afterPropertiesSet();
    fileReadingMessageSource.start();
    DirectoryScanner scanner = fileReadingMessageSource.getScanner();
    assertThat(scanner.getClass().getName(), containsString("FileReadingMessageSource$WatchServiceDirectoryScanner"));
    List<File> files = scanner.listFiles(folder.getRoot());
    assertEquals(3, files.size());
    assertTrue(files.contains(top1));
    assertTrue(files.contains(foo1));
    assertTrue(files.contains(bar1));
    fileReadingMessageSource.start();
    File top2 = this.folder.newFile();
    File foo2 = File.createTempFile("foo", ".txt", this.foo);
    File bar2 = File.createTempFile("bar", ".txt", this.bar);
    File baz = new File(this.foo, "baz");
    baz.mkdir();
    File baz1 = File.createTempFile("baz", ".txt", baz);
    files = scanner.listFiles(folder.getRoot());
    int n = 0;
    Set<File> accum = new HashSet<File>(files);
    while (n++ < 300 && accum.size() != 4) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertEquals(4, accum.size());
    assertTrue(accum.contains(top2));
    assertTrue(accum.contains(foo2));
    assertTrue(accum.contains(bar2));
    assertTrue(accum.contains(baz1));
    /*See AbstractWatchKey#signalEvent source code:
			if(var5 >= 512) {
				var1 = StandardWatchEventKinds.OVERFLOW;
			}
		*/
    fileReadingMessageSource.start();
    List<File> filesForOverflow = new ArrayList<File>(600);
    for (int i = 0; i < 600; i++) {
        filesForOverflow.add(this.folder.newFile("" + i));
    }
    n = 0;
    while (n++ < 300 && accum.size() < 604) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertEquals(604, accum.size());
    for (File fileForOverFlow : filesForOverflow) {
        accum.contains(fileForOverFlow);
    }
    File baz2 = File.createTempFile("baz2", ".txt", baz);
    n = 0;
    while (n++ < 300 && accum.size() < 605) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertTrue(accum.contains(baz2));
    File baz2Copy = new File(baz2.getAbsolutePath());
    baz2Copy.setLastModified(baz2.lastModified() + 100000);
    n = 0;
    files.clear();
    while (n++ < 300 && files.size() < 1) {
        Thread.sleep(100);
        files = scanner.listFiles(folder.getRoot());
        accum.addAll(files);
    }
    assertEquals(1, files.size());
    assertTrue(files.contains(baz2));
    baz2.delete();
    n = 0;
    while (n++ < 300 && removeFileLatch.getCount() > 0) {
        Thread.sleep(100);
        scanner.listFiles(folder.getRoot());
    }
    assertTrue(removeFileLatch.await(10, TimeUnit.SECONDS));
    File baz3 = File.createTempFile("baz3", ".txt", baz);
    n = 0;
    Message<File> fileMessage = null;
    while (n++ < 300 && (fileMessage = fileReadingMessageSource.receive()) == null) {
        Thread.sleep(100);
    }
    assertNotNull(fileMessage);
    assertEquals(baz3, fileMessage.getPayload());
    assertThat(fileMessage.getHeaders().get(FileHeaders.RELATIVE_PATH, String.class), startsWith(TestUtils.applySystemFileSeparator("foo/baz/")));
    fileReadingMessageSource.stop();
}
Also used : ArrayList(java.util.ArrayList) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) FileSystemPersistentAcceptOnceFileListFilter(org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter) BeanFactory(org.springframework.beans.factory.BeanFactory) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with FileSystemPersistentAcceptOnceFileListFilter

use of org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter in project spring-integration by spring-projects.

the class AbstractInboundFileSynchronizingMessageSource method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();
    Assert.notNull(this.localDirectory, "localDirectory must not be null");
    try {
        if (!this.localDirectory.exists()) {
            if (this.autoCreateLocalDirectory) {
                if (logger.isDebugEnabled()) {
                    logger.debug("The '" + this.localDirectory + "' directory doesn't exist; Will create.");
                }
                this.localDirectory.mkdirs();
            } else {
                throw new FileNotFoundException(this.localDirectory.getName());
            }
        }
        this.fileSource.setDirectory(this.localDirectory);
        if (this.localFileListFilter == null) {
            this.localFileListFilter = new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), getComponentName());
        }
        FileListFilter<File> filter = buildFilter();
        if (this.scannerExplicitlySet) {
            Assert.state(!this.fileSource.isUseWatchService(), "'useWatchService' and 'scanner' are mutually exclusive.");
            this.fileSource.getScanner().setFilter(filter);
        } else if (!this.fileSource.isUseWatchService()) {
            DirectoryScanner directoryScanner = new DefaultDirectoryScanner();
            directoryScanner.setFilter(filter);
            this.fileSource.setScanner(directoryScanner);
        } else {
            this.fileSource.setFilter(filter);
        }
        if (this.getBeanFactory() != null) {
            this.fileSource.setBeanFactory(this.getBeanFactory());
        }
        this.fileSource.afterPropertiesSet();
        this.synchronizer.afterPropertiesSet();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new BeanInitializationException("Failure during initialization of MessageSource for: " + this.getClass(), e);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) FileSystemPersistentAcceptOnceFileListFilter(org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter) DirectoryScanner(org.springframework.integration.file.DirectoryScanner) DefaultDirectoryScanner(org.springframework.integration.file.DefaultDirectoryScanner) FileNotFoundException(java.io.FileNotFoundException) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) File(java.io.File) DefaultDirectoryScanner(org.springframework.integration.file.DefaultDirectoryScanner) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Aggregations

File (java.io.File)2 FileSystemPersistentAcceptOnceFileListFilter (org.springframework.integration.file.filters.FileSystemPersistentAcceptOnceFileListFilter)2 SimpleMetadataStore (org.springframework.integration.metadata.SimpleMetadataStore)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1 DefaultDirectoryScanner (org.springframework.integration.file.DefaultDirectoryScanner)1 DirectoryScanner (org.springframework.integration.file.DirectoryScanner)1